SUMO - Simulation of Urban MObility
FXThreadEvent.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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <fxver.h>
33 #define NOMINMAX
34 #include <xincs.h>
35 #undef NOMINMAX
36 #include <fx.h>
37 #include <utils/common/StdDefs.h>
38 /*
39 #include <fxdefs.h>
40 #include <FXString.h>
41 #include <FXStream.h>
42 #include <FXSize.h>
43 #include <FXPoint.h>
44 #include <FXRectangle.h>
45 #include <FXRegistry.h>
46 #include <FXHash.h>
47 #include <FXApp.h>
48 */
49 #ifndef WIN32
50 #include <unistd.h>
51 #endif
52 
53 using namespace FX;
54 #include "fxexdefs.h"
55 #include "FXThreadEvent.h"
56 
57 // ===========================================================================
58 // used namespaces
59 // ===========================================================================
60 using namespace FXEX;
61 namespace FXEX {
62 
63 #ifndef WIN32
64 # define PIPE_READ 0
65 # define PIPE_WRITE 1
66 #endif
67 
68 // Message map
69 FXDEFMAP(FXThreadEvent) FXThreadEventMap[] = {
70  FXMAPTYPE(0, FXThreadEvent::onThreadEvent),
71  FXMAPFUNC(SEL_THREAD, 0, FXThreadEvent::onThreadEvent),
72  FXMAPFUNC(SEL_IO_READ, FXThreadEvent::ID_THREAD_EVENT, FXThreadEvent::onThreadSignal),
73 };
74 FXIMPLEMENT(FXThreadEvent, FXBaseObject, FXThreadEventMap, ARRAYNUMBER(FXThreadEventMap))
75 
76 // FXThreadEvent : Constructor
77 FXThreadEvent::FXThreadEvent(FXObject* tgt, FXSelector sel) : FXBaseObject(tgt, sel) {
78 #ifndef WIN32
79  FXMALLOC(&event, FXThreadEventHandle, 2);
80  FXint res = pipe(event);
81  FXASSERT(res == 0);
82  UNUSED_PARAMETER(res); // only used for assertion
83  getApp()->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
84 #else
85  event = CreateEvent(NULL, FALSE, FALSE, NULL);
86  FXASSERT(event != NULL);
87  getApp()->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
88 #endif
89 }
90 
91 // ~FXThreadEvent : Destructor
92 FXThreadEvent::~FXThreadEvent() {
93 #ifndef WIN32
94  getApp()->removeInput(event[PIPE_READ], INPUT_READ);
95  ::close(event[PIPE_READ]);
96  ::close(event[PIPE_WRITE]);
97  FXFREE(&event);
98 #else
99  getApp()->removeInput(event, INPUT_READ);
100  ::CloseHandle(event);
101 #endif
102 }
103 
104 // signal the target using the SEL_THREAD seltype
105 // this method is meant to be called from the worker thread
106 void FXThreadEvent::signal() {
107 #ifndef WIN32
108  FXuint seltype = SEL_THREAD;
109  FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
110  UNUSED_PARAMETER(res); // to make the compiler happy
111 #else
112  ::SetEvent(event);
113 #endif
114 }
115 
116 // signal the target using some seltype
117 // this method is meant to be called from the worker thread
118 void FXThreadEvent::signal(FXuint seltype) {
119 #ifndef WIN32
120  FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
121  UNUSED_PARAMETER(res); // to make the compiler happy
122 #else
123  UNUSED_PARAMETER(seltype);
124  ::SetEvent(event);
125 #endif
126 }
127 
128 // this thread is signalled via the IO/event, from other thread.
129 // We also figure out what SEL_type to generate.
130 // We forward it to ourselves first, to allow child classes to handle the event.
131 long FXThreadEvent::onThreadSignal(FXObject*, FXSelector, void*) {
132  FXuint seltype = SEL_THREAD;
133 #ifndef WIN32
134  FXint res = ::read(event[PIPE_READ], &seltype, sizeof(seltype));
135  UNUSED_PARAMETER(res); // to make the compiler happy
136 #else
137  //FIXME need win32 support
138 #endif
139  handle(this, FXSEL(seltype, 0), NULL);
140  return 0;
141 }
142 
143 // forward thread event to application - we generate the appropriate FOX event
144 // which is now in the main thread (ie no longer in the worker thread)
145 long FXThreadEvent::onThreadEvent(FXObject*, FXSelector sel, void*) {
146  FXuint seltype = FXSELTYPE(sel);
147  return target && target->handle(this, FXSEL(seltype, message), NULL);
148 }
149 
150 }
151 
152 
153 /****************************************************************************/
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:306
#define PIPE_READ
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
ID for message passing between threads.
Definition: GUIAppEnum.h:120
#define PIPE_WRITE
FXDEFMAP(FXRealSpinDialDial) FXSpinDialMap[]