sclang: ServerShmInterface - avoid object duplication in deepCopy
[supercollider.git] / external_libraries / boost / libs / regex / src / static_mutex.cpp
blobd14feb1e9b96132d0b3b3724de6de09cf212fc75
1 /*
3 * Copyright (c) 2004
4 * John Maddock
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>
22 #ifdef BOOST_HAS_THREADS
24 #include <boost/regex/pending/static_mutex.hpp>
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
36 namespace boost{
38 #if defined(BOOST_HAS_PTHREADS) && defined(PTHREAD_MUTEX_INITIALIZER)
40 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& m, bool lk)
41 : m_mutex(m), m_have_lock(false)
43 if(lk)
44 lock();
47 scoped_static_mutex_lock::~scoped_static_mutex_lock()
49 if(m_have_lock)
50 unlock();
53 void scoped_static_mutex_lock::lock()
55 if(0 == m_have_lock)
57 pthread_mutex_lock(&(m_mutex.m_mutex));
58 m_have_lock = true;
62 void scoped_static_mutex_lock::unlock()
64 if(m_have_lock)
66 pthread_mutex_unlock(&(m_mutex.m_mutex));
67 m_have_lock = false;
71 #elif defined(BOOST_HAS_WINTHREADS)
73 BOOST_STATIC_ASSERT(sizeof(LONG) == sizeof(boost::int32_t));
75 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& m, bool lk)
76 : m_mutex(m), m_have_lock(false)
78 if(lk)
79 lock();
82 scoped_static_mutex_lock::~scoped_static_mutex_lock()
84 if(m_have_lock)
85 unlock();
88 void scoped_static_mutex_lock::lock()
90 if(0 == m_have_lock)
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
98 Sleep(0);
100 m_have_lock = true;
104 void scoped_static_mutex_lock::unlock()
106 if(m_have_lock)
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;
117 #else
119 // Portable version of a static mutex based on Boost.Thread library:
121 #include <stdlib.h>
122 #include <boost/assert.hpp>
124 boost::recursive_mutex* static_mutex::m_pmutex = 0;
125 boost::once_flag static_mutex::m_once = BOOST_ONCE_INIT;
127 extern "C" BOOST_REGEX_DECL void boost_regex_free_static_mutex()
129 delete static_mutex::m_pmutex;
130 static_mutex::m_pmutex = 0;
133 void static_mutex::init()
135 m_pmutex = new boost::recursive_mutex();
136 int r = atexit(boost_regex_free_static_mutex);
137 BOOST_ASSERT(0 == r);
140 scoped_static_mutex_lock::scoped_static_mutex_lock(static_mutex& , bool lk)
141 : m_plock(0), m_have_lock(false)
143 if(lk)
144 lock();
147 scoped_static_mutex_lock::~scoped_static_mutex_lock()
149 if(m_have_lock)
150 unlock();
151 delete m_plock;
154 void scoped_static_mutex_lock::lock()
156 if(0 == m_have_lock)
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, boost::defer_lock);
161 m_plock->lock();
162 m_have_lock = true;
166 void scoped_static_mutex_lock::unlock()
168 if(m_have_lock)
170 m_plock->unlock();
171 m_have_lock = false;
175 #endif
179 #endif // BOOST_HAS_THREADS