30 #ifndef _CONCURRENCE_H
31 #define _CONCURRENCE_H 1
33 #pragma GCC system_header
36 #include <bits/gthr.h>
41 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 enum _Lock_policy { _S_single, _S_mutex, _S_atomic };
54 static const _Lock_policy __default_lock_policy =
56 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) \
57 && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4))
73 {
return "__gnu_cxx::__concurrence_lock_error"; }
81 {
return "__gnu_cxx::__concurrence_unlock_error"; }
89 {
return "__gnu_cxx::__concurrence_broadcast_error"; }
97 {
return "__gnu_cxx::__concurrence_wait_error"; }
102 __throw_concurrence_lock_error()
105 throw __concurrence_lock_error();
112 __throw_concurrence_unlock_error()
115 throw __concurrence_unlock_error();
121 #ifdef __GTHREAD_HAS_COND
123 __throw_concurrence_broadcast_error()
126 throw __concurrence_broadcast_error();
133 __throw_concurrence_wait_error()
136 throw __concurrence_wait_error();
143 template<
typename _Tp>
145 __copy_gthr_type(_Tp& __to,
const _Tp& __from)
147 #if defined __GXX_EXPERIMENTAL_CXX0X__ \
148 && defined _GLIBCXX_GTHREADS_NO_COPY_ASSIGN_IN_CXX11
149 __builtin_memcpy(&__to, &__from,
sizeof(__to));
158 __gthread_mutex_t _M_mutex;
160 __mutex(
const __mutex&);
161 __mutex& operator=(
const __mutex&);
167 if (__gthread_active_p())
169 #if defined __GTHREAD_MUTEX_INIT
170 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
171 __copy_gthr_type(_M_mutex, __tmp);
173 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
179 #if __GTHREADS && ! defined __GTHREAD_MUTEX_INIT
182 if (__gthread_active_p())
183 __gthread_mutex_destroy(&_M_mutex);
190 if (__gthread_active_p())
192 if (__gthread_mutex_lock(&_M_mutex) != 0)
193 __throw_concurrence_lock_error();
201 if (__gthread_active_p())
203 if (__gthread_mutex_unlock(&_M_mutex) != 0)
204 __throw_concurrence_unlock_error();
209 __gthread_mutex_t* gthread_mutex(
void)
210 {
return &_M_mutex; }
213 class __recursive_mutex
216 __gthread_recursive_mutex_t _M_mutex;
218 __recursive_mutex(
const __recursive_mutex&);
219 __recursive_mutex& operator=(
const __recursive_mutex&);
225 if (__gthread_active_p())
227 #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
228 __gthread_recursive_mutex_t __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
229 __copy_gthr_type(_M_mutex, __tmp);
231 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
237 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT
240 if (__gthread_active_p())
241 _S_destroy(&_M_mutex);
248 if (__gthread_active_p())
250 if (__gthread_recursive_mutex_lock(&_M_mutex) != 0)
251 __throw_concurrence_lock_error();
259 if (__gthread_active_p())
261 if (__gthread_recursive_mutex_unlock(&_M_mutex) != 0)
262 __throw_concurrence_unlock_error();
267 __gthread_recursive_mutex_t* gthread_recursive_mutex(
void)
268 {
return &_M_mutex; }
270 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT
274 template<
typename _Mx,
typename _Rm>
276 _S_destroy_win32(_Mx* __mx, _Rm
const* __rmx)
278 __mx->counter = __rmx->counter;
279 __mx->sema = __rmx->sema;
280 __gthread_mutex_destroy(__mx);
284 template<
typename _Rm>
285 static typename __enable_if<(bool)sizeof(&_Rm::sema), void>::__type
286 _S_destroy(_Rm* __mx)
288 __gthread_mutex_t __tmp;
289 _S_destroy_win32(&__tmp, __mx);
293 template<
typename _Rm>
294 static typename __enable_if<(bool)sizeof(&_Rm::actual), void>::__type
295 _S_destroy(_Rm* __mx)
296 { __gthread_mutex_destroy(&__mx->actual); }
299 template<
typename _Rm>
301 __enable_if<std::__are_same<_Rm, __gthread_mutex_t>::__value,
303 _S_destroy(_Rm* __mx)
304 { __gthread_mutex_destroy(__mx); }
314 typedef __mutex __mutex_type;
317 __mutex_type& _M_device;
323 explicit __scoped_lock(__mutex_type& __name) : _M_device(__name)
324 { _M_device.lock(); }
327 { _M_device.unlock(); }
330 #ifdef __GTHREAD_HAS_COND
334 __gthread_cond_t _M_cond;
336 __cond(
const __cond&);
337 __cond& operator=(
const __cond&);
343 if (__gthread_active_p())
345 #if defined __GTHREAD_COND_INIT
346 __gthread_cond_t __tmp = __GTHREAD_COND_INIT;
347 __copy_gthr_type(_M_cond, __tmp);
349 __GTHREAD_COND_INIT_FUNCTION(&_M_cond);
355 #if __GTHREADS && ! defined __GTHREAD_COND_INIT
358 if (__gthread_active_p())
359 __gthread_cond_destroy(&_M_cond);
366 if (__gthread_active_p())
368 if (__gthread_cond_broadcast(&_M_cond) != 0)
369 __throw_concurrence_broadcast_error();
374 void wait(__mutex *mutex)
378 if (__gthread_cond_wait(&_M_cond, mutex->gthread_mutex()) != 0)
379 __throw_concurrence_wait_error();
384 void wait_recursive(__recursive_mutex *mutex)
388 if (__gthread_cond_wait_recursive(&_M_cond,
389 mutex->gthread_recursive_mutex())
391 __throw_concurrence_wait_error();
398 _GLIBCXX_END_NAMESPACE_VERSION
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
Base class for all library exceptions.