00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __mitkOoCIODelegate_h
00011 #define __mitkOoCIODelegate_h
00012
00013 #include "mitkThread.h"
00014 #include "mitkSemaphore.h"
00015
00016 template <class UserClass>
00017 class mitkOoCIODelegate
00018 {
00019 public:
00020 mitkOoCIODelegate(UserClass* UserObject)
00021 {
00022 m_UserObject=UserObject;
00023 m_IOThreadWorking=false;
00024 m_IOStartSema=new mitkSemaphore;
00025 m_IOFinishSema=new mitkSemaphore;
00026 m_IOThread=new MITK_THREAD_MEMBER(mitkOoCIODelegate,this,_IOThreadEntrance);
00027 m_IOThread->Spawn();
00028 m_IOFunction=NULL;
00029 }
00030 virtual ~mitkOoCIODelegate()
00031 {
00032 m_IOThreadWorking=false;
00033 m_IOStartSema->Post();
00034 m_IOFinishSema->Post();
00035 m_IOThread->Wait();
00036 m_IOThread->Delete();
00037 m_IOFinishSema->Delete();
00038 m_IOStartSema->Delete();
00039 }
00040 void IOStart(void (UserClass::*IOFunction)()=NULL)
00041 {
00042 if (m_IOThreadWorking) m_IOFinishSema->Wait();
00043 m_IOFunction=IOFunction;
00044 m_IOThreadWorking=true;
00045 m_IOStartSema->Post();
00046 }
00047 void IOWait()
00048 {
00049 if (m_IOThreadWorking)
00050 {
00051 m_IOFinishSema->Wait();
00052 m_IOThreadWorking=false;
00053 }
00054 }
00055 private:
00056 void _IOThreadEntrance(void *)
00057 {
00058 while(1)
00059 {
00060 m_IOStartSema->Wait();
00061 if (!m_IOThreadWorking) break;
00062 if (m_IOFunction) (m_UserObject->*m_IOFunction)();
00063 m_IOFinishSema->Post();
00064 }
00065
00066 }
00067 UserClass* m_UserObject;
00068 void (UserClass::*m_IOFunction)();
00069 bool m_IOThreadWorking;
00070 mitkSemaphore *m_IOStartSema,*m_IOFinishSema;
00071 mitkThreadDelegate* m_IOThread;
00072 };
00073
00074 #endif
00075