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.sourceforge.net/
10 // Copyright (C) 2001-2012 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  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  FXuint seltype = SEL_THREAD;
107 #ifndef WIN32
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