ReplicaNet and RNLobby  1
ThreadClass.h
1 /* START_LICENSE_HEADER
2 
3 Copyright (C) 2000 Martin Piper, original design and program code
4 Copyright (C) 2001 Replica Software
5 
6 This program file is copyright (C) Replica Software and can only be used under license.
7 For more information visit: http://www.replicanet.com/
8 Or email: info@replicanet.com
9 
10 END_LICENSE_HEADER */
11 #include "RNPlatform/Inc/MemoryTracking.h"
12 #ifndef __THREADCLASS_H__
13 #define __THREADCLASS_H__
14 #if defined(_PS2)
15 #include <list>
16 #endif
17 
18 #if defined(_XBOX)
19 #include <xtl.h> // For XBox builds
20 #else
21 #if defined(_WIN32)
22 #include <windows.h> // For Windows builds
23 #endif
24 #endif
25 
26 #if defined(RN_UNIX_LIKE)
27 #include <pthread.h>
28 #endif
29 
30 namespace RNReplicaNet
31 {
32 
33 class Thread;
34 const unsigned int kTIME_INFINITE = 0xFFFFFFFF;
35 
40 {
41 public:
45  MutexClass();
46 
50  virtual ~MutexClass();
51 
55  void Lock(void);
56 
60  void UnLock(void);
61 
62  enum
63  {
64  kNoOwnerThread = -1
65  };
66 
67 private:
68 #if defined(_PS2)
69  // A binary semaphore created to lock all critical sections inside any mutex class
70  // A mutex per class could be used however the PS2 does not allow many to be created
71  static int mMutex;
72  // The count of the lock, for recursive locks
73  int mCount;
74  // The owner thread of this mutex, kNoOwnerThread means no thread currently owns the mutex and the mCount should then be 0
75  volatile int mOwnerThread;
76  // A linked list of waiting threads for this mutex
77  std::list<int> mWaitingThreadIDs;
78 #else
79  void *mMutex; // A pointer to a mutex/critical section/handle depending on the internal implementation.
80 
81 #if defined(_WIN32)
82  CRITICAL_SECTION mSection; // Local storage
83 #endif
84 
85 #if defined(RN_UNIX_LIKE)
86  pthread_mutex_t mLocalMutex;
87 #endif
88 
89 #endif
90 };
91 
96 class ThreadClass : public MutexClass
97 {
98 public:
102  ThreadClass();
103 
107  virtual ~ThreadClass();
108 
115  void Sleep(int milliseconds);
116 
117 protected:
118 friend class Thread;
123  void DoQuitNow(const int returnCode);
124 
128  virtual int ThreadEntry(void) = 0;
129 
133  virtual void ThreadExiting(const int returnCode);
134 
138  void CheckQuit(void);
139 
140  static size_t GetNumAllocated(void)
141  {
142  return mNumAllocated;
143  }
144 
145 private:
146  void CallThreadExiting(const int returnCode);
147 
148  volatile bool mQuitNow;
149 
150  Thread *mBoundThread;
151  volatile bool mThreadExitingCalled;
152 
153  static volatile size_t mNumAllocated;
154 };
155 
160 {
161 public:
166  LockingMechanism(MutexClass *lockee);
167 
171  virtual ~LockingMechanism();
172 
173 private:
174  MutexClass *mLockee;
175 };
176 
178 {
179 public:
185  static void Sleep(int milliseconds);
186 };
187 
191 #define THREADSAFELOCK() RNReplicaNet::LockingMechanism _lock(this);
192 #define THREADSAFELOCKCLASS(x) RNReplicaNet::LockingMechanism _class_lock(&(x));
193 #define THREADSAFELOCKCLASSp(x) RNReplicaNet::LockingMechanism _class_lockp(x);
194 #define THREADSAFELOCKCLASSNAMED(x) RNReplicaNet::LockingMechanism _class_lock##x (&(x));
195 #define THREADSAFELOCKCLASSNAMEDp(x) RNReplicaNet::LockingMechanism _class_lockp##x (x);
196 
197 } // namespace RNReplicaNet
198 
199 #endif
void Sleep(int milliseconds)
Definition: ThreadClass.h:177
Definition: ThreadClass.h:159
static void Sleep(int milliseconds)
virtual void ThreadExiting(const int returnCode)
virtual int ThreadEntry(void)=0
Definition: ThreadClass.h:39
LockingMechanism(MutexClass *lockee)
void DoQuitNow(const int returnCode)
Definition: ThreadClass.h:96
Definition: Thread.h:29