Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2004-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MFXSingleEventThread.cpp
15 : /// @author unknown_author
16 : /// @author Daniel Krajzewicz
17 : /// @author Michael Behrisch
18 : /// @author Laura Bieker
19 : /// @author Jakob Erdmann
20 : /// @date 2004-03-19
21 : ///
22 : //
23 : /****************************************************************************/
24 :
25 : /* =========================================================================
26 : * included modules
27 : * ======================================================================= */
28 : #include <config.h>
29 :
30 : #include <utils/common/StdDefs.h>
31 : #include "MFXInterThreadEventClient.h"
32 : #include "MFXSingleEventThread.h"
33 : #ifndef WIN32
34 : #include <pthread.h>
35 : #include <stdlib.h>
36 : #include <unistd.h>
37 : #else
38 : #include <process.h>
39 : #endif
40 : #include <thread>
41 :
42 : #ifndef WIN32
43 : # define PIPE_READ 0
44 : # define PIPE_WRITE 1
45 : #endif
46 :
47 : using namespace FXEX;
48 :
49 : // Message map
50 : FXDEFMAP(MFXSingleEventThread) MFXSingleEventThreadMap[] = {
51 : FXMAPFUNC(SEL_IO_READ, MFXSingleEventThread::ID_THREAD_EVENT, MFXSingleEventThread::onThreadSignal),
52 : FXMAPFUNC(SEL_THREAD, 0, MFXSingleEventThread::onThreadEvent),
53 : };
54 0 : FXIMPLEMENT(MFXSingleEventThread, FXObject, MFXSingleEventThreadMap, ARRAYNUMBER(MFXSingleEventThreadMap))
55 :
56 :
57 :
58 15102 : MFXSingleEventThread::MFXSingleEventThread(FXApp* a, MFXInterThreadEventClient* client)
59 15102 : : FXObject(), myClient(client) {
60 15102 : myApp = (a);
61 : #ifndef WIN32
62 15102 : FXMALLOC(&event, MFXThreadEventHandle, 2);
63 15102 : FXint res = pipe(event);
64 : FXASSERT(res == 0);
65 : UNUSED_PARAMETER(res); // only used for assertion
66 15102 : myApp->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
67 : #else
68 : event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
69 : FXASSERT(event != NULL);
70 : myApp->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
71 : #endif
72 15102 : }
73 :
74 :
75 15076 : MFXSingleEventThread::~MFXSingleEventThread() {
76 : #ifndef WIN32
77 15076 : myApp->removeInput(event[PIPE_READ], INPUT_READ);
78 15076 : ::close(event[PIPE_READ]);
79 15076 : ::close(event[PIPE_WRITE]);
80 15076 : FXFREE(&event);
81 : #else
82 : myApp->removeInput(event, INPUT_READ);
83 : ::CloseHandle(event);
84 : #endif
85 15076 : }
86 :
87 :
88 : void
89 0 : MFXSingleEventThread::signal() {
90 : #ifndef WIN32
91 0 : FXuint seltype = SEL_THREAD;
92 0 : FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
93 : UNUSED_PARAMETER(res); // to make the compiler happy
94 : #else
95 : ::SetEvent(event);
96 : #endif
97 0 : }
98 :
99 :
100 : void
101 0 : MFXSingleEventThread::signal(FXuint seltype) {
102 : UNUSED_PARAMETER(seltype);
103 : #ifndef WIN32
104 0 : FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
105 : UNUSED_PARAMETER(res); // to make the compiler happy
106 : #else
107 : ::SetEvent(event);
108 : #endif
109 0 : }
110 :
111 :
112 : long
113 0 : MFXSingleEventThread::onThreadSignal(FXObject*, FXSelector, void*) {
114 : #ifndef WIN32
115 0 : FXuint seltype = SEL_THREAD;
116 0 : FXint res = ::read(event[PIPE_READ], &seltype, sizeof(seltype));
117 : UNUSED_PARAMETER(res); // to make the compiler happy
118 : #else
119 : //FIXME need win32 support
120 : #endif
121 : FXSelector sel = FXSEL(SEL_THREAD, 0);
122 0 : handle(this, sel, nullptr);
123 0 : return 0;
124 : }
125 :
126 :
127 : long
128 0 : MFXSingleEventThread::onThreadEvent(FXObject*, FXSelector, void*) {
129 0 : myClient->eventOccurred();
130 : /*
131 : FXuint seltype1 = FXSELTYPE(SEL_THREAD);
132 : if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {
133 : }
134 : FXuint seltype = FXSELTYPE(sel);
135 : return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);
136 : */
137 0 : return 1;
138 : }
139 :
140 :
141 : void
142 64930 : MFXSingleEventThread::sleep(long ms) {
143 64930 : std::this_thread::sleep_for(std::chrono::milliseconds(ms));
144 64930 : }
|