1 // Copyright (C) 2000 Stephen Cleary
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org for updates, documentation, and revision history.
9 //////////////////////////////////////////////////////////////////////////////
11 // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
12 // Software License, Version 1.0. (See accompanying file
13 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
15 // See http://www.boost.org/libs/container for documentation.
17 //////////////////////////////////////////////////////////////////////////////
19 #ifndef BOOST_CONTAINER_MUTEX_HPP
20 #define BOOST_CONTAINER_MUTEX_HPP
22 #ifndef BOOST_CONFIG_HPP
23 # include <boost/config.hpp>
26 #if defined(BOOST_HAS_PRAGMA_ONCE)
30 //#define BOOST_CONTAINER_NO_MT
31 //#define BOOST_CONTAINER_NO_SPINLOCKS
33 #include <boost/container/detail/config_begin.hpp>
34 #include <boost/container/detail/workaround.hpp>
36 // Extremely Light-Weight wrapper classes for OS thread synchronization
38 #define BOOST_MUTEX_HELPER_NONE 0
39 #define BOOST_MUTEX_HELPER_WIN32 1
40 #define BOOST_MUTEX_HELPER_PTHREAD 2
41 #define BOOST_MUTEX_HELPER_SPINLOCKS 3
43 #if !defined(BOOST_HAS_THREADS) && !defined(BOOST_NO_MT)
47 #if defined(BOOST_NO_MT) || defined(BOOST_CONTAINER_NO_MT)
48 // No multithreading -> make locks into no-ops
49 #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_NONE
52 #if !defined(BOOST_CONTAINER_NO_SPINLOCKS) && \
53 ((defined(__GNUC__) && \
54 ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) || \
55 defined(__i386__) || defined(__x86_64__))) || \
56 (defined(_MSC_VER) && _MSC_VER>=1310))
57 #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_SPINLOCKS
60 #if defined(BOOST_WINDOWS)
62 #ifndef BOOST_MUTEX_HELPER
63 #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_WIN32
65 #elif defined(BOOST_HAS_UNISTD_H)
67 #if !defined(BOOST_MUTEX_HELPER) && (defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS))
68 #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_PTHREAD
73 #ifndef BOOST_MUTEX_HELPER
74 #error Unable to determine platform mutex type; #define BOOST_NO_MT to assume single-threaded
77 #if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE
79 #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_SPINLOCKS
82 /* These are already defined on AMD64 builds */
85 #endif /* __cplusplus */
86 long __cdecl _InterlockedCompareExchange(long volatile *Dest, long Exchange, long Comp);
87 long __cdecl _InterlockedExchange(long volatile *Target, long Value);
90 #endif /* __cplusplus */
92 #pragma intrinsic (_InterlockedCompareExchange)
93 #pragma intrinsic (_InterlockedExchange)
94 #define interlockedcompareexchange _InterlockedCompareExchange
95 #define interlockedexchange _InterlockedExchange
96 #elif defined(WIN32) && defined(__GNUC__)
97 #define interlockedcompareexchange(a, b, c) __sync_val_compare_and_swap(a, c, b)
98 #define interlockedexchange __sync_lock_test_and_set
101 /* First, define CAS_LOCK and CLEAR_LOCK on ints */
102 /* Note CAS_LOCK defined to return 0 on success */
104 #if defined(__GNUC__)&& (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
105 #define BOOST_CONTAINER_CAS_LOCK(sl) __sync_lock_test_and_set(sl, 1)
106 #define BOOST_CONTAINER_CLEAR_LOCK(sl) __sync_lock_release(sl)
108 #elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
109 /* Custom spin locks for older gcc on x86 */
110 static inline int boost_container_x86_cas_lock(int *sl) {
114 __asm__ __volatile__ ("lock; cmpxchgl %1, %2"
116 : "r" (val), "m" (*(sl)), "0"(cmp)
121 static inline void boost_container_x86_clear_lock(int* sl) {
125 __asm__ __volatile__ ("lock; xchgl %0, %1"
127 : "m" (*(sl)), "0"(prev)
131 #define BOOST_CONTAINER_CAS_LOCK(sl) boost_container_x86_cas_lock(sl)
132 #define BOOST_CONTAINER_CLEAR_LOCK(sl) boost_container_x86_clear_lock(sl)
134 #else /* Win32 MSC */
135 #define BOOST_CONTAINER_CAS_LOCK(sl) interlockedexchange((long volatile*)sl, (long)1)
136 #define BOOST_CONTAINER_CLEAR_LOCK(sl) interlockedexchange((long volatile*)sl, (long)0)
139 /* How to yield for a spin lock */
140 #define SPINS_PER_YIELD 63
141 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
142 #define SLEEP_EX_DURATION 50 /* delay for yield/sleep */
143 #define SPIN_LOCK_YIELD SleepEx(SLEEP_EX_DURATION, FALSE)
144 #elif defined (__SVR4) && defined (__sun) /* solaris */
146 #define SPIN_LOCK_YIELD thr_yield();
147 #elif !defined(LACKS_SCHED_H)
149 #define SPIN_LOCK_YIELD sched_yield();
151 #define SPIN_LOCK_YIELD
152 #endif /* ... yield ... */
154 #define BOOST_CONTAINER_SPINS_PER_YIELD 63
155 inline int boost_interprocess_spin_acquire_lock(int *sl) {
157 while (*(volatile int *)sl != 0 ||
158 BOOST_CONTAINER_CAS_LOCK(sl)) {
159 if ((++spins & BOOST_CONTAINER_SPINS_PER_YIELD) == 0) {
165 #define BOOST_CONTAINER_MLOCK_T int
166 #define BOOST_CONTAINER_TRY_LOCK(sl) !BOOST_CONTAINER_CAS_LOCK(sl)
167 #define BOOST_CONTAINER_RELEASE_LOCK(sl) BOOST_CONTAINER_CLEAR_LOCK(sl)
168 #define BOOST_CONTAINER_ACQUIRE_LOCK(sl) (BOOST_CONTAINER_CAS_LOCK(sl)? boost_interprocess_spin_acquire_lock(sl) : 0)
169 #define BOOST_MOVE_INITIAL_LOCK(sl) (*sl = 0)
170 #define BOOST_CONTAINER_DESTROY_LOCK(sl) (0)
171 #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32
173 #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD
178 namespace container {
179 namespace container_detail {
181 #if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE
185 null_mutex(const null_mutex &);
186 void operator=(const null_mutex &);
191 static void lock() { }
192 static void unlock() { }
195 typedef null_mutex default_mutex;
196 #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_SPINLOCKS
201 BOOST_CONTAINER_MLOCK_T sl;
202 spin_mutex(const spin_mutex &);
203 void operator=(const spin_mutex &);
206 spin_mutex() { BOOST_MOVE_INITIAL_LOCK(&sl); }
208 void lock() { BOOST_CONTAINER_ACQUIRE_LOCK(&sl); }
209 void unlock() { BOOST_CONTAINER_RELEASE_LOCK(&sl); }
211 typedef spin_mutex default_mutex;
212 #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32
216 CRITICAL_SECTION mtx;
218 mutex(const mutex &);
219 void operator=(const mutex &);
223 { InitializeCriticalSection(&mtx); }
226 { DeleteCriticalSection(&mtx); }
229 { EnterCriticalSection(&mtx); }
232 { LeaveCriticalSection(&mtx); }
235 typedef mutex default_mutex;
236 #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD
242 mutex(const mutex &);
243 void operator=(const mutex &);
247 { pthread_mutex_init(&mtx, 0); }
250 { pthread_mutex_destroy(&mtx); }
253 { pthread_mutex_lock(&mtx); }
256 { pthread_mutex_unlock(&mtx); }
259 typedef mutex default_mutex;
262 template<class Mutex>
266 scoped_lock(Mutex &m)
276 } // namespace container_detail
277 } // namespace container
280 #undef BOOST_MUTEX_HELPER_WIN32
281 #undef BOOST_MUTEX_HELPER_PTHREAD
282 #undef BOOST_MUTEX_HELPER_NONE
283 #undef BOOST_MUTEX_HELPER
284 #undef BOOST_MUTEX_HELPER_SPINLOCKS
286 #include <boost/container/detail/config_end.hpp>