SUMO - Simulation of Urban MObility
FXThreadEvent.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 //
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2004-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <fxver.h>
34 #define NOMINMAX
35 #include <xincs.h>
36 #undef NOMINMAX
37 #include <fx.h>
38 #include <utils/common/StdDefs.h>
39 /*
40 #include <fxdefs.h>
41 #include <FXString.h>
42 #include <FXStream.h>
43 #include <FXSize.h>
44 #include <FXPoint.h>
45 #include <FXRectangle.h>
46 #include <FXRegistry.h>
47 #include <FXHash.h>
48 #include <FXApp.h>
49 */
50 #ifndef WIN32
51 #include <unistd.h>
52 #endif
53 
54 using namespace FX;
55 #include "fxexdefs.h"
56 #include "FXThreadEvent.h"
57 
58 // ===========================================================================
59 // used namespaces
60 // ===========================================================================
61 using namespace FXEX;
62 namespace FXEX {
63 
64 #ifndef WIN32
65 # define PIPE_READ 0
66 # define PIPE_WRITE 1
67 #endif
68 
69 // Message map
70 FXDEFMAP(FXThreadEvent) FXThreadEventMap[] = {
71  FXMAPTYPE(0, FXThreadEvent::onThreadEvent),
72  FXMAPFUNC(SEL_THREAD, 0, FXThreadEvent::onThreadEvent),
73  FXMAPFUNC(SEL_IO_READ, FXThreadEvent::ID_THREAD_EVENT, FXThreadEvent::onThreadSignal),
74 };
75 FXIMPLEMENT(FXThreadEvent, FXBaseObject, FXThreadEventMap, ARRAYNUMBER(FXThreadEventMap))
76 
77 // FXThreadEvent : Constructor
78 FXThreadEvent::FXThreadEvent(FXObject* tgt, FXSelector sel) : FXBaseObject(tgt, sel) {
79 #ifndef WIN32
80  FXMALLOC(&event, FXThreadEventHandle, 2);
81  FXint res = pipe(event);
82  FXASSERT(res == 0);
83  UNUSED_PARAMETER(res); // only used for assertion
84  getApp()->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
85 #else
86  event = CreateEvent(NULL, FALSE, FALSE, NULL);
87  FXASSERT(event != NULL);
88  getApp()->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
89 #endif
90 }
91 
92 // ~FXThreadEvent : Destructor
93 FXThreadEvent::~FXThreadEvent() {
94 #ifndef WIN32
95  getApp()->removeInput(event[PIPE_READ], INPUT_READ);
96  ::close(event[PIPE_READ]);
97  ::close(event[PIPE_WRITE]);
98  FXFREE(&event);
99 #else
100  getApp()->removeInput(event, INPUT_READ);
101  ::CloseHandle(event);
102 #endif
103 }
104 
105 // signal the target using the SEL_THREAD seltype
106 // this method is meant to be called from the worker thread
107 void FXThreadEvent::signal() {
108 #ifndef WIN32
109  FXuint seltype = SEL_THREAD;
110  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
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  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
121 #else
122  UNUSED_PARAMETER(seltype);
123  ::SetEvent(event);
124 #endif
125 }
126 
127 // this thread is signalled via the IO/event, from other thread.
128 // We also figure out what SEL_type to generate.
129 // We forward it to ourselves first, to allow child classes to handle the event.
130 long FXThreadEvent::onThreadSignal(FXObject*, FXSelector, void*) {
131  FXuint seltype = SEL_THREAD;
132 #ifndef WIN32
133  ::read(event[PIPE_READ], &seltype, sizeof(seltype));
134 #else
135  //FIXME need win32 support
136 #endif
137  handle(this, FXSEL(seltype, 0), NULL);
138  return 0;
139 }
140 
141 // forward thread event to application - we generate the appropriate FOX event
142 // which is now in the main thread (ie no longer in the worker thread)
143 long FXThreadEvent::onThreadEvent(FXObject*, FXSelector sel, void*) {
144  FXuint seltype = FXSELTYPE(sel);
145  return target && target->handle(this, FXSEL(seltype, message), NULL);
146 }
147 
148 }
149 
150 
151 
152 /****************************************************************************/
153 
ID for message passing between threads.
Definition: GUIAppEnum.h:121
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:307
#define PIPE_READ
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:40
#define PIPE_WRITE
FXDEFMAP(FXRealSpinDialDial) FXSpinDialMap[]