FXThreadEvent.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00035
00036
00037
00038
00039
00040
00041
00042
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
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
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
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
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
00102
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
00113
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
00123
00124
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
00131 #endif
00132 handle(this,FXSEL(seltype,0),NULL);
00133 return 0;
00134 }
00135
00136
00137
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