STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

winstl_thread_mutex.h

Go to the documentation of this file.
00001 /* 
00002  * File:        winstl_thread_mutex.h (originally MWCrtSct.h, ::SynesisWin)
00003  *
00004  * Purpose:     Intra-process mutex, based on Windows CRITICAL_SECTION.
00005  *
00006  * Date:        17th December 1996
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2002-2004, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * 
00039 
00040 
00044 
00045 #ifndef WINSTL_INCL_H_WINSTL_THREAD_MUTEX
00046 #define WINSTL_INCL_H_WINSTL_THREAD_MUTEX
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define WINSTL_VER_H_WINSTL_THREAD_MUTEX_MAJOR     2
00050 # define WINSTL_VER_H_WINSTL_THREAD_MUTEX_MINOR     0
00051 # define WINSTL_VER_H_WINSTL_THREAD_MUTEX_REVISION  1
00052 # define WINSTL_VER_H_WINSTL_THREAD_MUTEX_EDIT      25
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef WINSTL_INCL_H_WINSTL
00060 # include "winstl.h"    // Include the WinSTL root header
00061 #endif /* !WINSTL_INCL_H_WINSTL */
00062 
00063 /* 
00064  * Namespace
00065  */
00066 
00067 #ifndef _WINSTL_NO_NAMESPACE
00068 # if defined(_STLSOFT_NO_NAMESPACE) || \
00069      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00070 /* There is no stlsoft namespace, so must define ::winstl */
00071 namespace winstl
00072 {
00073 # else
00074 /* Define stlsoft::winstl_project */
00075 
00076 namespace stlsoft
00077 {
00078 
00079 namespace winstl_project
00080 {
00081 
00082 # endif /* _STLSOFT_NO_NAMESPACE */
00083 #endif /* !_WINSTL_NO_NAMESPACE */
00084 
00085 /* 
00086  * Spin-count support
00087  */
00088 
00089 #ifdef __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT
00090 # undef __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT
00091 #endif /* __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT */
00092 
00093 #ifdef __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00094 # undef __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00095 #endif /* __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT */
00096 
00097 #if defined(_WIN32_WINNT) && \
00098     _WIN32_WINNT >= 0x0403
00099 # define __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT
00100 #endif /* _WIN32_WINNT >= 0x0403 */
00101 
00102 #if defined(_WIN32_WINNT) && \
00103     _WIN32_WINNT >= 0x0400
00104 # define __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT
00105 #endif /* _WIN32_WINNT >= 0x0400 */
00106 
00107 /* 
00108  * Classes
00109  */
00110 
00111 // class thread_mutex
00113 class thread_mutex
00114 {
00115 public:
00116     typedef thread_mutex class_type;
00117 
00118 // Construction
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 /* __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT */
00135 
00136     ~thread_mutex() winstl_throw_0()
00137     {
00138         ::DeleteCriticalSection(&m_cs);
00139     }
00140 
00141 // Operations
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 /* __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT */
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 /* __WINSTL_THREAD_MUTEX_SPIN_COUNT_SUPPORT */
00175 
00176 // Members
00177 private:
00178     CRITICAL_SECTION    m_cs;   // critical section
00179 
00180 // Not to be implemented
00181 private:
00182     thread_mutex(class_type const &rhs);
00183     thread_mutex &operator =(class_type const &rhs);
00184 };
00185 
00186 /* 
00187  * Control shims
00188  */
00189 
00190 #ifndef _WINSTL_NO_NAMESPACE
00191 # if defined(_STLSOFT_NO_NAMESPACE) || \
00192      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00193 } // namespace winstl
00194 # else
00195 } // namespace winstl_project
00196 # endif /* _STLSOFT_NO_NAMESPACE */
00197 #endif /* !_WINSTL_NO_NAMESPACE */
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 /* __STLSOFT_COMPILER_IS_BORLAND */
00241 # endif /* _STLSOFT_NO_NAMESPACE */
00242 #endif /* !_WINSTL_NO_NAMESPACE */
00243 
00244 /* 
00245  * lock_traits (for the compilers that do not support Koenig Lookup)
00246  */
00247 
00248 // class lock_traits
00250 struct thread_mutex_lock_traits
00251 {
00252 public:
00254     typedef thread_mutex                lock_type;
00255     typedef thread_mutex_lock_traits    class_type;
00256 
00257 // Operations
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 // Unit-testing
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 /* __WINSTL_THREAD_MUTEX_TRY_LOCK_SUPPORT */
00301         mx_r.unlock();
00302 
00303         return bSuccess;
00304     }
00305 
00306     unittest_registrar    unittest_winstl_thread_mutex(test_winstl_thread_mutex);
00307 
00308 } // namespace unittest
00309 
00310 #endif /* STLSOFT_UNITTEST */
00311 
00312 /* 
00313 
00314 #ifndef _WINSTL_NO_NAMESPACE
00315 # if defined(_STLSOFT_NO_NAMESPACE) || \
00316      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00317 } // namespace winstl
00318 # else
00319 } // namespace winstl_project
00320 } // namespace stlsoft
00321 # endif /* _STLSOFT_NO_NAMESPACE */
00322 #endif /* !_WINSTL_NO_NAMESPACE */
00323 
00324 /* 
00325 
00326 #endif /* !WINSTL_INCL_H_WINSTL_THREAD_MUTEX */
00327 
00328 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004