SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FXSingleEventThread.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  * included modules
3  * ======================================================================= */
4 #ifdef _MSC_VER
5 #include <windows_config.h>
6 #else
7 #include <config.h>
8 #endif
9 
10 #include <utils/common/StdDefs.h>
12 #include "FXSingleEventThread.h"
13 #include "fxexdefs.h"
14 #ifndef WIN32
15 #include <pthread.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #else
19 #include <process.h>
20 #endif
21 
22 #ifdef CHECK_MEMORY_LEAKS
23 #include <foreign/nvwa/debug_new.h>
24 #endif
25 
26 #ifndef WIN32
27 # define PIPE_READ 0
28 # define PIPE_WRITE 1
29 #endif
30 
31 using namespace FXEX;
32 
33 // Message map
34 FXDEFMAP(FXSingleEventThread) FXSingleEventThreadMap[] = {
37 };
38 FXIMPLEMENT(FXSingleEventThread, FXObject, FXSingleEventThreadMap, ARRAYNUMBER(FXSingleEventThreadMap))
39 
40 
41 
43  : FXObject(), myClient(client) {
44  myApp = (a);
45 #ifndef WIN32
46  FXMALLOC(&event, FXThreadEventHandle, 2);
47  FXint res = pipe(event);
48  FXASSERT(res == 0);
49  UNUSED_PARAMETER(res); // only used for assertion
50  myApp->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
51 #else
52  event = CreateEvent(NULL, FALSE, FALSE, NULL);
53  FXASSERT(event != NULL);
54  myApp->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
55 #endif
56 }
57 
58 
60 #ifndef WIN32
61  myApp->removeInput(event[PIPE_READ], INPUT_READ);
62  ::close(event[PIPE_READ]);
63  ::close(event[PIPE_WRITE]);
64  FXFREE(&event);
65 #else
66  myApp->removeInput(event, INPUT_READ);
67  ::CloseHandle(event);
68 #endif
69 }
70 
71 
72 void
74 #ifndef WIN32
75  FXuint seltype = SEL_THREAD;
76  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
77 #else
78  ::SetEvent(event);
79 #endif
80 }
81 
82 
83 void
84 FXSingleEventThread::signal(FXuint seltype) {
85  UNUSED_PARAMETER(seltype);
86 #ifndef WIN32
87  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
88 #else
89  ::SetEvent(event);
90 #endif
91 }
92 
93 
94 long
96 #ifndef WIN32
97  FXuint seltype = SEL_THREAD;
98  ::read(event[PIPE_READ], &seltype, sizeof(seltype));
99 #else
100  //FIXME need win32 support
101 #endif
102  FXSelector sel = FXSEL(SEL_THREAD, 0);
103  handle(this, sel, 0);
104  return 0;
105 }
106 
107 
108 long
110  myClient->eventOccured();
111  /*
112  FXuint seltype1 = FXSELTYPE(SEL_THREAD);
113  if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {
114  }
115  FXuint seltype = FXSELTYPE(sel);
116  return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);
117  */
118  return 1;
119 }
120 
121 
122 void
124 #ifdef WIN32
125  Sleep(ms);
126 #else
127  long long us = ms * 1000;
128  usleep(us);
129 #endif
130 }
131 
132 
133 
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:306
#define PIPE_READ
long onThreadEvent(FXObject *, FXSelector, void *)
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:38
static void sleep(long ms)
ID for message passing between threads.
Definition: GUIAppEnum.h:117
#define PIPE_WRITE
FXDEFMAP(FXBaseObject) FXBaseObjectMap[]
long onThreadSignal(FXObject *, FXSelector, void *)