00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __THREADCLASS_H__
00012 #define __THREADCLASS_H__
00013 #include "RNPlatform/Inc/DLLExportAPI.h"
00014
00015 #if defined(_PS2)
00016 #include <list>
00017 #endif
00018
00019 namespace RNReplicaNet
00020 {
00021
00022 class Thread;
00023 const unsigned int kTIME_INFINITE = 0xFFFFFFFF;
00024
00028 class REPNETEXPORTAPI MutexClass
00029 {
00030 public:
00034 MutexClass();
00035
00039 virtual ~MutexClass();
00040
00044 void Lock(void);
00045
00049 void UnLock(void);
00050
00051 enum
00052 {
00053 kNoOwnerThread = -1
00054 };
00055
00056 private:
00057 #if defined(_PS2)
00058
00059
00060 static int mMutex;
00061
00062 int mCount;
00063
00064 volatile int mOwnerThread;
00065
00066 std::list<int> mWaitingThreadIDs;
00067 #else
00068 void *mMutex;
00069 #endif
00070 };
00071
00076 class REPNETEXPORTAPI ThreadClass : public MutexClass
00077 {
00078 public:
00082 ThreadClass();
00083
00087 virtual ~ThreadClass();
00088
00094 void Sleep(int milliseconds);
00095
00096 protected:
00101 void DoQuitNow(const int returnCode);
00102
00103 private:
00104
00105 friend class Thread;
00106 virtual int ThreadEntry(void) = 0;
00107
00108 volatile bool mQuitNow;
00109
00110 void CheckQuit(void);
00111 Thread *mBoundThread;
00112 };
00113
00117 class REPNETEXPORTAPI LockingMechanism
00118 {
00119 public:
00124 LockingMechanism(MutexClass *lockee) : mLockee(0)
00125 {
00126 if (lockee)
00127 {
00128 lockee->Lock();
00129 }
00130 mLockee = lockee;
00131 }
00132
00136 virtual ~LockingMechanism()
00137 {
00138 if (mLockee)
00139 {
00140 MutexClass *tl = mLockee;
00141 mLockee = 0;
00142 tl->UnLock();
00143 }
00144 }
00145
00146 private:
00147 MutexClass *mLockee;
00148 };
00149
00150 class REPNETEXPORTAPI CurrentThread
00151 {
00152 public:
00158 static void Sleep(int milliseconds);
00159 };
00160
00164 #define THREADSAFELOCK() RNReplicaNet::LockingMechanism _lock(this);
00165 #define THREADSAFELOCKCLASS(x) RNReplicaNet::LockingMechanism _class_lock(&(x));
00166 #define THREADSAFELOCKCLASSp(x) RNReplicaNet::LockingMechanism _class_lockp(x);
00167 #define THREADSAFELOCKCLASSNAMED(x) RNReplicaNet::LockingMechanism _class_lock##x (&(x));
00168 #define THREADSAFELOCKCLASSNAMEDp(x) RNReplicaNet::LockingMechanism _class_lockp##x (x);
00169
00170 }
00171
00172 #endif