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
00091
00092 {
00093 public:
00094 typedef spin_mutex class_type;
00095
00096
00097 public:
00099 ss_explicit_k spin_mutex(ws_sint32_t *p = NULL) winstl_throw_0()
00100 : m_spinCount((NULL != p) ? p : &m_internalCount)
00101 , m_internalCount(0)
00102 #ifdef STLSOFT_SPINMUTEX_COUNT_LOCKS
00103 , m_cLocks(0)
00104 #endif
00105 {}
00107 ~spin_mutex() winstl_throw_0()
00108 {
00109 #ifdef STLSOFT_SPINMUTEX_COUNT_LOCKS
00110 stlsoft_assert(m_cLocks == 0);
00111 #endif // STLSOFT_SPINMUTEX_COUNT_LOCKS
00112 }
00113
00114
00115 public:
00117 void lock() winstl_throw_0()
00118 {
00119 for(; 0 != ::InterlockedExchange((LPLONG)m_spinCount, 1); ::Sleep(1))
00120 {}
00121 #ifdef STLSOFT_SPINMUTEX_COUNT_LOCKS
00122 stlsoft_assert(++m_cLocks != 0);
00123 #endif // STLSOFT_SPINMUTEX_COUNT_LOCKS
00124 }
00126 void unlock() winstl_throw_0()
00127 {
00128 #ifdef STLSOFT_SPINMUTEX_COUNT_LOCKS
00129 stlsoft_assert(m_cLocks-- != 0);
00130 #endif // STLSOFT_SPINMUTEX_COUNT_LOCKS
00131 (void)::InterlockedExchange((LPLONG)m_spinCount, 0);
00132 }
00133
00134
00135 private:
00136 ws_sint32_t *m_spinCount;
00137 ws_sint32_t m_internalCount;
00138 #ifdef STLSOFT_SPINMUTEX_COUNT_LOCKS
00139 ws_sint32_t m_cLocks;
00140 #endif // STLSOFT_SPINMUTEX_COUNT_LOCKS
00141
00142
00143 private:
00144 spin_mutex(class_type const &rhs);
00145 spin_mutex &operator =(class_type const &rhs);
00146 };
00147
00148
00149
00150
00151
00152 #ifndef _WINSTL_NO_NAMESPACE
00153 # if defined(_STLSOFT_NO_NAMESPACE) || \
00154 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00155 }
00156 # else
00157 }
00158 # endif
00159 #endif
00160
00162
00165
00169
00174
00178 inline void lock_instance(winstl_ns_qual(spin_mutex) &mx)
00179 {
00180 mx.lock();
00181 }
00182
00186 inline void unlock_instance(winstl_ns_qual(spin_mutex) &mx)
00187 {
00188 mx.unlock();
00189 }
00190
00192
00193 #ifndef _WINSTL_NO_NAMESPACE
00194 # if defined(_STLSOFT_NO_NAMESPACE) || \
00195 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00196 namespace winstl {
00197 # else
00198 namespace winstl_project {
00199 # if defined(__STLSOFT_COMPILER_IS_BORLAND)
00200 using ::stlsoft::lock_instance;
00201 using ::stlsoft::unlock_instance;
00202 # endif
00203 # endif
00204 #endif
00205
00206
00207
00208
00209
00210
00212
00213 {
00214 public:
00216 typedef spin_mutex lock_type;
00217 typedef spin_mutex_lock_traits class_type;
00218
00219
00220 public:
00222 static void lock(spin_mutex &c)
00223 {
00224 lock_instance(c);
00225 }
00226
00228 static void unlock(spin_mutex &c)
00229 {
00230 unlock_instance(c);
00231 }
00232 };
00233
00235
00236
00237 #ifdef STLSOFT_UNITTEST
00238
00239 namespace unittest
00240 {
00241 ss_bool_t test_winstl_spin_mutex(unittest_reporter *r)
00242 {
00243 using stlsoft::unittest::unittest_initialiser;
00244
00245 ss_bool_t bSuccess = true;
00246
00247 unittest_initialiser init(r, "WinSTL", "spin_mutex", __FILE__);
00248
00249 #if 0
00250 if(<<TODO>>)
00251 {
00252 r->report("<<TODO>> failed ", __LINE__);
00253 bSuccess = false;
00254 }
00255 #endif
00256
00257 return bSuccess;
00258 }
00259
00260 unittest_registrar unittest_winstl_spin_mutex(test_winstl_spin_mutex);
00261
00262 }
00263
00264 #endif
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 #endif
00277
00278
00279
00280
00281
00282