1 // Support for concurrent programing -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file ext/concurrence.h
27 * This file is a GNU extension to the Standard C++ Library.
30 #ifndef _CONCURRENCE_H
31 #define _CONCURRENCE_H 1
33 #pragma GCC system_header
36 #include <bits/gthr.h>
37 #include <bits/functexcept.h>
38 #include <bits/cpp_type_traits.h>
39 #include <ext/type_traits.h>
41 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 // Available locking policies:
46 // _S_single single-threaded code that doesn't need to be locked.
47 // _S_mutex multi-threaded code that requires additional support
48 // from gthr.h or abstraction layers in concurrence.h.
49 // _S_atomic multi-threaded code using atomic operations.
50 enum _Lock_policy
{ _S_single
, _S_mutex
, _S_atomic
};
52 // Compile time constant that indicates prefered locking policy in
53 // the current configuration.
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))
66 // NB: As this is used in libsupc++, need to only depend on
67 // exception. No stdexception classes, no use of std::string.
68 class __concurrence_lock_error
: public std::exception
73 { return "__gnu_cxx::__concurrence_lock_error"; }
76 class __concurrence_unlock_error
: public std::exception
81 { return "__gnu_cxx::__concurrence_unlock_error"; }
84 class __concurrence_broadcast_error
: public std::exception
89 { return "__gnu_cxx::__concurrence_broadcast_error"; }
92 class __concurrence_wait_error
: public std::exception
97 { return "__gnu_cxx::__concurrence_wait_error"; }
100 // Substitute for concurrence_error object in the case of -fno-exceptions.
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();
146 __gthread_mutex_t _M_mutex
;
148 __mutex(const __mutex
&);
149 __mutex
& operator=(const __mutex
&);
155 if (__gthread_active_p())
157 #if defined __GTHREAD_MUTEX_INIT
158 __gthread_mutex_t __tmp
= __GTHREAD_MUTEX_INIT
;
161 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex
);
167 #if __GTHREADS && ! defined __GTHREAD_MUTEX_INIT
170 if (__gthread_active_p())
171 __gthread_mutex_destroy(&_M_mutex
);
178 if (__gthread_active_p())
180 if (__gthread_mutex_lock(&_M_mutex
) != 0)
181 __throw_concurrence_lock_error();
189 if (__gthread_active_p())
191 if (__gthread_mutex_unlock(&_M_mutex
) != 0)
192 __throw_concurrence_unlock_error();
197 __gthread_mutex_t
* gthread_mutex(void)
198 { return &_M_mutex
; }
201 class __recursive_mutex
204 __gthread_recursive_mutex_t _M_mutex
;
206 __recursive_mutex(const __recursive_mutex
&);
207 __recursive_mutex
& operator=(const __recursive_mutex
&);
213 if (__gthread_active_p())
215 #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
216 __gthread_recursive_mutex_t __tmp
= __GTHREAD_RECURSIVE_MUTEX_INIT
;
219 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex
);
225 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT
228 if (__gthread_active_p())
229 _S_destroy(&_M_mutex
);
236 if (__gthread_active_p())
238 if (__gthread_recursive_mutex_lock(&_M_mutex
) != 0)
239 __throw_concurrence_lock_error();
247 if (__gthread_active_p())
249 if (__gthread_recursive_mutex_unlock(&_M_mutex
) != 0)
250 __throw_concurrence_unlock_error();
255 __gthread_recursive_mutex_t
* gthread_recursive_mutex(void)
256 { return &_M_mutex
; }
258 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT
259 // FIXME: gthreads doesn't define __gthread_recursive_mutex_destroy
260 // so we need to obtain a __gthread_mutex_t to destroy
262 template<typename _Mx
, typename _Rm
>
264 _S_destroy_win32(_Mx
* __mx
, _Rm
const* __rmx
)
266 __mx
->counter
= __rmx
->counter
;
267 __mx
->sema
= __rmx
->sema
;
268 __gthread_mutex_destroy(__mx
);
271 // matches a gthr-win32.h recursive mutex
272 template<typename _Rm
>
273 static typename __enable_if
<sizeof(&_Rm::sema
), void>::__type
274 _S_destroy(_Rm
* __mx
)
276 __gthread_mutex_t __tmp
;
277 _S_destroy_win32(&__tmp
, __mx
);
280 // matches a recursive mutex with a member 'actual'
281 template<typename _Rm
>
282 static typename __enable_if
<sizeof(&_Rm::actual
), void>::__type
283 _S_destroy(_Rm
* __mx
)
284 { __gthread_mutex_destroy(&__mx
->actual
); }
286 // matches when there's only one mutex type
287 template<typename _Rm
>
289 __enable_if
<std::__are_same
<_Rm
, __gthread_mutex_t
>::__value
,
291 _S_destroy(_Rm
* __mx
)
292 { __gthread_mutex_destroy(__mx
); }
296 /// Scoped lock idiom.
297 // Acquire the mutex here with a constructor call, then release with
298 // the destructor call in accordance with RAII style.
302 typedef __mutex __mutex_type
;
305 __mutex_type
& _M_device
;
307 __scoped_lock(const __scoped_lock
&);
308 __scoped_lock
& operator=(const __scoped_lock
&);
311 explicit __scoped_lock(__mutex_type
& __name
) : _M_device(__name
)
312 { _M_device
.lock(); }
314 ~__scoped_lock() throw()
315 { _M_device
.unlock(); }
318 #ifdef __GTHREAD_HAS_COND
322 __gthread_cond_t _M_cond
;
324 __cond(const __cond
&);
325 __cond
& operator=(const __cond
&);
331 if (__gthread_active_p())
333 #if defined __GTHREAD_COND_INIT
334 __gthread_cond_t __tmp
= __GTHREAD_COND_INIT
;
337 __GTHREAD_COND_INIT_FUNCTION(&_M_cond
);
343 #if __GTHREADS && ! defined __GTHREAD_COND_INIT
346 if (__gthread_active_p())
347 __gthread_cond_destroy(&_M_cond
);
354 if (__gthread_active_p())
356 if (__gthread_cond_broadcast(&_M_cond
) != 0)
357 __throw_concurrence_broadcast_error();
362 void wait(__mutex
*mutex
)
366 if (__gthread_cond_wait(&_M_cond
, mutex
->gthread_mutex()) != 0)
367 __throw_concurrence_wait_error();
372 void wait_recursive(__recursive_mutex
*mutex
)
376 if (__gthread_cond_wait_recursive(&_M_cond
,
377 mutex
->gthread_recursive_mutex())
379 __throw_concurrence_wait_error();
386 _GLIBCXX_END_NAMESPACE_VERSION