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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
22 //
23 /****************************************************************************/
24 
25 /* =========================================================================
26  * included modules
27  * ======================================================================= */
28 #include <config.h>
29 
30 #include <utils/common/StdDefs.h>
32 #include "FXSingleEventThread.h"
33 #include "fxexdefs.h"
34 #ifndef WIN32
35 #include <pthread.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #else
39 #include <process.h>
40 #endif
41 #include <chrono>
42 #include <thread>
43 
44 #ifndef WIN32
45 # define PIPE_READ 0
46 # define PIPE_WRITE 1
47 #endif
48 
49 using namespace FXEX;
50 
51 // Message map
52 FXDEFMAP(FXSingleEventThread) FXSingleEventThreadMap[] = {
55 };
56 FXIMPLEMENT(FXSingleEventThread, FXObject, FXSingleEventThreadMap, ARRAYNUMBER(FXSingleEventThreadMap))
57 
58 
59 
61  : FXObject(), myClient(client) {
62  myApp = (a);
63 #ifndef WIN32
64  FXMALLOC(&event, FXThreadEventHandle, 2);
65  FXint res = pipe(event);
66  FXASSERT(res == 0);
67  UNUSED_PARAMETER(res); // only used for assertion
68  myApp->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
69 #else
70  event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
71  FXASSERT(event != NULL);
72  myApp->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
73 #endif
74 }
75 
76 
78 #ifndef WIN32
79  myApp->removeInput(event[PIPE_READ], INPUT_READ);
80  ::close(event[PIPE_READ]);
81  ::close(event[PIPE_WRITE]);
82  FXFREE(&event);
83 #else
84  myApp->removeInput(event, INPUT_READ);
85  ::CloseHandle(event);
86 #endif
87 }
88 
89 
90 void
92 #ifndef WIN32
93  FXuint seltype = SEL_THREAD;
94  FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
95  UNUSED_PARAMETER(res); // to make the compiler happy
96 #else
97  ::SetEvent(event);
98 #endif
99 }
100 
101 
102 void
104  UNUSED_PARAMETER(seltype);
105 #ifndef WIN32
106  FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
107  UNUSED_PARAMETER(res); // to make the compiler happy
108 #else
109  ::SetEvent(event);
110 #endif
111 }
112 
113 
114 long
115 FXSingleEventThread::onThreadSignal(FXObject*, FXSelector, void*) {
116 #ifndef WIN32
117  FXuint seltype = SEL_THREAD;
118  FXint res = ::read(event[PIPE_READ], &seltype, sizeof(seltype));
119  UNUSED_PARAMETER(res); // to make the compiler happy
120 #else
121  //FIXME need win32 support
122 #endif
123  FXSelector sel = FXSEL(SEL_THREAD, 0);
124  handle(this, sel, nullptr);
125  return 0;
126 }
127 
128 
129 long
130 FXSingleEventThread::onThreadEvent(FXObject*, FXSelector, void*) {
131  myClient->eventOccurred();
132  /*
133  FXuint seltype1 = FXSELTYPE(SEL_THREAD);
134  if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {
135  }
136  FXuint seltype = FXSELTYPE(sel);
137  return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);
138  */
139  return 1;
140 }
141 
142 
143 void
145  std::this_thread::sleep_for(std::chrono::milliseconds(ms));
146 }
147 
148 
149 
#define PIPE_READ
#define PIPE_WRITE
@ ID_THREAD_EVENT
ID for message passing between threads.
Definition: GUIAppEnum.h:292
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:29
static void sleep(long ms)
long onThreadEvent(FXObject *, FXSelector, void *)
long onThreadSignal(FXObject *, FXSelector, void *)
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:303
@ SEL_THREAD
Definition: fxexdefs.h:169
FXDEFMAP(FXBaseObject) FXBaseObjectMap[]