SUMO - Simulation of Urban MObility
FXSingleEventThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2004-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
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 <utils/common/StdDefs.h>
35 #include "FXSingleEventThread.h"
36 #include "fxexdefs.h"
37 #ifndef WIN32
38 #include <pthread.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #else
42 #include <process.h>
43 #endif
44 
45 #ifndef WIN32
46 # define PIPE_READ 0
47 # define PIPE_WRITE 1
48 #endif
49 
50 using namespace FXEX;
51 
52 // Message map
53 FXDEFMAP(FXSingleEventThread) FXSingleEventThreadMap[] = {
56 };
57 FXIMPLEMENT(FXSingleEventThread, FXObject, FXSingleEventThreadMap, ARRAYNUMBER(FXSingleEventThreadMap))
58 
59 
60 
62  : FXObject(), myClient(client) {
63  myApp = (a);
64 #ifndef WIN32
65  FXMALLOC(&event, FXThreadEventHandle, 2);
66  FXint res = pipe(event);
67  FXASSERT(res == 0);
68  UNUSED_PARAMETER(res); // only used for assertion
69  myApp->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
70 #else
71  event = CreateEvent(NULL, FALSE, FALSE, NULL);
72  FXASSERT(event != NULL);
73  myApp->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
74 #endif
75 }
76 
77 
79 #ifndef WIN32
80  myApp->removeInput(event[PIPE_READ], INPUT_READ);
81  ::close(event[PIPE_READ]);
82  ::close(event[PIPE_WRITE]);
83  FXFREE(&event);
84 #else
85  myApp->removeInput(event, INPUT_READ);
86  ::CloseHandle(event);
87 #endif
88 }
89 
90 
91 void
93 #ifndef WIN32
94  FXuint seltype = SEL_THREAD;
95  FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
96  UNUSED_PARAMETER(res); // to make the compiler happy
97 #else
98  ::SetEvent(event);
99 #endif
100 }
101 
102 
103 void
105  UNUSED_PARAMETER(seltype);
106 #ifndef WIN32
107  FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
108  UNUSED_PARAMETER(res); // to make the compiler happy
109 #else
110  ::SetEvent(event);
111 #endif
112 }
113 
114 
115 long
116 FXSingleEventThread::onThreadSignal(FXObject*, FXSelector, void*) {
117 #ifndef WIN32
118  FXuint seltype = SEL_THREAD;
119  FXint res = ::read(event[PIPE_READ], &seltype, sizeof(seltype));
120  UNUSED_PARAMETER(res); // to make the compiler happy
121 #else
122  //FIXME need win32 support
123 #endif
124  FXSelector sel = FXSEL(SEL_THREAD, 0);
125  handle(this, sel, 0);
126  return 0;
127 }
128 
129 
130 long
131 FXSingleEventThread::onThreadEvent(FXObject*, FXSelector , void*) {
132  myClient->eventOccured();
133  /*
134  FXuint seltype1 = FXSELTYPE(SEL_THREAD);
135  if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {
136  }
137  FXuint seltype = FXSELTYPE(sel);
138  return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);
139  */
140  return 1;
141 }
142 
143 
144 void
146 #ifdef WIN32
147  Sleep(ms);
148 #else
149  long long us = ms * 1000;
150  usleep(us);
151 #endif
152 }
153 
154 
155 
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:306
#define PIPE_READ
long onThreadEvent(FXObject *, FXSelector, void *)
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
ID for message passing between threads.
Definition: GUIAppEnum.h:120
static void sleep(long ms)
#define PIPE_WRITE
FXDEFMAP(FXBaseObject) FXBaseObjectMap[]
long onThreadSignal(FXObject *, FXSelector, void *)