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-2016 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 #include <xincs.h>
35 #include <fx.h>
36 #include <utils/common/StdDefs.h>
37 /*
38 #include <fxdefs.h>
39 #include <FXString.h>
40 #include <FXStream.h>
41 #include <FXSize.h>
42 #include <FXPoint.h>
43 #include <FXRectangle.h>
44 #include <FXRegistry.h>
45 #include <FXHash.h>
46 #include <FXApp.h>
47 */
48 #ifndef WIN32
49 #include <unistd.h>
50 #endif
51 
52 using namespace FX;
53 #include "fxexdefs.h"
54 #include "FXThreadEvent.h"
55 
56 #ifdef CHECK_MEMORY_LEAKS
57 #include <foreign/nvwa/debug_new.h>
58 // ===========================================================================
59 // used namespaces
60 // ===========================================================================
61 #endif // _DEBUG
62 using namespace FXEX;
63 namespace FXEX {
64 
65 #ifndef WIN32
66 # define PIPE_READ 0
67 # define PIPE_WRITE 1
68 #endif
69 
70 // Message map
71 FXDEFMAP(FXThreadEvent) FXThreadEventMap[] = {
72  FXMAPTYPE(0, FXThreadEvent::onThreadEvent),
73  FXMAPFUNC(SEL_THREAD, 0, FXThreadEvent::onThreadEvent),
74  FXMAPFUNC(SEL_IO_READ, FXThreadEvent::ID_THREAD_EVENT, FXThreadEvent::onThreadSignal),
75 };
76 FXIMPLEMENT(FXThreadEvent, FXBaseObject, FXThreadEventMap, ARRAYNUMBER(FXThreadEventMap))
77 
78 // FXThreadEvent : Constructor
79 FXThreadEvent::FXThreadEvent(FXObject* tgt, FXSelector sel) : FXBaseObject(tgt, sel) {
80 #ifndef WIN32
81  FXMALLOC(&event, FXThreadEventHandle, 2);
82  FXint res = pipe(event);
83  FXASSERT(res == 0);
84  UNUSED_PARAMETER(res); // only used for assertion
85  getApp()->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
86 #else
87  event = CreateEvent(NULL, FALSE, FALSE, NULL);
88  FXASSERT(event != NULL);
89  getApp()->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
90 #endif
91 }
92 
93 // ~FXThreadEvent : Destructor
94 FXThreadEvent::~FXThreadEvent() {
95 #ifndef WIN32
96  getApp()->removeInput(event[PIPE_READ], INPUT_READ);
97  ::close(event[PIPE_READ]);
98  ::close(event[PIPE_WRITE]);
99  FXFREE(&event);
100 #else
101  getApp()->removeInput(event, INPUT_READ);
102  ::CloseHandle(event);
103 #endif
104 }
105 
106 // signal the target using the SEL_THREAD seltype
107 // this method is meant to be called from the worker thread
108 void FXThreadEvent::signal() {
109 #ifndef WIN32
110  FXuint seltype = SEL_THREAD;
111  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
112 #else
113  ::SetEvent(event);
114 #endif
115 }
116 
117 // signal the target using some seltype
118 // this method is meant to be called from the worker thread
119 void FXThreadEvent::signal(FXuint seltype) {
120 #ifndef WIN32
121  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
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  ::read(event[PIPE_READ], &seltype, sizeof(seltype));
135 #else
136  //FIXME need win32 support
137 #endif
138  handle(this, FXSEL(seltype, 0), NULL);
139  return 0;
140 }
141 
142 // forward thread event to application - we generate the appropriate FOX event
143 // which is now in the main thread (ie no longer in the worker thread)
144 long FXThreadEvent::onThreadEvent(FXObject*, FXSelector sel, void*) {
145  FXuint seltype = FXSELTYPE(sel);
146  return target && target->handle(this, FXSEL(seltype, message), NULL);
147 }
148 
149 }
150 
151 
152 
153 /****************************************************************************/
154 
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:39
#define PIPE_WRITE
FXDEFMAP(FXRealSpinDialDial) FXSpinDialMap[]