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  myApp->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
50 #else
51  event = CreateEvent(NULL, FALSE, FALSE, NULL);
52  FXASSERT(event != NULL);
53  myApp->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
54 #endif
55 }
56 
57 
59 #ifndef WIN32
60  myApp->removeInput(event[PIPE_READ], INPUT_READ);
61  ::close(event[PIPE_READ]);
62  ::close(event[PIPE_WRITE]);
63  FXFREE(&event);
64 #else
65  myApp->removeInput(event, INPUT_READ);
66  ::CloseHandle(event);
67 #endif
68 }
69 
70 
71 void
73 #ifndef WIN32
74  FXuint seltype = SEL_THREAD;
75  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
76 #else
77  ::SetEvent(event);
78 #endif
79 }
80 
81 
82 void
83 FXSingleEventThread::signal(FXuint seltype) {
84  UNUSED_PARAMETER(seltype);
85 #ifndef WIN32
86  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
87 #else
88  ::SetEvent(event);
89 #endif
90 }
91 
92 
93 long
95 #ifndef WIN32
96  FXuint seltype = SEL_THREAD;
97  ::read(event[PIPE_READ], &seltype, sizeof(seltype));
98 #else
99  //FIXME need win32 support
100 #endif
101  FXSelector sel = FXSEL(SEL_THREAD, 0);
102  handle(this, sel, 0);
103  return 0;
104 }
105 
106 
107 long
109  myClient->eventOccured();
110  /*
111  FXuint seltype1 = FXSELTYPE(SEL_THREAD);
112  if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {
113  }
114  FXuint seltype = FXSELTYPE(sel);
115  return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);
116  */
117  return 1;
118 }
119 
120 
121 void
123 #ifdef WIN32
124  Sleep(ms);
125 #else
126  long long us = ms * 1000;
127  usleep(us);
128 #endif
129 }
130 
131 
132 
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:306
#define PIPE_READ
long onThreadEvent(FXObject *, FXSelector, void *)
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:36
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 *)