6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE static_mutex.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares static_mutex lock type.
19 #define BOOST_REGEX_SOURCE
20 #include <boost/config.hpp>
22 #ifdef BOOST_HAS_THREADS
24 #include <boost/regex/pending/static_mutex.hpp>
26 #if defined(BOOST_HAS_WINTHREADS)
30 #define WIN32_LEAN_AND_MEAN
32 #include <boost/static_assert.hpp>
38 #if defined(BOOST_HAS_PTHREADS) && defined(PTHREAD_MUTEX_INITIALIZER)
40 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& m, bool lk)
41 : m_mutex(m), m_have_lock(false)
47 scoped_static_mutex_lock::~scoped_static_mutex_lock()
53 void scoped_static_mutex_lock::lock()
57 pthread_mutex_lock(&(m_mutex.m_mutex));
62 void scoped_static_mutex_lock::unlock()
66 pthread_mutex_unlock(&(m_mutex.m_mutex));
71 #elif defined(BOOST_HAS_WINTHREADS)
73 BOOST_STATIC_ASSERT(sizeof(LONG) == sizeof(boost::int32_t));
75 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& m, bool lk)
76 : m_mutex(m), m_have_lock(false)
82 scoped_static_mutex_lock::~scoped_static_mutex_lock()
88 void scoped_static_mutex_lock::lock()
92 #if !defined(InterlockedCompareExchangePointer)
93 while(0 != InterlockedCompareExchange(reinterpret_cast<void**>((boost::uint_least16_t*)&(m_mutex.m_mutex)), (void*)1, 0))
95 while(0 != InterlockedCompareExchange(reinterpret_cast<LONG*>(&(m_mutex.m_mutex)), 1, 0))
104 void scoped_static_mutex_lock::unlock()
108 #if !defined(InterlockedCompareExchangePointer)
109 InterlockedExchange((LONG*)&(m_mutex.m_mutex), 0);
111 InterlockedExchange(reinterpret_cast<LONG*>(&(m_mutex.m_mutex)), 0);
119 // Portable version of a static mutex based on Boost.Thread library:
122 #include <boost/assert.hpp>
124 boost::recursive_mutex* static_mutex::m_pmutex = 0;
125 boost::once_flag static_mutex::m_once = BOOST_ONCE_INIT;
127 extern "C" BOOST_REGEX_DECL void free_static_mutex()
129 delete static_mutex::m_pmutex;
130 static_mutex::m_pmutex = 0;
133 void static_mutex::init()
135 m_pmutex = new boost::recursive_mutex();
136 int r = atexit(free_static_mutex);
137 BOOST_ASSERT(0 == r);
140 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& , bool lk)
141 : m_plock(0), m_have_lock(false)
147 scoped_static_mutex_lock::~scoped_static_mutex_lock()
154 void scoped_static_mutex_lock::lock()
158 boost::call_once(static_mutex::m_once,&static_mutex::init);
160 m_plock = new boost::recursive_mutex::scoped_lock(*static_mutex::m_pmutex, false);
166 void scoped_static_mutex_lock::unlock()
179 #endif // BOOST_HAS_THREADS