00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef WINSTL_INCL_H_WINSTL
00060 # include "winstl.h"
00061 #endif
00062
00063
00064
00065
00066
00067 #ifndef _WINSTL_NO_NAMESPACE
00068 # if defined(_STLSOFT_NO_NAMESPACE) || \
00069 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00070
00071 namespace winstl
00072 {
00073 # else
00074
00075
00076 namespace stlsoft
00077 {
00078
00079 namespace winstl_project
00080 {
00081
00082 # endif
00083 #endif
00084
00085
00086
00087
00088
00089 #ifdef __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT
00090 # undef __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT
00091 #endif
00092
00093 #ifdef __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00094 # undef __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00095 #endif
00096
00097 #if defined(_WIN32_WINNT) && \
00098 _WIN32_WINNT >= 0x0403
00099 # define __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT
00100 #endif
00101
00102 #if defined(_WIN32_WINNT) && \
00103 _WIN32_WINNT >= 0x0400
00104 # define __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00105 #endif
00106
00107
00108
00109
00110
00111
00113
00114 {
00115 public:
00116 typedef thread_mutex class_type;
00117
00118
00119 public:
00121 thread_mutex() winstl_throw_0()
00122 {
00123 ::InitializeCriticalSection(&m_cs);
00124 }
00125 #if defined(__WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT)
00130 thread_mutex(ws_dword_t spinCount) winstl_throw_0()
00131 {
00132 ::InitializeCriticalSectionAndSpinCount(&m_cs, spinCount);
00133 }
00134 #endif
00135
00136 ~thread_mutex() winstl_throw_0()
00137 {
00138 ::DeleteCriticalSection(&m_cs);
00139 }
00140
00141
00142 public:
00144 void lock() winstl_throw_0()
00145 {
00146 ::EnterCriticalSection(&m_cs);
00147 }
00148 #if defined(__WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT)
00153 bool try_lock()
00154 {
00155 return ::TryEnterCriticalSection(&m_cs) != FALSE;
00156 }
00157 #endif
00158
00159 void unlock() winstl_throw_0()
00160 {
00161 ::LeaveCriticalSection(&m_cs);
00162 }
00163
00164 #if defined(__WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT)
00170 ws_dword_t set_spin_count(ws_dword_t spinCount) winstl_throw_0()
00171 {
00172 return ::SetCriticalSectionSpinCount(&m_cs, spinCount);
00173 }
00174 #endif
00175
00176
00177 private:
00178 CRITICAL_SECTION m_cs;
00179
00180
00181 private:
00182 thread_mutex(class_type const &rhs);
00183 thread_mutex &operator =(class_type const &rhs);
00184 };
00185
00186
00187
00188
00189
00190 #ifndef _WINSTL_NO_NAMESPACE
00191 # if defined(_STLSOFT_NO_NAMESPACE) || \
00192 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00193 }
00194 # else
00195 }
00196 # endif
00197 #endif
00198
00200
00203
00207
00212
00216 inline void lock_instance(winstl_ns_qual(thread_mutex) &mx)
00217 {
00218 mx.lock();
00219 }
00220
00224 inline void unlock_instance(winstl_ns_qual(thread_mutex) &mx)
00225 {
00226 mx.unlock();
00227 }
00228
00230
00231 #ifndef _WINSTL_NO_NAMESPACE
00232 # if defined(_STLSOFT_NO_NAMESPACE) || \
00233 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00234 namespace winstl {
00235 # else
00236 namespace winstl_project {
00237 # if defined(__STLSOFT_COMPILER_IS_BORLAND)
00238 using ::stlsoft::lock_instance;
00239 using ::stlsoft::unlock_instance;
00240 # endif
00241 # endif
00242 #endif
00243
00244
00245
00246
00247
00248
00250
00251 {
00252 public:
00254 typedef thread_mutex lock_type;
00255 typedef thread_mutex_lock_traits class_type;
00256
00257
00258 public:
00260 static void lock(thread_mutex &c)
00261 {
00262 lock_instance(c);
00263 }
00264
00266 static void unlock(thread_mutex &c)
00267 {
00268 unlock_instance(c);
00269 }
00270 };
00271
00273
00274
00275 #ifdef STLSOFT_UNITTEST
00276
00277 namespace unittest
00278 {
00279 ss_bool_t test_winstl_thread_mutex(unittest_reporter *r)
00280 {
00281 using stlsoft::unittest::unittest_initialiser;
00282
00283 ss_bool_t bSuccess = true;
00284
00285 unittest_initialiser init(r, "WinSTL", "thread_mutex", __FILE__);
00286
00287 thread_mutex mx_r;
00288
00289 mx_r.lock();
00290 #if defined(__WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT)
00291 if(!mx_r.try_lock())
00292 {
00293 r->report("process_mutex (recursive) could not lock recursively ", __LINE__);
00294 bSuccess = false;
00295 }
00296 else
00297 {
00298 mx_r.unlock();
00299 }
00300 #endif
00301 mx_r.unlock();
00302
00303 return bSuccess;
00304 }
00305
00306 unittest_registrar unittest_winstl_thread_mutex(test_winstl_thread_mutex);
00307
00308 }
00309
00310 #endif
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 #endif
00323
00324
00325
00326
00327
00328