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>
21 #include <boost/assert.hpp>
23 #ifdef BOOST_HAS_THREADS
25 #include <boost/regex/pending/static_mutex.hpp>
27 #if defined(BOOST_HAS_WINTHREADS)
31 #define WIN32_LEAN_AND_MEAN
33 #include <boost/static_assert.hpp>
39 #if defined(BOOST_HAS_PTHREADS) && defined(PTHREAD_MUTEX_INITIALIZER)
41 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex
& m
, bool lk
)
42 : m_mutex(m
), m_have_lock(false)
48 scoped_static_mutex_lock::~scoped_static_mutex_lock()
54 void scoped_static_mutex_lock::lock()
58 // Client code will throw if this fails:
59 m_have_lock
= (pthread_mutex_lock(&(m_mutex
.m_mutex
)) == 0);
63 void scoped_static_mutex_lock::unlock()
67 // If this fails there's nothing we can do except assert,
68 // exceptions are out of the question as this code is called
69 // from the lock's destructor:
70 BOOST_VERIFY(pthread_mutex_unlock(&(m_mutex
.m_mutex
)) == 0);
75 #elif defined(BOOST_HAS_WINTHREADS)
77 BOOST_STATIC_ASSERT(sizeof(LONG
) == sizeof(boost::int32_t));
79 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex
& m
, bool lk
)
80 : m_mutex(m
), m_have_lock(false)
86 scoped_static_mutex_lock::~scoped_static_mutex_lock()
92 void scoped_static_mutex_lock::lock()
96 #if !defined(InterlockedCompareExchangePointer)
97 while(0 != InterlockedCompareExchange(reinterpret_cast<void**>((boost::uint_least16_t*)&(m_mutex
.m_mutex
)), (void*)1, 0))
99 while(0 != InterlockedCompareExchange(reinterpret_cast<LONG
*>(&(m_mutex
.m_mutex
)), 1, 0))
108 void scoped_static_mutex_lock::unlock()
112 #if !defined(InterlockedCompareExchangePointer)
113 InterlockedExchange((LONG
*)&(m_mutex
.m_mutex
), 0);
115 InterlockedExchange(reinterpret_cast<LONG
*>(&(m_mutex
.m_mutex
)), 0);
123 // Portable version of a static mutex based on Boost.Thread library:
126 #include <boost/assert.hpp>
128 boost::recursive_mutex
* static_mutex::m_pmutex
= 0;
129 boost::once_flag
static_mutex::m_once
= BOOST_ONCE_INIT
;
131 extern "C" BOOST_REGEX_DECL
void boost_regex_free_static_mutex()
133 delete static_mutex::m_pmutex
;
134 static_mutex::m_pmutex
= 0;
137 void static_mutex::init()
139 m_pmutex
= new boost::recursive_mutex();
140 int r
= atexit(boost_regex_free_static_mutex
);
141 BOOST_ASSERT(0 == r
);
144 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex
& , bool lk
)
145 : m_plock(0), m_have_lock(false)
151 scoped_static_mutex_lock::~scoped_static_mutex_lock()
158 void scoped_static_mutex_lock::lock()
162 boost::call_once(static_mutex::m_once
,&static_mutex::init
);
164 m_plock
= new boost::unique_lock
<boost::recursive_mutex
>(*static_mutex::m_pmutex
, boost::defer_lock
);
170 void scoped_static_mutex_lock::unlock()
183 #endif // BOOST_HAS_THREADS