29 #ifndef _CONCURRENCE_H 30 #define _CONCURRENCE_H 1 32 #pragma GCC system_header 35 #include <bits/gthr.h> 40 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 enum _Lock_policy { _S_single, _S_mutex, _S_atomic };
53 static const _Lock_policy __default_lock_policy =
55 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) \ 56 && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) 72 {
return "__gnu_cxx::__concurrence_lock_error"; }
80 {
return "__gnu_cxx::__concurrence_unlock_error"; }
88 {
return "__gnu_cxx::__concurrence_broadcast_error"; }
96 {
return "__gnu_cxx::__concurrence_wait_error"; }
101 __throw_concurrence_lock_error()
102 { _GLIBCXX_THROW_OR_ABORT(__concurrence_lock_error()); }
105 __throw_concurrence_unlock_error()
106 { _GLIBCXX_THROW_OR_ABORT(__concurrence_unlock_error()); }
108 #ifdef __GTHREAD_HAS_COND 110 __throw_concurrence_broadcast_error()
111 { _GLIBCXX_THROW_OR_ABORT(__concurrence_broadcast_error()); }
114 __throw_concurrence_wait_error()
115 { _GLIBCXX_THROW_OR_ABORT(__concurrence_wait_error()); }
121 #if __GTHREADS && defined __GTHREAD_MUTEX_INIT 122 __gthread_mutex_t _M_mutex = __GTHREAD_MUTEX_INIT;
124 __gthread_mutex_t _M_mutex;
127 __mutex(
const __mutex&);
128 __mutex& operator=(
const __mutex&);
133 #if __GTHREADS && ! defined __GTHREAD_MUTEX_INIT 134 if (__gthread_active_p())
135 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
139 #if __GTHREADS && ! defined __GTHREAD_MUTEX_INIT 142 if (__gthread_active_p())
143 __gthread_mutex_destroy(&_M_mutex);
150 if (__gthread_active_p())
152 if (__gthread_mutex_lock(&_M_mutex) != 0)
153 __throw_concurrence_lock_error();
161 if (__gthread_active_p())
163 if (__gthread_mutex_unlock(&_M_mutex) != 0)
164 __throw_concurrence_unlock_error();
169 __gthread_mutex_t* gthread_mutex(
void)
170 {
return &_M_mutex; }
173 class __recursive_mutex
176 #if __GTHREADS && defined __GTHREAD_RECURSIVE_MUTEX_INIT 177 __gthread_recursive_mutex_t _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
179 __gthread_recursive_mutex_t _M_mutex;
182 __recursive_mutex(
const __recursive_mutex&);
183 __recursive_mutex& operator=(
const __recursive_mutex&);
188 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT 189 if (__gthread_active_p())
190 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
194 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT 197 if (__gthread_active_p())
198 __gthread_recursive_mutex_destroy(&_M_mutex);
205 if (__gthread_active_p())
207 if (__gthread_recursive_mutex_lock(&_M_mutex) != 0)
208 __throw_concurrence_lock_error();
216 if (__gthread_active_p())
218 if (__gthread_recursive_mutex_unlock(&_M_mutex) != 0)
219 __throw_concurrence_unlock_error();
224 __gthread_recursive_mutex_t* gthread_recursive_mutex(
void)
225 {
return &_M_mutex; }
234 typedef __mutex __mutex_type;
237 __mutex_type& _M_device;
243 explicit __scoped_lock(__mutex_type& __name) : _M_device(__name)
244 { _M_device.lock(); }
247 { _M_device.unlock(); }
250 #ifdef __GTHREAD_HAS_COND 254 #if __GTHREADS && defined __GTHREAD_COND_INIT 255 __gthread_cond_t _M_cond = __GTHREAD_COND_INIT;
257 __gthread_cond_t _M_cond;
260 __cond(
const __cond&);
261 __cond& operator=(
const __cond&);
266 #if __GTHREADS && ! defined __GTHREAD_COND_INIT 267 if (__gthread_active_p())
268 __GTHREAD_COND_INIT_FUNCTION(&_M_cond);
272 #if __GTHREADS && ! defined __GTHREAD_COND_INIT 275 if (__gthread_active_p())
276 __gthread_cond_destroy(&_M_cond);
283 if (__gthread_active_p())
285 if (__gthread_cond_broadcast(&_M_cond) != 0)
286 __throw_concurrence_broadcast_error();
291 void wait(__mutex *mutex)
295 if (__gthread_cond_wait(&_M_cond, mutex->gthread_mutex()) != 0)
296 __throw_concurrence_wait_error();
301 void wait_recursive(__recursive_mutex *mutex)
305 if (__gthread_cond_wait_recursive(&_M_cond,
306 mutex->gthread_recursive_mutex())
308 __throw_concurrence_wait_error();
315 _GLIBCXX_END_NAMESPACE_VERSION
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
GNU extensions for public use.
Base class for all library exceptions.