csutil/threading/thread.h
00001 /* 00002 Copyright (C) 2006 by Marten Svanfeldt 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Lesser General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_CSUTIL_THREADING_THREAD_H__ 00020 #define __CS_CSUTIL_THREADING_THREAD_H__ 00021 00022 #include "csutil/noncopyable.h" 00023 #include "csutil/refcount.h" 00024 #include "csutil/refarr.h" 00025 00026 #include "csutil/threading/mutex.h" 00027 00028 namespace CS 00029 { 00030 namespace Threading 00031 { 00038 enum ThreadPriority 00039 { 00041 THREAD_PRIO_LOW = 0, 00042 00044 THREAD_PRIO_NORMAL = 1, 00045 00050 THREAD_PRIO_HIGH = 2 00051 }; 00052 00058 class Runnable : public csRefCount, private CS::NonCopyable 00059 { 00060 public: 00066 virtual void Run () = 0; 00067 00071 virtual const char* GetName () const 00072 { 00073 return "Unnamed thread"; 00074 } 00075 }; 00076 00078 typedef uintptr_t ThreadID; 00079 00080 } 00081 } 00082 00083 // Include implementation specific versions 00084 #if defined(CS_PLATFORM_WIN32) 00085 # include "csutil/threading/win32_thread.h" 00086 #elif defined(CS_PLATFORM_UNIX) || \ 00087 defined(CS_PLATFORM_MACOSX) 00088 # include "csutil/threading/pthread_thread.h" 00089 #else 00090 #error "No threading implementation for your platform" 00091 #endif 00092 00093 00094 namespace CS 00095 { 00096 namespace Threading 00097 { 00098 00099 00104 class Thread : private CS::NonCopyable, 00105 private Implementation::ThreadBase 00106 { 00107 public: 00108 using ThreadBase::IncRef; 00109 using ThreadBase::DecRef; 00110 using ThreadBase::GetRefCount; 00111 00118 Thread (Runnable* runnable, bool start = false) 00119 : ThreadBase (runnable) 00120 { 00121 if (start) 00122 Start (); 00123 } 00124 00130 Thread (Runnable* runnable, ThreadPriority prio) 00131 : ThreadBase (runnable) 00132 { 00133 SetPriority (prio); 00134 } 00135 00143 Thread (Runnable* runnable, bool start, ThreadPriority prio) 00144 : ThreadBase (runnable) 00145 { 00146 SetPriority (prio); 00147 00148 if (start) 00149 Start (); 00150 } 00151 00152 ~Thread () 00153 { 00154 if (IsRunning ()) 00155 Stop (); 00156 } 00157 00161 void Start () 00162 { 00163 ThreadBase::Start (); 00164 } 00165 00174 void Stop () 00175 { 00176 ThreadBase::Stop (); 00177 } 00178 00182 bool IsRunning () const 00183 { 00184 return ThreadBase::IsRunning (); 00185 } 00186 00194 bool SetPriority (ThreadPriority prio) 00195 { 00196 return ThreadBase::SetPriority (prio); 00197 } 00198 00202 ThreadPriority GetPriority () const 00203 { 00204 return ThreadBase::GetPriority (); 00205 } 00206 00211 void Wait () const 00212 { 00213 ThreadBase::Wait (); 00214 } 00215 00222 static void Yield () 00223 { 00224 ThreadBase::Yield (); 00225 } 00226 00232 static ThreadID GetThreadID () 00233 { 00234 return ThreadBase::GetThreadID (); 00235 } 00236 }; 00237 00238 00242 class ThreadGroup : private CS::NonCopyable 00243 { 00244 public: 00245 00251 void Add (Thread* thread) 00252 { 00253 ScopedLock<Mutex> lock (mutex); 00254 allThreads.PushSmart (thread); 00255 } 00256 00260 void Remove (Thread* thread) 00261 { 00262 ScopedLock<Mutex> lock (mutex); 00263 allThreads.Delete (thread); 00264 } 00265 00269 Thread* GetThread (size_t index) const 00270 { 00271 ScopedLock<Mutex> lock (mutex); 00272 return allThreads.Get (index); 00273 } 00274 00278 size_t GetSize () const 00279 { 00280 return allThreads.GetSize (); 00281 } 00282 00287 void StartAll () 00288 { 00289 ScopedLock<Mutex> lock (mutex); 00290 00291 for (size_t i = 0; i < allThreads.GetSize (); ++i) 00292 { 00293 allThreads[i]->Start (); 00294 } 00295 } 00296 00301 void StopAll () 00302 { 00303 ScopedLock<Mutex> lock (mutex); 00304 00305 for (size_t i = 0; i < allThreads.GetSize (); ++i) 00306 { 00307 allThreads[i]->Stop (); 00308 } 00309 } 00310 00315 void WaitAll () 00316 { 00317 ScopedLock<Mutex> lock (mutex); 00318 00319 for (size_t i = 0; i < allThreads.GetSize (); ++i) 00320 { 00321 allThreads[i]->Wait (); 00322 } 00323 } 00324 00325 private: 00326 csRefArray<Thread> allThreads; 00327 mutable Mutex mutex; 00328 }; 00329 } 00330 } 00331 00332 00333 00334 #endif
Generated for Crystal Space 2.0 by doxygen 1.6.1