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.hpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares static_mutex lock type, there are three different
17 * implementations: POSIX pthreads, WIN32 threads, and portable,
18 * these are described in more detail below.
21 #ifndef BOOST_REGEX_STATIC_MUTEX_HPP
22 #define BOOST_REGEX_STATIC_MUTEX_HPP
24 #include <boost/config.hpp>
25 #include <boost/regex/config.hpp> // dll import/export options.
27 #ifdef BOOST_HAS_PTHREADS
31 #if defined(BOOST_HAS_PTHREADS) && defined(PTHREAD_MUTEX_INITIALIZER)
34 // simple wrap around a pthread_mutex_t initialized with
35 // PTHREAD_MUTEX_INITIALIZER.
39 class BOOST_REGEX_DECL scoped_static_mutex_lock;
44 typedef scoped_static_mutex_lock scoped_lock;
45 pthread_mutex_t m_mutex;
48 #define BOOST_STATIC_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER, }
50 class BOOST_REGEX_DECL scoped_static_mutex_lock
53 scoped_static_mutex_lock(static_mutex& mut, bool lk = true);
54 ~scoped_static_mutex_lock();
55 inline bool locked()const
59 inline operator void const*()const
61 return locked() ? this : 0;
66 static_mutex& m_mutex;
72 #elif defined(BOOST_HAS_WINTHREADS)
75 // Use a 32-bit int as a lock, along with a test-and-set
76 // implementation using InterlockedCompareExchange.
79 #include <boost/cstdint.hpp>
83 class BOOST_REGEX_DECL scoped_static_mutex_lock;
88 typedef scoped_static_mutex_lock scoped_lock;
89 boost::int32_t m_mutex;
92 #define BOOST_STATIC_MUTEX_INIT { 0, }
94 class BOOST_REGEX_DECL scoped_static_mutex_lock
97 scoped_static_mutex_lock(static_mutex& mut, bool lk = true);
98 ~scoped_static_mutex_lock();
99 operator void const*()const
101 return locked() ? this : 0;
110 static_mutex& m_mutex;
112 scoped_static_mutex_lock(const scoped_static_mutex_lock&);
113 scoped_static_mutex_lock& operator=(const scoped_static_mutex_lock&);
120 // Portable version of a static mutex based on Boost.Thread library:
121 // This has to use a single mutex shared by all instances of static_mutex
122 // because boost::call_once doesn't alow us to pass instance information
123 // down to the initialisation proceedure. In fact the initialisation routine
124 // may need to be called more than once - but only once per instance.
126 // Since this preprocessor path is almost never taken, we hide these header
127 // dependencies so that build tools don't find them.
129 #define B1 <boost/thread/once.hpp>
130 #define B2 <boost/thread/recursive_mutex.hpp>
138 class BOOST_REGEX_DECL scoped_static_mutex_lock;
139 extern "C" BOOST_REGEX_DECL void boost_regex_free_static_mutex();
141 class BOOST_REGEX_DECL static_mutex
144 typedef scoped_static_mutex_lock scoped_lock;
146 static boost::recursive_mutex* m_pmutex;
147 static boost::once_flag m_once;
150 #define BOOST_STATIC_MUTEX_INIT { }
152 class BOOST_REGEX_DECL scoped_static_mutex_lock
155 scoped_static_mutex_lock(static_mutex& mut, bool lk = true);
156 ~scoped_static_mutex_lock();
157 operator void const*()const;
162 boost::recursive_mutex::scoped_lock* m_plock;
166 inline scoped_static_mutex_lock::operator void const*()const
168 return locked() ? this : 0;
171 inline bool scoped_static_mutex_lock::locked()const