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