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