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 
00070   typedef unsigned int ThreadID;
00071 
00072 }
00073 }
00074 
00075 // Include implementation specific versions
00076 #if defined(CS_PLATFORM_WIN32)
00077 # include "csutil/threading/win32_thread.h"
00078 #elif defined(CS_PLATFORM_UNIX) || \
00079   defined(CS_PLATFORM_MACOSX)
00080 # include "csutil/threading/pthread_thread.h"
00081 #else
00082 #error "No threading implementation for your platform"
00083 #endif
00084 
00085 
00086 namespace CS
00087 {
00088 namespace Threading
00089 {
00090   
00091   
00096   class Thread : public csRefCount, private CS::NonCopyable, 
00097     private Implementation::ThreadBase
00098   {
00099   public:
00100 
00107     Thread (Runnable* runnable, bool start = false)
00108       : ThreadBase (runnable)
00109     {
00110       if (start)
00111         Start ();
00112     }
00113 
00119     Thread (Runnable* runnable, ThreadPriority prio)
00120       : ThreadBase (runnable)
00121     {
00122       SetPriority (prio);
00123     }
00124 
00132     Thread (Runnable* runnable, bool start, ThreadPriority prio)
00133       : ThreadBase (runnable)
00134     {
00135       SetPriority (prio);
00136 
00137       if (start)
00138         Start ();
00139     }
00140   
00141     ~Thread ()
00142     {
00143       if (IsRunning ())
00144         Stop ();
00145     }
00146 
00150     void Start ()
00151     {
00152       ThreadBase::Start ();
00153     }
00154 
00163     void Stop ()
00164     {
00165       ThreadBase::Stop ();
00166     }
00167 
00171     bool IsRunning () const
00172     {
00173       return ThreadBase::IsRunning ();
00174     }
00175 
00183     bool SetPriority (ThreadPriority prio)
00184     {      
00185       return ThreadBase::SetPriority (prio);          
00186     }
00187 
00191     ThreadPriority GetPriority () const
00192     {
00193       return ThreadBase::GetPriority ();
00194     }
00195 
00200     void Wait () const
00201     {
00202       ThreadBase::Wait ();
00203     }
00204 
00211     static void Yield ()
00212     {
00213       ThreadBase::Yield ();
00214     }
00215 
00221     static ThreadID GetThreadID ()
00222     {
00223       return ThreadBase::GetThreadID ();
00224     }
00225   };
00226 
00227 
00231   class ThreadGroup : private CS::NonCopyable
00232   {
00233   public:
00234 
00240     void Add (Thread* thread)
00241     {
00242       ScopedLock<Mutex> lock (mutex);
00243       allThreads.PushSmart (thread);
00244     }
00245 
00249     void Remove (Thread* thread)
00250     {
00251       ScopedLock<Mutex> lock (mutex);
00252       allThreads.Delete (thread);
00253     }
00254     
00258     Thread* GetThread (size_t index) const
00259     {
00260       ScopedLock<Mutex> lock (mutex);
00261       return allThreads.Get (index);
00262     }
00263     
00267     size_t GetSize () const
00268     {
00269       return allThreads.GetSize ();
00270     }
00271 
00276     void StartAll ()
00277     {
00278       ScopedLock<Mutex> lock (mutex);
00279 
00280       for (size_t i = 0; i < allThreads.GetSize (); ++i)
00281       {
00282         allThreads[i]->Start ();
00283       }
00284     }
00285 
00290     void StopAll ()
00291     {
00292       ScopedLock<Mutex> lock (mutex);
00293 
00294       for (size_t i = 0; i < allThreads.GetSize (); ++i)
00295       {
00296         allThreads[i]->Stop ();
00297       }
00298     }
00299 
00304     void WaitAll ()
00305     {
00306       ScopedLock<Mutex> lock (mutex);
00307       
00308       for (size_t i = 0; i < allThreads.GetSize (); ++i)
00309       {
00310         allThreads[i]->Wait ();
00311       }
00312     }
00313 
00314   private:
00315     csRefArray<Thread> allThreads;
00316     mutable Mutex mutex;
00317   };
00318 }
00319 }
00320 
00321 
00322 
00323 #endif

Generated for Crystal Space 1.2.1 by doxygen 1.5.3