Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MFXThreadEvent.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2004-2026 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/****************************************************************************/
20//
21/****************************************************************************/
22#include <config.h>
23
24#include "fxheader.h"
26/*
27#include <fxdefs.h>
28#include <FXString.h>
29#include <FXStream.h>
30#include <FXSize.h>
31#include <FXPoint.h>
32#include <FXRectangle.h>
33#include <FXRegistry.h>
34#include <FXHash.h>
35#include <FXApp.h>
36*/
37#ifndef WIN32
38#include <unistd.h>
39#endif
40
41using namespace FX;
42#include "MFXThreadEvent.h"
43
44// ===========================================================================
45// used namespaces
46// ===========================================================================
47using namespace FXEX;
48namespace FXEX {
49
50#ifndef WIN32
51# define PIPE_READ 0
52# define PIPE_WRITE 1
53#endif
54
55// Message map
56FXDEFMAP(MFXThreadEvent) MFXThreadEventMap[] = {
60};
61FXIMPLEMENT(MFXThreadEvent, MFXBaseObject, MFXThreadEventMap, ARRAYNUMBER(MFXThreadEventMap))
62
63// MFXThreadEvent : Constructor
64MFXThreadEvent::MFXThreadEvent(FXObject* tgt, FXSelector sel) : MFXBaseObject(tgt, sel) {
65#ifndef WIN32
66 FXMALLOC(&event, MFXThreadEventHandle, 2);
67 FXint res = pipe(event);
68 FXASSERT(res == 0);
69 UNUSED_PARAMETER(res); // only used for assertion
70 getApp()->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
71#else
72 event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
73 FXASSERT(event != NULL);
74 getApp()->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
75#endif
76}
77
78// ~MFXThreadEvent : Destructor
80#ifndef WIN32
81 getApp()->removeInput(event[PIPE_READ], INPUT_READ);
82 ::close(event[PIPE_READ]);
83 ::close(event[PIPE_WRITE]);
84 FXFREE(&event);
85#else
86 getApp()->removeInput(event, INPUT_READ);
87 ::CloseHandle(event);
88#endif
89}
90
91// signal the target using the SEL_THREAD seltype
92// this method is meant to be called from the worker thread
94#ifndef WIN32
95 FXuint seltype = SEL_THREAD;
96 FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
97 UNUSED_PARAMETER(res); // to make the compiler happy
98#else
99 ::SetEvent(event);
100#endif
101}
102
103// signal the target using some seltype
104// this method is meant to be called from the worker thread
105void MFXThreadEvent::signal(FXuint seltype) {
106#ifndef WIN32
107 FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
108 UNUSED_PARAMETER(res); // to make the compiler happy
109#else
110 UNUSED_PARAMETER(seltype);
111 ::SetEvent(event);
112#endif
113}
114
115// this thread is signalled via the IO/event, from other thread.
116// We also figure out what SEL_type to generate.
117// We forward it to ourselves first, to allow child classes to handle the event.
118long MFXThreadEvent::onThreadSignal(FXObject*, FXSelector, void*) {
119 FXuint seltype = SEL_THREAD;
120#ifndef WIN32
121 FXint res = ::read(event[PIPE_READ], &seltype, sizeof(seltype));
122 UNUSED_PARAMETER(res); // to make the compiler happy
123#else
124 //FIXME need win32 support
125#endif
126 handle(this, FXSEL(seltype, 0), nullptr);
127 return 0;
128}
129
130// forward thread event to application - we generate the appropriate FOX event
131// which is now in the main thread (ie no longer in the worker thread)
132long MFXThreadEvent::onThreadEvent(FXObject*, FXSelector sel, void*) {
133 FXuint seltype = FXSELTYPE(sel);
134 return target && target->handle(this, FXSEL(seltype, message), nullptr);
135}
136
137}
138
139
140/****************************************************************************/
@ ID_THREAD_EVENT
ID for message passing between threads.
Definition GUIAppEnum.h:352
#define PIPE_READ
#define PIPE_WRITE
FXApp * getApp()
application pointer
long onThreadEvent(FXObject *, FXSelector, void *)
long onThreadSignal(FXObject *, FXSelector, void *)
virtual ~MFXThreadEvent()
destructor
MFXThreadEventHandle event
#define UNUSED_PARAMETER(x)
@ SEL_THREAD
Definition fxexdefs.h:153
FXInputHandle * MFXThreadEventHandle
Definition fxexdefs.h:282
FXDEFMAP(MFXBaseObject) MFXBaseObjectMap[]
Definition GUI.h:31