Eclipse SUMO - Simulation of Urban MObility
FXSingleEventThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2004-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 //
19 /****************************************************************************/
20 
21 /* =========================================================================
22  * included modules
23  * ======================================================================= */
24 #include <config.h>
25 
26 #include <utils/common/StdDefs.h>
28 #include "FXSingleEventThread.h"
29 #include "fxexdefs.h"
30 #ifndef WIN32
31 #include <pthread.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #else
35 #include <process.h>
36 #endif
37 #include <chrono>
38 #include <thread>
39 
40 #ifndef WIN32
41 # define PIPE_READ 0
42 # define PIPE_WRITE 1
43 #endif
44 
45 using namespace FXEX;
46 
47 // Message map
48 FXDEFMAP(FXSingleEventThread) FXSingleEventThreadMap[] = {
51 };
52 FXIMPLEMENT(FXSingleEventThread, FXObject, FXSingleEventThreadMap, ARRAYNUMBER(FXSingleEventThreadMap))
53 
54 
55 
57  : FXObject(), myClient(client) {
58  myApp = (a);
59 #ifndef WIN32
60  FXMALLOC(&event, FXThreadEventHandle, 2);
61  FXint res = pipe(event);
62  FXASSERT(res == 0);
63  UNUSED_PARAMETER(res); // only used for assertion
64  myApp->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
65 #else
66  event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
67  FXASSERT(event != NULL);
68  myApp->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
69 #endif
70 }
71 
72 
74 #ifndef WIN32
75  myApp->removeInput(event[PIPE_READ], INPUT_READ);
76  ::close(event[PIPE_READ]);
77  ::close(event[PIPE_WRITE]);
78  FXFREE(&event);
79 #else
80  myApp->removeInput(event, INPUT_READ);
81  ::CloseHandle(event);
82 #endif
83 }
84 
85 
86 void
88 #ifndef WIN32
89  FXuint seltype = SEL_THREAD;
90  FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
91  UNUSED_PARAMETER(res); // to make the compiler happy
92 #else
93  ::SetEvent(event);
94 #endif
95 }
96 
97 
98 void
99 FXSingleEventThread::signal(FXuint seltype) {
100  UNUSED_PARAMETER(seltype);
101 #ifndef WIN32
102  FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
103  UNUSED_PARAMETER(res); // to make the compiler happy
104 #else
105  ::SetEvent(event);
106 #endif
107 }
108 
109 
110 long
111 FXSingleEventThread::onThreadSignal(FXObject*, FXSelector, void*) {
112 #ifndef WIN32
113  FXuint seltype = SEL_THREAD;
114  FXint res = ::read(event[PIPE_READ], &seltype, sizeof(seltype));
115  UNUSED_PARAMETER(res); // to make the compiler happy
116 #else
117  //FIXME need win32 support
118 #endif
119  FXSelector sel = FXSEL(SEL_THREAD, 0);
120  handle(this, sel, nullptr);
121  return 0;
122 }
123 
124 
125 long
126 FXSingleEventThread::onThreadEvent(FXObject*, FXSelector, void*) {
127  myClient->eventOccurred();
128  /*
129  FXuint seltype1 = FXSELTYPE(SEL_THREAD);
130  if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {
131  }
132  FXuint seltype = FXSELTYPE(sel);
133  return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);
134  */
135  return 1;
136 }
137 
138 
139 void
141  std::this_thread::sleep_for(std::chrono::milliseconds(ms));
142 }
143 
144 
145 
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:31
FXEX::SEL_THREAD
Definition: fxexdefs.h:165
fxexdefs.h
MFXInterThreadEventClient
Definition: MFXInterThreadEventClient.h:27
FXSingleEventThread::sleep
static void sleep(long ms)
Definition: FXSingleEventThread.cpp:140
PIPE_READ
#define PIPE_READ
Definition: FXSingleEventThread.cpp:41
FXSingleEventThread::onThreadSignal
long onThreadSignal(FXObject *, FXSelector, void *)
Definition: FXSingleEventThread.cpp:111
ID_THREAD_EVENT
ID for message passing between threads.
Definition: GUIAppEnum.h:275
FXSingleEventThread::onThreadEvent
long onThreadEvent(FXObject *, FXSelector, void *)
Definition: FXSingleEventThread.cpp:126
FXEX
Definition: FXBaseObject.cpp:47
FXSingleEventThread.h
FXEX::FXDEFMAP
FXDEFMAP(FXBaseObject) FXBaseObjectMap[]
FXEX::FXThreadEventHandle
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:299
MFXInterThreadEventClient.h
PIPE_WRITE
#define PIPE_WRITE
Definition: FXSingleEventThread.cpp:42
FXSingleEventThread::~FXSingleEventThread
virtual ~FXSingleEventThread()
Definition: FXSingleEventThread.cpp:73
FXSingleEventThread::ID_THREAD_EVENT
Definition: FXSingleEventThread.h:46
config.h
StdDefs.h
FXSingleEventThread
Definition: FXSingleEventThread.h:34
FXSingleEventThread::signal
void signal()
Definition: FXSingleEventThread.cpp:87