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