SUMO - Simulation of Urban MObility
FXThreadEvent.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00007 //
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This file is part of SUMO.
00014 //   SUMO is free software: you can redistribute it and/or modify
00015 //   it under the terms of the GNU General Public License as published by
00016 //   the Free Software Foundation, either version 3 of the License, or
00017 //   (at your option) any later version.
00018 //
00019 /****************************************************************************/
00020 
00021 
00022 // ===========================================================================
00023 // included modules
00024 // ===========================================================================
00025 #ifdef _MSC_VER
00026 #include <windows_config.h>
00027 #else
00028 #include <config.h>
00029 #endif
00030 
00031 #include <fxver.h>
00032 #include <xincs.h>
00033 #include <fx.h>
00034 #include <utils/common/StdDefs.h>
00035 /*
00036 #include <fxdefs.h>
00037 #include <FXString.h>
00038 #include <FXStream.h>
00039 #include <FXSize.h>
00040 #include <FXPoint.h>
00041 #include <FXRectangle.h>
00042 #include <FXRegistry.h>
00043 #include <FXHash.h>
00044 #include <FXApp.h>
00045 */
00046 #ifndef WIN32
00047 #include <unistd.h>
00048 #endif
00049 
00050 using namespace FX;
00051 #include "fxexdefs.h"
00052 #include "FXThreadEvent.h"
00053 
00054 #ifdef CHECK_MEMORY_LEAKS
00055 #include <foreign/nvwa/debug_new.h>
00056 // ===========================================================================
00057 // used namespaces
00058 // ===========================================================================
00059 #endif // _DEBUG
00060 using namespace FXEX;
00061 namespace FXEX {
00062 
00063 #ifndef WIN32
00064 # define PIPE_READ 0
00065 # define PIPE_WRITE 1
00066 #endif
00067 
00068 // Message map
00069 FXDEFMAP(FXThreadEvent) FXThreadEventMap[] = {
00070     FXMAPTYPE(0, FXThreadEvent::onThreadEvent),
00071     FXMAPFUNC(SEL_THREAD, 0, FXThreadEvent::onThreadEvent),
00072     FXMAPFUNC(SEL_IO_READ, FXThreadEvent::ID_THREAD_EVENT, FXThreadEvent::onThreadSignal),
00073 };
00074 FXIMPLEMENT(FXThreadEvent, FXBaseObject, FXThreadEventMap, ARRAYNUMBER(FXThreadEventMap))
00075 
00076 // FXThreadEvent : Constructor
00077 FXThreadEvent::FXThreadEvent(FXObject* tgt, FXSelector sel) : FXBaseObject(tgt, sel) {
00078 #ifndef WIN32
00079     FXMALLOC(&event, FXThreadEventHandle, 2);
00080     FXint res = pipe(event);
00081     FXASSERT(res == 0);
00082     getApp()->addInput(event[PIPE_READ], INPUT_READ, this, ID_THREAD_EVENT);
00083 #else
00084     event = CreateEvent(NULL, FALSE, FALSE, NULL);
00085     FXASSERT(event != NULL);
00086     getApp()->addInput(event, INPUT_READ, this, ID_THREAD_EVENT);
00087 #endif
00088 }
00089 
00090 // ~FXThreadEvent : Destructor
00091 FXThreadEvent::~FXThreadEvent() {
00092 #ifndef WIN32
00093     getApp()->removeInput(event[PIPE_READ], INPUT_READ);
00094     ::close(event[PIPE_READ]);
00095     ::close(event[PIPE_WRITE]);
00096     FXFREE(&event);
00097 #else
00098     getApp()->removeInput(event, INPUT_READ);
00099     ::CloseHandle(event);
00100 #endif
00101 }
00102 
00103 // signal the target using the SEL_THREAD seltype
00104 // this method is meant to be called from the worker thread
00105 void FXThreadEvent::signal() {
00106     FXuint seltype = SEL_THREAD;
00107 #ifndef WIN32
00108     ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
00109 #else
00110     ::SetEvent(event);
00111 #endif
00112 }
00113 
00114 // signal the target using some seltype
00115 // this method is meant to be called from the worker thread
00116 void FXThreadEvent::signal(FXuint seltype) {
00117 #ifndef WIN32
00118     ::write(event[PIPE_WRITE], &seltype, sizeof(seltype));
00119 #else
00120     UNUSED_PARAMETER(seltype);
00121     ::SetEvent(event);
00122 #endif
00123 }
00124 
00125 // this thread is signalled via the IO/event, from other thread.
00126 // We also figure out what SEL_type to generate.
00127 // We forward it to ourselves first, to allow child classes to handle the event.
00128 long FXThreadEvent::onThreadSignal(FXObject*, FXSelector, void*) {
00129     FXuint seltype = SEL_THREAD;
00130 #ifndef WIN32
00131     ::read(event[PIPE_READ], &seltype, sizeof(seltype));
00132 #else
00133     //FIXME need win32 support
00134 #endif
00135     handle(this, FXSEL(seltype, 0), NULL);
00136     return 0;
00137 }
00138 
00139 // forward thread event to application - we generate the appropriate FOX event
00140 // which is now in the main thread (ie no longer in the worker thread)
00141 long FXThreadEvent::onThreadEvent(FXObject*, FXSelector sel, void*) {
00142     FXuint seltype = FXSELTYPE(sel);
00143     return target && target->handle(this, FXSEL(seltype, message), NULL);
00144 }
00145 
00146 }
00147 
00148 
00149 
00150 /****************************************************************************/
00151 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines