CrystalSpace

Public API Reference

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,
00042 
00044   THREAD_PRIO_NORMAL,
00045 
00050   THREAD_PRIO_HIGH
00051 };
00052 
00058 class Runnable : public csRefCount, private CS::NonCopyable
00059 {
00060 public:
00066   virtual void Run () = 0;
00067 };
00068 
00069 }
00070 }
00071 
00072 // Include implementation specific versions
00073 #if defined(CS_PLATFORM_WIN32)
00074 # include "csutil/threading/win32_thread.h"
00075 #elif defined(CS_PLATFORM_UNIX) || \
00076   defined(CS_PLATFORM_MACOSX)
00077 # include "csutil/threading/pthread_thread.h"
00078 #else
00079 #error "No threading implementation for your platform"
00080 #endif
00081 
00082 
00083 namespace CS
00084 {
00085 namespace Threading
00086 {
00087   
00092   class Thread : public csRefCount, private CS::NonCopyable, 
00093     private Implementation::ThreadBase
00094   {
00095   public:
00096 
00103     Thread (Runnable* runnable, bool start = false)
00104       : ThreadBase (runnable)
00105     {
00106       if (start)
00107         Start ();
00108     }
00109 
00115     Thread (Runnable* runnable, ThreadPriority prio)
00116       : ThreadBase (runnable)
00117     {
00118       SetPriority (prio);
00119     }
00120 
00128     Thread (Runnable* runnable, bool start, ThreadPriority prio)
00129       : ThreadBase (runnable)
00130     {
00131       SetPriority (prio);
00132 
00133       if (start)
00134         Start ();
00135     }
00136   
00137     ~Thread ()
00138     {
00139       if (IsRunning ())
00140         Stop ();
00141     }
00142 
00146     void Start ()
00147     {
00148       ThreadBase::Start ();
00149     }
00150 
00159     void Stop ()
00160     {
00161       ThreadBase::Stop ();
00162     }
00163 
00167     bool IsRunning () const
00168     {
00169       return ThreadBase::IsRunning ();
00170     }
00171 
00179     bool SetPriority (ThreadPriority prio)
00180     {
00181       if (prio == priority)
00182         return true;
00183 
00184       bool result;
00185       result = ThreadBase::SetPriority (prio);
00186       
00187       if (result)
00188         priority = priority;
00189 
00190       return result;
00191     }
00192 
00196     ThreadPriority GetPriority () const
00197     {
00198       return priority;
00199     }
00200 
00205     void Wait () const
00206     {
00207       ThreadBase::Wait ();
00208     }
00209 
00216     static void Yield ()
00217     {
00218       ThreadBase::Yield ();
00219     }
00220 
00221   private:
00222     ThreadPriority priority;
00223   };
00224 
00225 
00229    class ThreadGroup : private CS::NonCopyable
00230   {
00231   public:
00232 
00238     void Add (Thread* thread)
00239     {
00240       ScopedLock<Mutex> lock (mutex);
00241       allThreads.PushSmart (thread);
00242     }
00243 
00247     void Remove (Thread* thread)
00248     {
00249       ScopedLock<Mutex> lock (mutex);
00250       allThreads.Delete (thread);
00251     }
00252     
00256     Thread* GetThread (size_t index) const
00257     {
00258       ScopedLock<Mutex> lock (mutex);
00259       return allThreads.Get (index);
00260     }
00261     
00265     size_t GetSize () const
00266     {
00267       return allThreads.GetSize ();
00268     }
00269 
00274     void StartAll ()
00275     {
00276       ScopedLock<Mutex> lock (mutex);
00277 
00278       for (size_t i = 0; i < allThreads.GetSize (); ++i)
00279       {
00280         allThreads[i]->Start ();
00281       }
00282     }
00283 
00288     void StopAll ()
00289     {
00290       ScopedLock<Mutex> lock (mutex);
00291 
00292       for (size_t i = 0; i < allThreads.GetSize (); ++i)
00293       {
00294         allThreads[i]->Stop ();
00295       }
00296     }
00297 
00302     void WaitAll ()
00303     {
00304       ScopedLock<Mutex> lock (mutex);
00305       
00306       for (size_t i = 0; i < allThreads.GetSize (); ++i)
00307       {
00308         allThreads[i]->Wait ();
00309       }
00310     }
00311 
00312   private:
00313     csRefArray<Thread> allThreads;
00314     mutable Mutex mutex;
00315   };
00316 }
00317 }
00318 
00319 
00320 
00321 #endif

Generated for Crystal Space 1.2 by doxygen 1.4.7