SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif
49 
50 #ifndef WIN32
51 # define PIPE_READ 0
52 # define PIPE_WRITE 1
53 #endif
54 
55 using namespace FXEX;
56 
57 // Message map
58 FXDEFMAP(FXSingleEventThread) FXSingleEventThreadMap[] = {
61 };
62 FXIMPLEMENT(FXSingleEventThread, FXObject, FXSingleEventThreadMap, ARRAYNUMBER(FXSingleEventThreadMap))
63 
64 
65 
67  : FXObject(), myClient(client) {
68  myApp = (a);
69 #ifndef WIN32
70  FXMALLOC(&event, FXThreadEventHandle, 2);
71  FXint res = pipe(event);
72  FXASSERT(res == 0);
73  UNUSED_PARAMETER(res); // only used for assertion
74  myApp->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
75 #else
76  event = CreateEvent(NULL, FALSE, FALSE, NULL);
77  FXASSERT(event != NULL);
78  myApp->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
79 #endif
80 }
81 
82 
84 #ifndef WIN32
85  myApp->removeInput(event[PIPE_READ], INPUT_READ);
86  ::close(event[PIPE_READ]);
87  ::close(event[PIPE_WRITE]);
88  FXFREE(&event);
89 #else
90  myApp->removeInput(event, INPUT_READ);
91  ::CloseHandle(event);
92 #endif
93 }
94 
95 
96 void
98 #ifndef WIN32
99  FXuint seltype = SEL_THREAD;
100  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
101 #else
102  ::SetEvent(event);
103 #endif
104 }
105 
106 
107 void
109  UNUSED_PARAMETER(seltype);
110 #ifndef WIN32
111  ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
112 #else
113  ::SetEvent(event);
114 #endif
115 }
116 
117 
118 long
119 FXSingleEventThread::onThreadSignal(FXObject*, FXSelector, void*) {
120 #ifndef WIN32
121  FXuint seltype = SEL_THREAD;
122  ::read(event[PIPE_READ], &seltype, sizeof(seltype));
123 #else
124  //FIXME need win32 support
125 #endif
126  FXSelector sel = FXSEL(SEL_THREAD, 0);
127  handle(this, sel, 0);
128  return 0;
129 }
130 
131 
132 long
133 FXSingleEventThread::onThreadEvent(FXObject*, FXSelector , void*) {
134  myClient->eventOccured();
135  /*
136  FXuint seltype1 = FXSELTYPE(SEL_THREAD);
137  if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {
138  }
139  FXuint seltype = FXSELTYPE(sel);
140  return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);
141  */
142  return 1;
143 }
144 
145 
146 void
148 #ifdef WIN32
149  Sleep(ms);
150 #else
151  long long us = ms * 1000;
152  usleep(us);
153 #endif
154 }
155 
156 
157 
ID for message passing between threads.
Definition: GUIAppEnum.h:121
FXInputHandle * FXThreadEventHandle
Definition: fxexdefs.h:307
#define PIPE_READ
long onThreadEvent(FXObject *, FXSelector, void *)
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
static void sleep(long ms)
#define PIPE_WRITE
FXDEFMAP(FXBaseObject) FXBaseObjectMap[]
long onThreadSignal(FXObject *, FXSelector, void *)