SUMO - Simulation of Urban MObility
FXSingleEventThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 //
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2004-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 /* =========================================================================
26  * included modules
27  * ======================================================================= */
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <utils/common/StdDefs.h>
36 #include "FXSingleEventThread.h"
37 #include "fxexdefs.h"
38 #ifndef WIN32
39 #include <pthread.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #else
43 #include <process.h>
44 #endif
45 
46 #ifndef WIN32
47 # define PIPE_READ 0
48 # define PIPE_WRITE 1
49 #endif
50 
51 using namespace FXEX;
52 
53 // Message map
54 FXDEFMAP(FXSingleEventThread) FXSingleEventThreadMap[] = {
57 };
58 FXIMPLEMENT(FXSingleEventThread, FXObject, FXSingleEventThreadMap, ARRAYNUMBER(FXSingleEventThreadMap))
59 
60 
61 
63  : FXObject(), myClient(client) {
64  myApp = (a);
65 #ifndef WIN32
66  FXMALLOC(&event, FXThreadEventHandle, 2);
67  FXint res = pipe(event);
68  FXASSERT(res == 0);
69  UNUSED_PARAMETER(res); // only used for assertion
70  myApp->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
71 #else
72  event = CreateEvent(NULL, FALSE, FALSE, NULL);
73  FXASSERT(event != NULL);
74  myApp->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
75 #endif
76 }
77 
78 
80 #ifndef WIN32
81  myApp->removeInput(event[PIPE_READ], INPUT_READ);
82  ::close(event[PIPE_READ]);
83  ::close(event[PIPE_WRITE]);
84  FXFREE(&event);
85 #else
86  myApp->removeInput(event, INPUT_READ);
87  ::CloseHandle(event);
88 #endif
89 }
90 
91 
92 void
94 #ifndef WIN32
95  FXuint seltype = SEL_THREAD;
96  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
97 #else
98  ::SetEvent(event);
99 #endif
100 }
101 
102 
103 void
105  UNUSED_PARAMETER(seltype);
106 #ifndef WIN32
107  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
108 #else
109  ::SetEvent(event);
110 #endif
111 }
112 
113 
114 long
115 FXSingleEventThread::onThreadSignal(FXObject*, FXSelector, void*) {
116 #ifndef WIN32
117  FXuint seltype = SEL_THREAD;
118  ::read(event[PIPE_READ], &seltype, sizeof(seltype));
119 #else
120  //FIXME need win32 support
121 #endif
122  FXSelector sel = FXSEL(SEL_THREAD, 0);
123  handle(this, sel, 0);
124  return 0;
125 }
126 
127 
128 long
129 FXSingleEventThread::onThreadEvent(FXObject*, FXSelector , void*) {
130  myClient->eventOccured();
131  /*
132  FXuint seltype1 = FXSELTYPE(SEL_THREAD);
133  if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {
134  }
135  FXuint seltype = FXSELTYPE(sel);
136  return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);
137  */
138  return 1;
139 }
140 
141 
142 void
144 #ifdef WIN32
145  Sleep(ms);
146 #else
147  long long us = ms * 1000;
148  usleep(us);
149 #endif
150 }
151 
152 
153 
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:307
#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:121
#define PIPE_WRITE
FXDEFMAP(FXBaseObject) FXBaseObjectMap[]
long onThreadSignal(FXObject *, FXSelector, void *)