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
00047 #ifndef WINSTL_INCL_WINSTL_SYNCH_HPP_THREAD_MUTEX
00048 #define WINSTL_INCL_WINSTL_SYNCH_HPP_THREAD_MUTEX
00049
00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00051 # define WINSTL_VER_WINSTL_SYNCH_HPP_THREAD_MUTEX_MAJOR 4
00052 # define WINSTL_VER_WINSTL_SYNCH_HPP_THREAD_MUTEX_MINOR 0
00053 # define WINSTL_VER_WINSTL_SYNCH_HPP_THREAD_MUTEX_REVISION 1
00054 # define WINSTL_VER_WINSTL_SYNCH_HPP_THREAD_MUTEX_EDIT 52
00055 #endif
00056
00057
00058
00059
00060
00061 #ifndef WINSTL_INCL_WINSTL_H_WINSTL
00062 # include <winstl/winstl.h>
00063 #endif
00064 #ifndef STLSOFT_INCL_STLSOFT_SYNCH_HPP_CONCEPTS
00065 # include <stlsoft/synch/concepts.hpp>
00066 #endif
00067
00068
00069
00070
00071
00072 #ifndef _WINSTL_NO_NAMESPACE
00073 # if defined(_STLSOFT_NO_NAMESPACE) || \
00074 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00075
00076 namespace winstl
00077 {
00078 # else
00079
00080
00081 namespace stlsoft
00082 {
00083
00084 namespace winstl_project
00085 {
00086
00087 # endif
00088 #endif
00089
00090
00091
00092
00093
00094 #ifdef __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT
00095 # undef __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT
00096 #endif
00097
00098 #ifdef __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00099 # undef __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00100 #endif
00101
00102 #if defined(_WIN32_WINNT) && \
00103 _WIN32_WINNT >= 0x0403
00104 # define __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT
00105 #endif
00106
00107 #if defined(_WIN32_WINNT) && \
00108 _WIN32_WINNT >= 0x0400
00109 # define __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00110 #endif
00111
00112
00113
00114
00115
00116
00121 class thread_mutex
00122 : public stlsoft_ns_qual(critical_section)< STLSOFT_CRITICAL_SECTION_IS_RECURSIVE
00123 #ifdef __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00124 , STLSOFT_CRITICAL_SECTION_IS_TRYABLE
00125 #else
00126 , STLSOFT_CRITICAL_SECTION_ISNOT_TRYABLE
00127 #endif
00128 >
00129 {
00130 public:
00132 typedef thread_mutex class_type;
00133
00134
00135 public:
00137 thread_mutex() stlsoft_throw_0()
00138 {
00139 ::InitializeCriticalSection(&m_cs);
00140 }
00141 #if defined(__WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT)
00146 thread_mutex(ws_dword_t spinCount) stlsoft_throw_0()
00147 {
00148 ::InitializeCriticalSectionAndSpinCount(&m_cs, spinCount);
00149 }
00150 #endif
00152 ~thread_mutex() stlsoft_throw_0()
00153 {
00154 ::DeleteCriticalSection(&m_cs);
00155 }
00156
00157
00158 public:
00160 void lock() stlsoft_throw_0()
00161 {
00162 ::EnterCriticalSection(&m_cs);
00163 }
00164 #if defined(__WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT)
00169 bool try_lock()
00170 {
00171 return ::TryEnterCriticalSection(&m_cs) != FALSE;
00172 }
00173 #endif
00175 void unlock() stlsoft_throw_0()
00176 {
00177 ::LeaveCriticalSection(&m_cs);
00178 }
00179
00180 #if defined(__WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT)
00186 ws_dword_t set_spin_count(ws_dword_t spinCount) stlsoft_throw_0()
00187 {
00188 return ::SetCriticalSectionSpinCount(&m_cs, spinCount);
00189 }
00190 #endif
00191
00192
00193 private:
00194 CRITICAL_SECTION m_cs;
00195
00196
00197 private:
00198 thread_mutex(class_type const& rhs);
00199 thread_mutex& operator =(class_type const& rhs);
00200 };
00201
00202
00203
00204
00205
00206 #ifndef _WINSTL_NO_NAMESPACE
00207 # if defined(_STLSOFT_NO_NAMESPACE) || \
00208 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00209 }
00210 # else
00211 }
00212 # endif
00213 #endif
00214
00221 inline void lock_instance(winstl_ns_qual(thread_mutex) &mx)
00222 {
00223 mx.lock();
00224 }
00225
00232 inline void unlock_instance(winstl_ns_qual(thread_mutex) &mx)
00233 {
00234 mx.unlock();
00235 }
00236
00237
00238 #ifndef _WINSTL_NO_NAMESPACE
00239 # if defined(_STLSOFT_NO_NAMESPACE) || \
00240 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00241 namespace winstl {
00242 # else
00243 namespace winstl_project {
00244 # if defined(STLSOFT_COMPILER_IS_BORLAND)
00245 using ::stlsoft::lock_instance;
00246 using ::stlsoft::unlock_instance;
00247 # endif
00248 # endif
00249 #endif
00250
00251
00252
00253
00254
00255
00260 struct thread_mutex_lock_traits
00261 {
00262 public:
00264 typedef thread_mutex lock_type;
00266 typedef thread_mutex_lock_traits class_type;
00267
00268
00269 public:
00271 static void lock(thread_mutex &c)
00272 {
00273 lock_instance(c);
00274 }
00275
00277 static void unlock(thread_mutex &c)
00278 {
00279 unlock_instance(c);
00280 }
00281 };
00282
00284
00285
00286 #ifdef STLSOFT_UNITTEST
00287 # include "./unittest/thread_mutex_unittest_.h"
00288 #endif
00289
00290
00291
00292 #ifndef _WINSTL_NO_NAMESPACE
00293 # if defined(_STLSOFT_NO_NAMESPACE) || \
00294 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00295 }
00296 # else
00297 }
00298 }
00299 # endif
00300 #endif
00301
00302
00303
00304 #endif
00305
00306