Path: blob/main/src/utils/foxtools/MFXSingleEventThread.cpp
169678 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2004-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file MFXSingleEventThread.cpp14/// @author unknown_author15/// @author Daniel Krajzewicz16/// @author Michael Behrisch17/// @author Laura Bieker18/// @author Jakob Erdmann19/// @date 2004-03-1920///21//22/****************************************************************************/2324/* =========================================================================25* included modules26* ======================================================================= */27#include <config.h>2829#include <utils/common/StdDefs.h>30#include "MFXInterThreadEventClient.h"31#include "MFXSingleEventThread.h"32#ifndef WIN3233#include <pthread.h>34#include <stdlib.h>35#include <unistd.h>36#else37#include <process.h>38#endif39#include <thread>4041#ifndef WIN3242# define PIPE_READ 043# define PIPE_WRITE 144#endif4546using namespace FXEX;4748// Message map49FXDEFMAP(MFXSingleEventThread) MFXSingleEventThreadMap[] = {50FXMAPFUNC(SEL_IO_READ, MFXSingleEventThread::ID_THREAD_EVENT, MFXSingleEventThread::onThreadSignal),51FXMAPFUNC(SEL_THREAD, 0, MFXSingleEventThread::onThreadEvent),52};53FXIMPLEMENT(MFXSingleEventThread, FXObject, MFXSingleEventThreadMap, ARRAYNUMBER(MFXSingleEventThreadMap))54555657MFXSingleEventThread::MFXSingleEventThread(FXApp* a, MFXInterThreadEventClient* client)58: FXObject(), myClient(client) {59myApp = (a);60#ifndef WIN3261FXMALLOC(&event, MFXThreadEventHandle, 2);62FXint res = pipe(event);63FXASSERT(res == 0);64UNUSED_PARAMETER(res); // only used for assertion65myApp->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);66#else67event = CreateEvent(nullptr, FALSE, FALSE, nullptr);68FXASSERT(event != NULL);69myApp->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);70#endif71}727374MFXSingleEventThread::~MFXSingleEventThread() {75#ifndef WIN3276myApp->removeInput(event[PIPE_READ], INPUT_READ);77::close(event[PIPE_READ]);78::close(event[PIPE_WRITE]);79FXFREE(&event);80#else81myApp->removeInput(event, INPUT_READ);82::CloseHandle(event);83#endif84}858687void88MFXSingleEventThread::signal() {89#ifndef WIN3290FXuint seltype = SEL_THREAD;91FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));92UNUSED_PARAMETER(res); // to make the compiler happy93#else94::SetEvent(event);95#endif96}979899void100MFXSingleEventThread::signal(FXuint seltype) {101UNUSED_PARAMETER(seltype);102#ifndef WIN32103FXint res = ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));104UNUSED_PARAMETER(res); // to make the compiler happy105#else106::SetEvent(event);107#endif108}109110111long112MFXSingleEventThread::onThreadSignal(FXObject*, FXSelector, void*) {113#ifndef WIN32114FXuint seltype = SEL_THREAD;115FXint res = ::read(event[PIPE_READ], &seltype, sizeof(seltype));116UNUSED_PARAMETER(res); // to make the compiler happy117#else118//FIXME need win32 support119#endif120FXSelector sel = FXSEL(SEL_THREAD, 0);121handle(this, sel, nullptr);122return 0;123}124125126long127MFXSingleEventThread::onThreadEvent(FXObject*, FXSelector, void*) {128myClient->eventOccurred();129/*130FXuint seltype1 = FXSELTYPE(SEL_THREAD);131if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {132}133FXuint seltype = FXSELTYPE(sel);134return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);135*/136return 1;137}138139140void141MFXSingleEventThread::sleep(long ms) {142std::this_thread::sleep_for(std::chrono::milliseconds(ms));143}144145146