00001 00011 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00012 #pragma once 00013 #endif 00014 #ifndef COMMON_THREADS_H_ 00015 #define COMMON_THREADS_H_ 00016 00017 namespace common 00018 { 00019 00024 00026 class Thread_pimpl; 00028 class Mutex_pimpl; 00030 class Semaphore_pimpl; 00032 class Cond_pimpl; 00034 class Barrier_pimpl; 00036 class Event_pimpl; 00037 00039 00047 class Thread 00048 { 00049 DECLARE_NO_COPY_CLASS(Thread) 00050 friend class Thread_pimpl; 00051 00052 private: 00053 scoped_ptr<Thread_pimpl> pimpl; 00054 00055 protected: 00057 virtual void Run() = 0; 00058 00060 00065 void Exit(); 00067 00071 void Yield_(); 00072 00073 public: 00074 Thread(); 00075 virtual ~Thread(); 00076 00078 void Start(); 00080 00084 void Join(); 00085 00087 void Kill(); 00088 00089 // Rzeczy które nie wiem jak zrealizować w pthreads - działają tylko w Windows 00090 #ifdef WIN32 00092 bool IsRunning(); 00094 00098 void Pause(); 00099 void Resume(); 00100 00105 static const int PRIORITY_IDLE; 00106 static const int PRIORITY_VERY_LOW; 00107 static const int PRIORITY_LOW; 00108 static const int PRIORITY_DEFAULT; 00109 static const int PRIORITY_HIGH; 00110 static const int PRIORITY_VERY_HIGH; 00111 static const int PRIORITY_REALTIME; 00112 void SetPriority(int Priority); 00113 int GetPriority(); 00115 #endif 00116 }; 00117 00119 00122 class Mutex 00123 { 00124 DECLARE_NO_COPY_CLASS(Mutex) 00125 friend class Mutex_pimpl; 00126 friend class Cond; 00127 00128 private: 00129 scoped_ptr<Mutex_pimpl> pimpl; 00130 00131 public: 00134 00135 static const uint FLAG_RECURSIVE = 0x01; 00137 static const uint FLAG_WAIT_TIMEOUT = 0x02; 00139 00140 Mutex(uint Flag); 00141 ~Mutex(); 00142 00144 void Lock(); 00146 void Unlock(); 00148 00152 bool TryLock(); 00154 00158 bool TimeoutLock(uint Milliseconds); 00159 }; 00160 00162 00166 class MutexLock 00167 { 00168 private: 00169 Mutex *m_Mutex; 00170 DECLARE_NO_COPY_CLASS(MutexLock) 00171 00172 public: 00173 MutexLock(Mutex *m) : m_Mutex(m) { m->Lock(); } 00174 ~MutexLock() { m_Mutex->Unlock(); } 00175 }; 00176 00182 #define MUTEX_LOCK(M) MutexLock __mutex_lock_obj(M); 00183 00185 00190 class Semaphore 00191 { 00192 DECLARE_NO_COPY_CLASS(Semaphore) 00193 00194 private: 00195 scoped_ptr<Semaphore_pimpl> pimpl; 00196 00197 public: 00198 Semaphore(uint InitialValue); 00199 ~Semaphore(); 00200 00202 00205 void P(); 00207 00211 bool TryP(); 00213 00217 bool TimeoutP(uint Milliseconds); 00218 00220 void V(); 00222 void V(uint ReleaseCount); 00223 }; 00224 00226 00232 class Cond 00233 { 00234 DECLARE_NO_COPY_CLASS(Cond) 00235 00236 private: 00237 scoped_ptr<Cond_pimpl> pimpl; 00238 00239 public: 00240 Cond(); 00241 ~Cond(); 00242 00244 00245 void Wait(Mutex *m); 00247 00251 bool TimeoutWait(Mutex *m, uint Milliseconds); 00252 00254 00258 void Signal(); 00260 00264 void Broadcast(); 00265 }; 00266 00268 00273 class Barrier 00274 { 00275 DECLARE_NO_COPY_CLASS(Barrier) 00276 00277 private: 00278 scoped_ptr<Barrier_pimpl> pimpl; 00279 00280 public: 00281 Barrier(uint NumThreads); 00282 ~Barrier(); 00283 00284 void Wait(); 00285 }; 00286 00288 00292 class Event 00293 { 00294 DECLARE_NO_COPY_CLASS(Event); 00295 00296 private: 00297 scoped_ptr<Event_pimpl> pimpl; 00298 00299 public: 00300 enum TYPE 00301 { 00304 TYPE_AUTO_RESET, 00307 TYPE_MANUAL_RESET, 00308 }; 00309 00310 Event(bool InitialState, TYPE Type); 00311 ~Event(); 00312 00314 00318 void Set(); 00320 void Reset(); 00321 00323 00327 void Wait(); 00329 00330 bool Test(); 00332 00337 bool TimeoutWait(uint Milliseconds); 00338 }; 00339 00341 // code_threads 00342 00343 } // namespace common 00344 00345 #endif
1.5.5