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 #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 // ===========================================================================
57 // used namespaces
58 // ===========================================================================
59 using namespace FXEX;
60 namespace FXEX {
61 
62 #ifndef WIN32
63 # define PIPE_READ 0
64 # define PIPE_WRITE 1
65 #endif
66 
67 // Message map
68 FXDEFMAP(FXThreadEvent) FXThreadEventMap[] = {
69  FXMAPTYPE(0, FXThreadEvent::onThreadEvent),
70  FXMAPFUNC(SEL_THREAD, 0, FXThreadEvent::onThreadEvent),
71  FXMAPFUNC(SEL_IO_READ, FXThreadEvent::ID_THREAD_EVENT, FXThreadEvent::onThreadSignal),
72 };
73 FXIMPLEMENT(FXThreadEvent, FXBaseObject, FXThreadEventMap, ARRAYNUMBER(FXThreadEventMap))
74 
75 // FXThreadEvent : Constructor
76 FXThreadEvent::FXThreadEvent(FXObject* tgt, FXSelector sel) : FXBaseObject(tgt, sel) {
77 #ifndef WIN32
78  FXMALLOC(&event, FXThreadEventHandle, 2);
79  FXint res = pipe(event);
80  FXASSERT(res == 0);
81  UNUSED_PARAMETER(res); // only used for assertion
82  getApp()->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
83 #else
84  event = CreateEvent(NULL, FALSE, FALSE, NULL);
85  FXASSERT(event != NULL);
86  getApp()->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
87 #endif
88 }
89 
90 // ~FXThreadEvent : Destructor
91 FXThreadEvent::~FXThreadEvent() {
92 #ifndef WIN32
93  getApp()->removeInput(event[PIPE_READ], INPUT_READ);
94  ::close(event[PIPE_READ]);
95  ::close(event[PIPE_WRITE]);
96  FXFREE(&event);
97 #else
98  getApp()->removeInput(event, INPUT_READ);
99  ::CloseHandle(event);
100 #endif
101 }
102 
103 // signal the target using the SEL_THREAD seltype
104 // this method is meant to be called from the worker thread
105 void FXThreadEvent::signal() {
106 #ifndef WIN32
107  FXuint seltype = SEL_THREAD;
108  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
109 #else
110  ::SetEvent(event);
111 #endif
112 }
113 
114 // signal the target using some seltype
115 // this method is meant to be called from the worker thread
116 void FXThreadEvent::signal(FXuint seltype) {
117 #ifndef WIN32
118  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
119 #else
120  UNUSED_PARAMETER(seltype);
121  ::SetEvent(event);
122 #endif
123 }
124 
125 // this thread is signalled via the IO/event, from other thread.
126 // We also figure out what SEL_type to generate.
127 // We forward it to ourselves first, to allow child classes to handle the event.
128 long FXThreadEvent::onThreadSignal(FXObject*, FXSelector, void*) {
129  FXuint seltype = SEL_THREAD;
130 #ifndef WIN32
131  ::read(event[PIPE_READ], &seltype, sizeof(seltype));
132 #else
133  //FIXME need win32 support
134 #endif
135  handle(this, FXSEL(seltype, 0), NULL);
136  return 0;
137 }
138 
139 // forward thread event to application - we generate the appropriate FOX event
140 // which is now in the main thread (ie no longer in the worker thread)
141 long FXThreadEvent::onThreadEvent(FXObject*, FXSelector sel, void*) {
142  FXuint seltype = FXSELTYPE(sel);
143  return target && target->handle(this, FXSEL(seltype, message), NULL);
144 }
145 
146 }
147 
148 
149 
150 /****************************************************************************/
151 
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:307
#define PIPE_READ
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:38
ID for message passing between threads.
Definition: GUIAppEnum.h:121
#define PIPE_WRITE
FXDEFMAP(FXRealSpinDialDial) FXSpinDialMap[]