00001 /*----------------------------------------------------------------------------- 00002 Name: ThreadBase.h 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Encapsulates (and hides) threads 00006 ------------------------------------------------------------------------------*/ 00007 00008 #ifndef _UTIL_THREAD_THREADBASE_H 00009 #define _UTIL_THREAD_THREADBASE_H 00010 00011 #include <util/xmlBlasterDef.h> 00012 00013 class thread; 00014 class mutex; 00015 class lock; 00016 class condition; 00017 00018 namespace org { namespace xmlBlaster { namespace util { namespace thread { 00019 00020 class ThreadImpl; 00021 class MutexImpl; 00022 class LockImpl; 00023 class ConditionImpl; 00024 00025 class Thread; 00026 class Condition; 00027 class Lock; 00028 00029 /* -------------------------- ThreadRunner --------------------------*/ 00030 00031 class ThreadRunner 00032 { 00033 public: 00034 Thread& owner_; 00035 ThreadRunner(Thread& owner); 00036 void operator()(); 00037 }; 00038 00039 00040 /* -------------------------- Thread --------------------------------*/ 00041 00058 class Dll_Export Thread 00059 { 00060 friend class ThreadRunner; 00061 private: 00062 ThreadImpl* thread_; 00063 ThreadRunner* runner_; 00064 bool isStarted_; 00065 00066 public: 00067 Thread(); 00068 00069 virtual ~Thread(); 00070 00076 virtual bool start(bool detached); 00077 00079 virtual void run() = 0; 00080 00082 static void sleepNanos(org::xmlBlaster::util::Timestamp nanoSecondDelay); 00083 00085 static void sleep(long millis); 00086 00088 static void sleepSecs(long secs); 00089 00091 static org::xmlBlaster::util::Timestamp getCurrentTimestamp(); 00092 00093 00098 virtual void join(); 00099 00100 00101 bool isRunning() const 00102 { 00103 return (thread_ != NULL); 00104 } 00105 00106 }; 00107 00108 00109 /* -------------------------- MutexClass -----------------------------*/ 00110 00111 class Dll_Export MutexClass 00112 { 00113 friend class Lock; 00114 private: 00115 MutexImpl* mutex_; 00116 00117 public: 00127 MutexClass(bool recursive=false); 00128 00129 ~MutexClass(); 00130 }; 00131 00132 00133 /* -------------------------- Lock ------------------------------*/ 00134 00135 class Dll_Export Lock 00136 { 00137 friend class Condition; 00138 private: 00139 LockImpl* lock_; 00140 00141 public: 00142 Lock(const MutexClass& mutex, bool ignore=false); 00143 00144 ~Lock(); 00145 }; 00146 00147 00148 /* -------------------------- Lock ------------------------------*/ 00149 00150 class Dll_Export Condition 00151 { 00152 private: 00153 ConditionImpl* condition_; 00154 public: 00155 Condition(); 00156 00157 ~Condition(); 00158 00159 void wait(const Lock& lock, long delay); 00160 00161 void notify(); 00162 }; 00163 00164 00165 }}}} // namespaces 00166 00167 #endif 00168