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;
104 static_mutex& m_mutex;
106 scoped_static_mutex_lock(const scoped_static_mutex_lock&);
107 scoped_static_mutex_lock& operator=(const scoped_static_mutex_lock&);
110 inline scoped_static_mutex_lock::operator void const*()const
112 return locked() ? this : 0;
115 inline bool scoped_static_mutex_lock::locked()const
124 // Portable version of a static mutex based on Boost.Thread library:
125 // This has to use a single mutex shared by all instances of static_mutex
126 // because boost::call_once doesn't alow us to pass instance information
127 // down to the initialisation proceedure. In fact the initialisation routine
128 // may need to be called more than once - but only once per instance.
130 // Since this preprocessor path is almost never taken, we hide these header
131 // dependencies so that build tools don't find them.
133 #define B1 <boost/thread/once.hpp>
134 #define B2 <boost/thread/recursive_mutex.hpp>
142 class BOOST_REGEX_DECL scoped_static_mutex_lock;
143 extern "C" BOOST_REGEX_DECL void free_static_mutex();
145 class BOOST_REGEX_DECL static_mutex
148 typedef scoped_static_mutex_lock scoped_lock;
150 static boost::recursive_mutex* m_pmutex;
151 static boost::once_flag m_once;
154 #define BOOST_STATIC_MUTEX_INIT { }
156 class BOOST_REGEX_DECL scoped_static_mutex_lock
159 scoped_static_mutex_lock(static_mutex& mut, bool lk = true);
160 ~scoped_static_mutex_lock();
161 operator void const*()const;
166 boost::recursive_mutex::scoped_lock* m_plock;
170 inline scoped_static_mutex_lock::operator void const*()const
172 return locked() ? this : 0;
175 inline bool scoped_static_mutex_lock::locked()const