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