00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #ifndef ThreadManagerH
00048 #define ThreadManagerH
00049
00050 #include <zeusbase/System/Map.hpp>
00051 #include <zeusbase/System/ZObject.h>
00052 #include <zeusbase/System/Interfaces/IThreadManager.hpp>
00053 #include <zeusbase/System/SynchronizeObject.h>
00054
00055
00056 BEGIN_NAMESPACE_Zeus
00057
00058 class TCriticalSection;
00059 class IThreadMessageQueue;
00060
00061 #define ThreadManager TThreadManager::getInstance()
00062
00063
00066
00067 zeusbase_class TThreadManager : public TZObject, public IThreadManager
00068 {
00069 public:
00070 void setDelegationInterface(IThreadManager& rInstance);
00071 void releaseDelegationInterface();
00072 static TThreadManager& getInstance();
00073
00074
00075 virtual void MQUALIFIER addThread(Uint uiID, IThreadMessageQueue& rQueue, bool bInterrupted);
00076 virtual Uint MQUALIFIER getCurrentThreadID() const;
00077 virtual Retval MQUALIFIER getMessageQueue(Uint uiThreadID, IThreadMessageQueue*& rpQueue);
00078 virtual bool MQUALIFIER isThreadInterrupted(Uint iID)const;
00079 virtual void MQUALIFIER removeThread(Uint uiID);
00080 virtual void MQUALIFIER setThreadState(Uint iID, bool bInterrupted);
00081 virtual Uint MQUALIFIER getMainThreadID() const;
00082 virtual Retval MQUALIFIER postObject(Uint uiThreadID,
00083 ISynchronizeObject& rObject,
00084 bool bWaitForCompletion);
00085 virtual Retval MQUALIFIER postObjectToMainThread(ISynchronizeObject& rObject,
00086 bool bWaitForCompletion);
00087 virtual bool MQUALIFIER isCurrentThread(Uint uiThreadID) const;
00088 virtual bool MQUALIFIER isCurrentMainThread() const;
00089
00090
00091 MEMORY_MANAGER_DECL
00092
00093 protected:
00094
00096
00097 #ifdef _MSC_VER
00098 public:
00099 #endif
00101
00102 TThreadManager();
00103 virtual ~TThreadManager();
00104
00105 private:
00107 static TAutoPtr<TThreadManager> m_ptrInstance;
00109 IThreadManager* m_pDelegation;
00111 TMap<Uint, bool> m_mapThreads;
00113 TMap<Uint, IThreadMessageQueue*> m_mapQueues;
00115 TCriticalSection& m_rMapLock;
00117 TCriticalSection& m_rDelegationLock;
00119 Uint m_uiMainThreadID;
00120
00121 };
00122
00123
00124
00125
00128
00129 inline TThreadManager& TThreadManager::getInstance()
00130 {
00131 #if defined(ENABLE_WIN32_BINDING)
00132 if (m_ptrInstance == NULL)
00133 {
00134 m_ptrInstance = new TThreadManager();
00135 }
00136 #endif
00137 return *m_ptrInstance;
00138 }
00139
00140
00143
00144 inline Retval MQUALIFIER TThreadManager::postObjectToMainThread(ISynchronizeObject& rObject,
00145 bool bWaitForCompletion)
00146 {
00147 return postObject(getMainThreadID(), rObject, bWaitForCompletion);
00148 }
00149
00150
00153
00154 inline bool MQUALIFIER TThreadManager::isCurrentThread(Uint uiThreadID) const
00155 {
00156 return (this->getCurrentThreadID() == uiThreadID);
00157 }
00158
00159
00162
00163 inline bool MQUALIFIER TThreadManager::isCurrentMainThread() const
00164 {
00165 return (this->getCurrentThreadID() == this->getMainThreadID());
00166 }
00167
00168 END_NAMESPACE_Zeus
00169
00170
00171
00172
00173
00174 #if defined(ENABLE_LINUX_BINDING) || defined(_MSC_VER)
00175 #include <zeusbase/System/SynchronizeObject.h>
00176 #include <zeusbase/System/Platforms/SynchronizeObjectDelegater.hpp>
00177
00178
00184
00185 #define SYNCHRONIZE_CLASS(clss, mMethod) \
00186 class TMethodCaller_##clss_##mMethod \
00187 : public NAMESPACE_Zeus::TSynchronizeObject\
00188 { \
00189 public: \
00190 TMethodCaller_##clss_##mMethod(clss& rT)\
00191 { \
00192 m_pT = &rT; \
00193 } \
00194 \
00195 void checkForError() \
00196 { \
00197 m_rLock.enter(); \
00198 if (this->hasError()) \
00199 { \
00200 m_pT = NULL; \
00201 } \
00202 m_rLock.leave(); \
00203 } \
00204 virtual void MQUALIFIER process() \
00205 { \
00206 m_rLock.enter(); \
00207 if (m_pT != NULL) \
00208 { \
00209 m_pT->mMethod(); \
00210 } \
00211 m_rLock.leave(); \
00212 } \
00213 \
00214 private: \
00215 clss* m_pT; \
00216 }; \
00217
00218
00225
00226 #define SYNCHRONIZE_METHOD(uiThreadID, clss, mMethod) \
00227 { \
00228 SYNCHRONIZE_CLASS(clss, mMethod); \
00229 \
00230 TAutoPtr<TMethodCaller_##clss_##mMethod> ptrObject = new TMethodCaller_##clss_##mMethod(*this); \
00231 ThreadManager.postObject(uiThreadID, *ptrObject, true); \
00232 ptrObject->checkForError(); \
00233 }
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00251
00252 #define SYNCHRONIZE_MT_METHOD(clss, mMethod) \
00253 { \
00254 SYNCHRONIZE_CLASS(clss, mMethod); \
00255 \
00256 TAutoPtr<TMethodCaller_##clss_##mMethod> ptrObject = new TMethodCaller_##clss_##mMethod(*this); \
00257 ThreadManager.postObjectToMainThread(*ptrObject, true); \
00258 ptrObject->checkForError(); \
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00279
00280 #define POST_METHOD(uiThreadID, class, mMethod) \
00281 { \
00282 SYNCHRONIZE_CLASS(clss, mMethod); \
00283 \
00284 TAutoPtr<TMethodCaller_##clss_##mMethod> ptrObject = new TMethodCaller_##clss_##mMethod(*this); \
00285 ThreadManager.postObject(uiThreadID, *ptrObject, false); \
00286 }
00287
00288
00289
00296
00297 #define POST_MT_METHOD(class, mMethod) \
00298 { \
00299 SYNCHRONIZE_CLASS(clss, mMethod); \
00300 \
00301 TAutoPtr<TMethodCaller_##clss_##mMethod> ptrObject = new TMethodCaller_##clss_##mMethod(*this); \
00302 ThreadManager.postObjectToMainThread(*ptrObject, false); \
00303 }
00304
00305
00306
00307
00308 #elif defined(__BCPLUSPLUS__)
00309 #include <zeusbase/System/Platforms/BorlandSynchronizeObject.hpp>
00310
00311
00318
00319 #define SYNCHRONIZE_METHOD(uiThreadID, clss, mMethod) \
00320 { \
00321 TAutoPtr<TBorlandSynchronizeObject> ptrObject = new TBorlandSynchronizeObject(&mMethod); \
00322 ThreadManager.postObject(uiThreadID, *ptrObject, true); \
00323 ptrObject->checkForError(); \
00324 }
00325
00326
00333
00334 #define SYNCHRONIZE_MT_METHOD(clss, mMethod) \
00335 { \
00336 TAutoPtr<TBorlandSynchronizeObject> ptrObject = new TBorlandSynchronizeObject(&mMethod); \
00337 ThreadManager.postObjectToMainThread(*ptrObject, true); \
00338 ptrObject->checkForError(); \
00339 }
00340
00341
00342
00350
00351 #define POST_METHOD(uiThreadID, class, mMethod) \
00352 { \
00353 TAutoPtr<TBorlandSynchronizeObject> ptrObject = new TBorlandSynchronizeObject(&mMethod); \
00354 ThreadManager.postObject(uiThreadID, *ptrObject, false); \
00355 }
00356
00357
00358
00365
00366 #define POST_MT_METHOD(class, mMethod) \
00367 { \
00368 TAutoPtr<TBorlandSynchronizeObject> ptrObject = new TBorlandSynchronizeObject(&mMethod); \
00369 ThreadManager.postObjectToMainThread(*ptrObject, false); \
00370 }
00371
00372 #endif
00373
00374
00375 #endif