1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
*
* Copyright (c) 2004
* John Maddock
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
/*
* LOCATION: see http://www.boost.org for most recent version.
* FILE static_mutex.cpp
* VERSION see <boost/version.hpp>
* DESCRIPTION: Declares static_mutex lock type.
*/
#define BOOST_REGEX_SOURCE
#include <boost/regex/config.hpp>
#if defined(BOOST_REGEX_CXX03)
#include <boost/assert.hpp>
#ifdef BOOST_HAS_THREADS
#include <boost/regex/pending/static_mutex.hpp>
#if defined(BOOST_HAS_WINTHREADS)
#ifndef NOMINMAX
# define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <boost/static_assert.hpp>
#endif
namespace boost{
#if defined(BOOST_HAS_PTHREADS) && defined(PTHREAD_MUTEX_INITIALIZER)
scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& m, bool lk)
: m_mutex(m), m_have_lock(false)
{
if(lk)
lock();
}
scoped_static_mutex_lock::~scoped_static_mutex_lock()
{
if(m_have_lock)
unlock();
}
void scoped_static_mutex_lock::lock()
{
if(0 == m_have_lock)
{
// Client code will throw if this fails:
m_have_lock = (pthread_mutex_lock(&(m_mutex.m_mutex)) == 0);
}
}
void scoped_static_mutex_lock::unlock()
{
if(m_have_lock)
{
// If this fails there's nothing we can do except assert,
// exceptions are out of the question as this code is called
// from the lock's destructor:
BOOST_VERIFY(pthread_mutex_unlock(&(m_mutex.m_mutex)) == 0);
m_have_lock = false;
}
}
#elif defined(BOOST_HAS_WINTHREADS)
BOOST_STATIC_ASSERT(sizeof(LONG) == sizeof(boost::int32_t));
scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& m, bool lk)
: m_mutex(m), m_have_lock(false)
{
if(lk)
lock();
}
scoped_static_mutex_lock::~scoped_static_mutex_lock()
{
if(m_have_lock)
unlock();
}
void scoped_static_mutex_lock::lock()
{
if(0 == m_have_lock)
{
#if !defined(InterlockedCompareExchangePointer)
while(0 != InterlockedCompareExchange(reinterpret_cast<void**>((boost::uint_least16_t*)&(m_mutex.m_mutex)), (void*)1, 0))
#else
while(0 != InterlockedCompareExchange(reinterpret_cast<LONG*>(&(m_mutex.m_mutex)), 1, 0))
#endif
{
Sleep(0);
}
m_have_lock = true;
}
}
void scoped_static_mutex_lock::unlock()
{
if(m_have_lock)
{
#if !defined(InterlockedCompareExchangePointer)
InterlockedExchange((LONG*)&(m_mutex.m_mutex), 0);
#else
InterlockedExchange(reinterpret_cast<LONG*>(&(m_mutex.m_mutex)), 0);
#endif
m_have_lock = false;
}
}
#else
//
// Portable version of a static mutex based on Boost.Thread library:
//
#include <stdlib.h>
#include <boost/assert.hpp>
boost::recursive_mutex* static_mutex::m_pmutex = 0;
boost::once_flag static_mutex::m_once = BOOST_ONCE_INIT;
extern "C" BOOST_REGEX_DECL void boost_regex_free_static_mutex()
{
delete static_mutex::m_pmutex;
static_mutex::m_pmutex = 0;
}
void static_mutex::init()
{
m_pmutex = new boost::recursive_mutex();
int r = atexit(boost_regex_free_static_mutex);
BOOST_ASSERT(0 == r);
}
scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& , bool lk)
: m_plock(0), m_have_lock(false)
{
if(lk)
lock();
}
scoped_static_mutex_lock::~scoped_static_mutex_lock()
{
if(m_have_lock)
unlock();
delete m_plock;
}
void scoped_static_mutex_lock::lock()
{
if(0 == m_have_lock)
{
boost::call_once(static_mutex::m_once,&static_mutex::init);
if(0 == m_plock)
m_plock = new boost::unique_lock<boost::recursive_mutex>(*static_mutex::m_pmutex, boost::defer_lock);
m_plock->lock();
m_have_lock = true;
}
}
void scoped_static_mutex_lock::unlock()
{
if(m_have_lock)
{
m_plock->unlock();
m_have_lock = false;
}
}
#endif
}
#endif // BOOST_HAS_THREADS
#endif
|