Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/foxtools/MFXSingleEventThread.cpp
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2004-2025 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
FXIMPLEMENT(MFXSingleEventThread, FXObject, MFXSingleEventThreadMap, ARRAYNUMBER(MFXSingleEventThreadMap))
55
56
57
58
MFXSingleEventThread::MFXSingleEventThread(FXApp* a, MFXInterThreadEventClient* client)
59
: FXObject(), myClient(client) {
60
myApp = (a);
61
#ifndef WIN32
62
FXMALLOC(&event, MFXThreadEventHandle, 2);
63
FXint res = pipe(event);
64
FXASSERT(res == 0);
65
UNUSED_PARAMETER(res); // only used for assertion
66
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
}
73
74
75
MFXSingleEventThread::~MFXSingleEventThread() {
76
#ifndef WIN32
77
myApp->removeInput(event[PIPE_READ], INPUT_READ);
78
::close(event[PIPE_READ]);
79
::close(event[PIPE_WRITE]);
80
FXFREE(&event);
81
#else
82
myApp->removeInput(event, INPUT_READ);
83
::CloseHandle(event);
84
#endif
85
}
86
87
88
void
89
MFXSingleEventThread::signal() {
90
#ifndef WIN32
91
FXuint seltype = SEL_THREAD;
92
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
}
98
99
100
void
101
MFXSingleEventThread::signal(FXuint seltype) {
102
UNUSED_PARAMETER(seltype);
103
#ifndef WIN32
104
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
}
110
111
112
long
113
MFXSingleEventThread::onThreadSignal(FXObject*, FXSelector, void*) {
114
#ifndef WIN32
115
FXuint seltype = SEL_THREAD;
116
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
handle(this, sel, nullptr);
123
return 0;
124
}
125
126
127
long
128
MFXSingleEventThread::onThreadEvent(FXObject*, FXSelector, void*) {
129
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
return 1;
138
}
139
140
141
void
142
MFXSingleEventThread::sleep(long ms) {
143
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
144
}
145
146