]> git.lyx.org Git - features.git/blob - boost/libs/regex/src/static_mutex.cpp
boost: add eol property
[features.git] / boost / libs / regex / src / static_mutex.cpp
1 /*
2  *
3  * Copyright (c) 2004
4  * John Maddock
5  *
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)
9  *
10  */
11  
12  /*
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.
17   */
18
19 #define BOOST_REGEX_SOURCE
20 #include <boost/config.hpp>
21
22 #ifdef BOOST_HAS_THREADS
23
24 #include <boost/regex/pending/static_mutex.hpp>
25
26 #if defined(BOOST_HAS_WINTHREADS)
27 #ifndef NOMINMAX
28 #  define NOMINMAX
29 #endif
30 #define WIN32_LEAN_AND_MEAN
31 #include <windows.h>
32 #include <boost/static_assert.hpp>
33 #endif
34
35
36 namespace boost{
37
38 #if defined(BOOST_HAS_PTHREADS) && defined(PTHREAD_MUTEX_INITIALIZER)
39
40 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& m, bool lk)
41 : m_mutex(m), m_have_lock(false)
42 {
43    if(lk)
44       lock();
45 }
46
47 scoped_static_mutex_lock::~scoped_static_mutex_lock()
48 {
49    if(m_have_lock)
50       unlock();
51 }
52
53 void scoped_static_mutex_lock::lock()
54 {
55    if(0 == m_have_lock)
56    {
57       pthread_mutex_lock(&(m_mutex.m_mutex));
58       m_have_lock = true;
59    }
60 }
61
62 void scoped_static_mutex_lock::unlock()
63 {
64    if(m_have_lock)
65    {
66       pthread_mutex_unlock(&(m_mutex.m_mutex));
67       m_have_lock = false;
68    }
69 }
70
71 #elif defined(BOOST_HAS_WINTHREADS)
72
73 BOOST_STATIC_ASSERT(sizeof(LONG) == sizeof(boost::int32_t));
74
75 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& m, bool lk)
76 : m_mutex(m), m_have_lock(false)
77 {
78    if(lk)
79       lock();
80 }
81
82 scoped_static_mutex_lock::~scoped_static_mutex_lock()
83 {
84    if(m_have_lock)
85       unlock();
86 }
87
88 void scoped_static_mutex_lock::lock()
89 {
90    if(0 == m_have_lock)
91    {
92 #if !defined(InterlockedCompareExchangePointer)
93       while(0 != InterlockedCompareExchange(reinterpret_cast<void**>((boost::uint_least16_t*)&(m_mutex.m_mutex)), (void*)1, 0))
94 #else
95       while(0 != InterlockedCompareExchange(reinterpret_cast<LONG*>(&(m_mutex.m_mutex)), 1, 0))
96 #endif
97       {
98          Sleep(0);
99       }
100       m_have_lock = true;
101    }
102 }
103
104 void scoped_static_mutex_lock::unlock()
105 {
106    if(m_have_lock)
107    {
108 #if !defined(InterlockedCompareExchangePointer)
109       InterlockedExchange((LONG*)&(m_mutex.m_mutex), 0);
110 #else
111       InterlockedExchange(reinterpret_cast<LONG*>(&(m_mutex.m_mutex)), 0);
112 #endif
113       m_have_lock = false;
114    }
115 }
116
117 #else
118 //
119 // Portable version of a static mutex based on Boost.Thread library:
120 //
121 #include <stdlib.h>
122 #include <boost/assert.hpp>
123
124 boost::recursive_mutex* static_mutex::m_pmutex = 0;
125 boost::once_flag static_mutex::m_once = BOOST_ONCE_INIT;
126
127 extern "C" BOOST_REGEX_DECL void free_static_mutex()
128 {
129    delete static_mutex::m_pmutex;
130    static_mutex::m_pmutex = 0;
131 }
132
133 void static_mutex::init()
134 {
135    m_pmutex = new boost::recursive_mutex();
136    int r = atexit(free_static_mutex);
137    BOOST_ASSERT(0 == r);
138 }
139
140 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& , bool lk)
141 : m_plock(0), m_have_lock(false)
142 {
143    if(lk)
144       lock();
145 }
146
147 scoped_static_mutex_lock::~scoped_static_mutex_lock()
148 {
149    if(m_have_lock)
150       unlock();
151    delete m_plock;
152 }
153
154 void scoped_static_mutex_lock::lock()
155 {
156    if(0 == m_have_lock)
157    {
158        boost::call_once(static_mutex::m_once,&static_mutex::init);
159       if(0 == m_plock)
160          m_plock = new boost::recursive_mutex::scoped_lock(*static_mutex::m_pmutex, false);
161       m_plock->lock();
162       m_have_lock = true;
163    }
164 }
165
166 void scoped_static_mutex_lock::unlock()
167 {
168    if(m_have_lock)
169    {
170       m_plock->unlock();
171       m_have_lock = false;
172    }
173 }
174
175 #endif
176
177 }
178
179 #endif // BOOST_HAS_THREADS