00001 00002 00003 00004 00005 00006 00007 00008 00009 #ifndef __FUNCTION_THREAD_H 00010 #define __FUNCTION_THREAD_H 00011 00012 #include "SingleProducerConsumer.h" 00013 #include "ThreadPool.h" 00014 #include "RakMemoryOverride.h" 00015 #include "Export.h" 00016 00017 namespace RakNet 00018 { 00019 00020 // Forward declarations 00021 class Functor; 00022 00025 class RAK_DLL_EXPORT FunctionThread 00026 { 00027 public: 00028 FunctionThread(); 00029 ~FunctionThread(); 00030 00031 struct FunctorAndContext 00032 { 00033 Functor *functor; 00034 void *context; 00035 }; 00036 00038 void StartThreads(int numThreads); 00039 00043 void StopThreads(bool blockOnCurrentProcessing); 00044 00051 void Push(Functor *functor, void *context=0); 00052 00055 void CallResultHandlers(void); 00056 00060 void CancelFunctorsWithContext(bool (*cancelThisFunctor)(FunctorAndContext func, void *userData), void *userData); 00061 00065 void SetPostResultFunction(void (*postResult)(FunctorAndContext func)); 00066 00067 00068 protected: 00069 void CancelInput(void); 00070 ThreadPool<FunctorAndContext, FunctorAndContext> threadPool; 00071 00072 void (*pr)(FunctorAndContext func); 00073 }; 00074 00077 class Functor 00078 { 00079 public: 00080 Functor() {} 00081 virtual ~Functor() {} 00082 00085 virtual void Process(void *context)=0; 00090 virtual void HandleResult(bool wasCancelled, void *context)=0; 00091 }; 00092 00093 class RAK_DLL_EXPORT FunctionThreadDependentClass 00094 { 00095 public: 00096 FunctionThreadDependentClass(); 00097 virtual ~FunctionThreadDependentClass(); 00098 00101 virtual void AssignFunctionThread(FunctionThread *ft); 00102 00104 FunctionThread *GetFunctionThread(void) const; 00105 00107 bool GetFunctionThreadWasAllocated(void) const; 00108 00112 virtual void PushFunctor(Functor *functor, void *context=0); 00113 protected: 00115 void StartFunctionThread(); 00116 FunctionThread *functionThread; 00117 bool functionThreadWasAllocated; 00118 }; 00119 00120 } 00121 00122 #endif