CppUnit project page | CppUnit home page |
00001 #ifndef CPPTL_THREAD_H_INCLUDED 00002 # define CPPTL_THREAD_H_INCLUDED 00003 00004 # include <cpptl/forwards.h> 00005 # if CPPTL_HAS_THREAD 00006 # include <cpptl/functor.h> 00007 # endif // # if CPPTL_HAS_THREAD 00008 00009 00010 namespace CppTL { 00011 00012 void CPPTL_API processThreadExitHandlers(); 00013 00014 00015 // Non-recursive mutex 00016 class CPPTL_API Mutex : public NonCopyable 00017 { 00018 public: 00019 class ScopedLockGuard 00020 { 00021 public: 00022 ScopedLockGuard( Mutex &mutex ) 00023 : mutex_( mutex ) 00024 { 00025 mutex_.lock(); 00026 } 00027 00028 ~ScopedLockGuard() 00029 { 00030 mutex_.unlock(); 00031 } 00032 00033 private: 00034 Mutex &mutex_; 00035 }; 00036 00037 Mutex(); 00038 ~Mutex(); 00039 00040 void lock(); 00041 void unlock(); 00042 # if CPPTL_HAS_THREAD 00043 private: 00044 void *data_; 00045 # endif 00046 }; 00047 00048 00049 // ////////////////////////////////////////////////////////////////// 00050 // ////////////////////////////////////////////////////////////////// 00051 // Implementation of thread API for NON THREAD-SAFE mode 00052 // ////////////////////////////////////////////////////////////////// 00053 // ////////////////////////////////////////////////////////////////// 00054 # if !(CPPTL_HAS_THREAD) 00055 00056 template<class ValueType> 00057 class ThreadLocalStorage 00058 { 00059 public: 00060 typedef ThreadLocalStorage<ValueType> SelfType; 00061 00062 ThreadLocalStorage( const ValueType &defaultValue = ValueType() ) 00063 : value_( defaultValue ) 00064 { 00065 } 00066 00067 bool isInitialized() const 00068 { 00069 return true; 00070 } 00071 00072 ValueType &get() 00073 { 00074 return value_; 00075 } 00076 00077 private: 00078 ValueType value_; 00079 }; 00080 00081 00082 // ////////////////////////////////////////////////////////////////// 00083 // ////////////////////////////////////////////////////////////////// 00084 // Implementation of thread API for THREAD-SAFE mode 00085 // ////////////////////////////////////////////////////////////////// 00086 // ////////////////////////////////////////////////////////////////// 00087 # else // # if !(CPPTL_HAS_THREAD) 00088 00090 namespace Impl { 00091 00092 class RawThreadStorage; 00093 00094 RawThreadStorage *CPPTL_API createRawThreadStorage( const Functor1<void *> &deallocator ); 00095 00096 void CPPTL_API freeRawThreadStorage( RawThreadStorage *storage ); 00097 00098 void *CPPTL_API getRawThreadStorage( RawThreadStorage *storage ); 00099 00100 void setRawThreadStorage( RawThreadStorage *storage, void *value ); 00101 00102 } // namespace Impl 00104 00105 00106 template<class ValueType> 00107 class ThreadLocalStorage : public NonCopyable 00108 { 00109 public: 00110 typedef void(*FreeFn)(void*); 00111 typedef ThreadLocalStorage<ValueType> SelfType; 00112 00113 ThreadLocalStorage( const ValueType &defaultValue = ValueType() ) 00114 : storage_( Impl::createRawThreadStorage( cfn1( FreeFn(freeValue) ) ) ) 00115 , defaultValue_( defaultValue ) 00116 { 00117 } 00118 00119 virtual ~ThreadLocalStorage() 00120 { 00121 Impl::freeRawThreadStorage( storage_ ); 00122 } 00123 00124 bool isInitialized() const 00125 { 00126 return Impl::getRawThreadStorage( storage_ ) != 0; 00127 } 00128 00129 ValueType &get() 00130 { 00131 ValueType * value = static_cast<ValueType *>( 00132 Impl::getRawThreadStorage( storage_ ) ); 00133 if ( !value ) 00134 { 00135 value = new ValueType( defaultValue_ ); 00136 Impl::setRawThreadStorage( storage_, value ); 00137 } 00138 return *value; 00139 } 00140 00141 SelfType &operator =( const ValueType &other ) 00142 { 00143 get() = other; 00144 return *this; 00145 } 00146 00147 private: 00148 static void freeValue( void *p ) 00149 { 00150 delete static_cast<ValueType *>( p ); 00151 } 00152 00153 ValueType defaultValue_; 00154 Impl::RawThreadStorage *storage_; 00155 }; 00156 00157 # endif // # ifndef CPPTL_THREAD_SAFE 00158 00159 } // namespace CppTL 00160 00161 00162 00163 #endif // CPPTL_THREAD_H_INCLUDED
hosts this site. |
Send comments to: CppUnit Developers |