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