Main Page | Class Hierarchy | Class List | Directories | File List | Class Members

ThreadClass.h

00001 /* START_LICENSE_HEADER
00002 
00003 Copyright (C) 2000 Martin Piper, original design and program code
00004 Copyright (C) 2001-2005 Replica Software
00005 
00006 This program file is copyright (C) Replica Software and can only be used under license.
00007 For more information visit: http://www.replicanet.com/
00008 Or email: info@replicanet.com
00009 
00010 END_LICENSE_HEADER */
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     // A binary semaphore created to lock all critical sections inside any mutex class
00059     // A mutex per class could be used however the PS2 does not allow many to be created
00060     static int mMutex;
00061     // The count of the lock, for recursive locks
00062     int mCount;
00063     // The owner thread of this mutex, kNoOwnerThread means no thread currently owns the mutex and the mCount should then be 0
00064     volatile int mOwnerThread;
00065     // A linked list of waiting threads for this mutex
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 } // namespace RNReplicaNet
00171 
00172 #endif

Generated on Sun Oct 30 01:12:31 2005 for Platform by  doxygen 1.4.1