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 2001-2010 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 /*
00034 #include <fxdefs.h>
00035 #include <FXString.h>
00036 #include <FXStream.h>
00037 #include <FXSize.h>
00038 #include <FXPoint.h>
00039 #include <FXRectangle.h>
00040 #include <FXRegistry.h>
00041 #include <FXHash.h>
00042 #include <FXApp.h>
00043 */
00044 #ifndef WIN32
00045 #include <unistd.h>
00046 #endif
00047 
00048 using namespace FX;
00049 #include "fxexdefs.h"
00050 #include "FXThreadEvent.h"
00051 
00052 #ifdef CHECK_MEMORY_LEAKS
00053 #include <foreign/nvwa/debug_new.h>
00054 // ===========================================================================
00055 // used namespaces
00056 // ===========================================================================
00057 #endif // _DEBUG
00058 using namespace FXEX;
00059 namespace FXEX {
00060 
00061 #ifndef WIN32
00062 # define PIPE_READ 0
00063 # define PIPE_WRITE 1
00064 #endif
00065 
00066 // Message map
00067 FXDEFMAP(FXThreadEvent) FXThreadEventMap[]={
00068     FXMAPTYPE(0,FXThreadEvent::onThreadEvent),
00069     FXMAPFUNC(SEL_THREAD,0, FXThreadEvent::onThreadEvent),
00070     FXMAPFUNC(SEL_IO_READ,FXThreadEvent::ID_THREAD_EVENT,FXThreadEvent::onThreadSignal),
00071 };
00072 FXIMPLEMENT(FXThreadEvent,FXBaseObject,FXThreadEventMap,ARRAYNUMBER(FXThreadEventMap))
00073 
00074 // FXThreadEvent : Constructor
00075 FXThreadEvent::FXThreadEvent(FXObject* tgt,FXSelector sel) : FXBaseObject(tgt,sel) {
00076 #ifndef WIN32
00077     FXMALLOC(&event,FXThreadEventHandle,2);
00078     FXint res = pipe(event);
00079     FXASSERT(res == 0);
00080     getApp()->addInput(event[PIPE_READ],INPUT_READ,this,ID_THREAD_EVENT);
00081 #else
00082     event=CreateEvent(NULL,FALSE,FALSE,NULL);
00083     FXASSERT(event != NULL);
00084     getApp()->addInput(event,INPUT_READ,this, ID_THREAD_EVENT);
00085 #endif
00086 }
00087 
00088 // ~FXThreadEvent : Destructor
00089 FXThreadEvent::~FXThreadEvent() {
00090 #ifndef WIN32
00091     getApp()->removeInput(event[PIPE_READ],INPUT_READ);
00092     ::close(event[PIPE_READ]);
00093     ::close(event[PIPE_WRITE]);
00094     FXFREE(&event);
00095 #else
00096     getApp()->removeInput(event,INPUT_READ);
00097     ::CloseHandle(event);
00098 #endif
00099 }
00100 
00101 // signal the target using the SEL_THREAD seltype
00102 // this method is meant to be called from the worker thread
00103 void FXThreadEvent::signal() {
00104     FXuint seltype=SEL_THREAD;
00105 #ifndef WIN32
00106     ::write(event[PIPE_WRITE],&seltype,sizeof(seltype));
00107 #else
00108     ::SetEvent(event);
00109 #endif
00110 }
00111 
00112 // signal the target using some seltype
00113 // this method is meant to be called from the worker thread
00114 void FXThreadEvent::signal(FXuint seltype) {
00115 #ifndef WIN32
00116     ::write(event[PIPE_WRITE],&seltype,sizeof(seltype));
00117 #else
00118     ::SetEvent(event);
00119 #endif
00120 }
00121 
00122 // this thread is signalled via the IO/event, from other thread.
00123 // We also figure out what SEL_type to generate.
00124 // We forward it to ourselves first, to allow child classes to handle the event.
00125 long FXThreadEvent::onThreadSignal(FXObject*,FXSelector,void*) {
00126     FXuint seltype=SEL_THREAD;
00127 #ifndef WIN32
00128     ::read(event[PIPE_READ],&seltype,sizeof(seltype));
00129 #else
00130     //FIXME need win32 support
00131 #endif
00132     handle(this,FXSEL(seltype,0),NULL);
00133     return 0;
00134 }
00135 
00136 // forward thread event to application - we generate the appropriate FOX event
00137 // which is now in the main thread (ie no longer in the worker thread)
00138 long FXThreadEvent::onThreadEvent(FXObject*,FXSelector sel,void*) {
00139     FXuint seltype = FXSELTYPE(sel);
00140     return target && target->handle(this,FXSEL(seltype,message),NULL);
00141 }
00142 
00143 }
00144 
00145 
00146 
00147 /****************************************************************************/
00148 

Generated on Wed May 5 00:06:29 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6