1 // Support for concurrent programing -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012
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 #if __GTHREADS && defined __GTHREAD_MUTEX_INIT
147 __gthread_mutex_t _M_mutex
= __GTHREAD_MUTEX_INIT
;
149 __gthread_mutex_t _M_mutex
;
152 __mutex(const __mutex
&);
153 __mutex
& operator=(const __mutex
&);
158 #if __GTHREADS && ! defined __GTHREAD_MUTEX_INIT
159 if (__gthread_active_p())
160 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex
);
164 #if __GTHREADS && ! defined __GTHREAD_MUTEX_INIT
167 if (__gthread_active_p())
168 __gthread_mutex_destroy(&_M_mutex
);
175 if (__gthread_active_p())
177 if (__gthread_mutex_lock(&_M_mutex
) != 0)
178 __throw_concurrence_lock_error();
186 if (__gthread_active_p())
188 if (__gthread_mutex_unlock(&_M_mutex
) != 0)
189 __throw_concurrence_unlock_error();
194 __gthread_mutex_t
* gthread_mutex(void)
195 { return &_M_mutex
; }
198 class __recursive_mutex
201 #if __GTHREADS && defined __GTHREAD_RECURSIVE_MUTEX_INIT
202 __gthread_recursive_mutex_t _M_mutex
= __GTHREAD_RECURSIVE_MUTEX_INIT
;
204 __gthread_recursive_mutex_t _M_mutex
;
207 __recursive_mutex(const __recursive_mutex
&);
208 __recursive_mutex
& operator=(const __recursive_mutex
&);
213 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT
214 if (__gthread_active_p())
215 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex
);
219 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT
222 if (__gthread_active_p())
223 _S_destroy(&_M_mutex
);
230 if (__gthread_active_p())
232 if (__gthread_recursive_mutex_lock(&_M_mutex
) != 0)
233 __throw_concurrence_lock_error();
241 if (__gthread_active_p())
243 if (__gthread_recursive_mutex_unlock(&_M_mutex
) != 0)
244 __throw_concurrence_unlock_error();
249 __gthread_recursive_mutex_t
* gthread_recursive_mutex(void)
250 { return &_M_mutex
; }
252 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT
253 // FIXME: gthreads doesn't define __gthread_recursive_mutex_destroy
254 // so we need to obtain a __gthread_mutex_t to destroy
256 template<typename _Mx
, typename _Rm
>
258 _S_destroy_win32(_Mx
* __mx
, _Rm
const* __rmx
)
260 __mx
->counter
= __rmx
->counter
;
261 __mx
->sema
= __rmx
->sema
;
262 __gthread_mutex_destroy(__mx
);
265 // matches a gthr-win32.h recursive mutex
266 template<typename _Rm
>
267 static typename __enable_if
<(bool)sizeof(&_Rm::sema
), void>::__type
268 _S_destroy(_Rm
* __mx
)
270 __gthread_mutex_t __tmp
;
271 _S_destroy_win32(&__tmp
, __mx
);
274 // matches a recursive mutex with a member 'actual'
275 template<typename _Rm
>
276 static typename __enable_if
<(bool)sizeof(&_Rm::actual
), void>::__type
277 _S_destroy(_Rm
* __mx
)
278 { __gthread_mutex_destroy(&__mx
->actual
); }
280 // matches when there's only one mutex type
281 template<typename _Rm
>
283 __enable_if
<std::__are_same
<_Rm
, __gthread_mutex_t
>::__value
,
285 _S_destroy(_Rm
* __mx
)
286 { __gthread_mutex_destroy(__mx
); }
290 /// Scoped lock idiom.
291 // Acquire the mutex here with a constructor call, then release with
292 // the destructor call in accordance with RAII style.
296 typedef __mutex __mutex_type
;
299 __mutex_type
& _M_device
;
301 __scoped_lock(const __scoped_lock
&);
302 __scoped_lock
& operator=(const __scoped_lock
&);
305 explicit __scoped_lock(__mutex_type
& __name
) : _M_device(__name
)
306 { _M_device
.lock(); }
308 ~__scoped_lock() throw()
309 { _M_device
.unlock(); }
312 #ifdef __GTHREAD_HAS_COND
316 #if __GTHREADS && defined __GTHREAD_COND_INIT
317 __gthread_cond_t _M_cond
= __GTHREAD_COND_INIT
;
319 __gthread_cond_t _M_cond
;
322 __cond(const __cond
&);
323 __cond
& operator=(const __cond
&);
328 #if __GTHREADS && ! defined __GTHREAD_COND_INIT
329 if (__gthread_active_p())
330 __GTHREAD_COND_INIT_FUNCTION(&_M_cond
);
334 #if __GTHREADS && ! defined __GTHREAD_COND_INIT
337 if (__gthread_active_p())
338 __gthread_cond_destroy(&_M_cond
);
345 if (__gthread_active_p())
347 if (__gthread_cond_broadcast(&_M_cond
) != 0)
348 __throw_concurrence_broadcast_error();
353 void wait(__mutex
*mutex
)
357 if (__gthread_cond_wait(&_M_cond
, mutex
->gthread_mutex()) != 0)
358 __throw_concurrence_wait_error();
363 void wait_recursive(__recursive_mutex
*mutex
)
367 if (__gthread_cond_wait_recursive(&_M_cond
,
368 mutex
->gthread_recursive_mutex())
370 __throw_concurrence_wait_error();
377 _GLIBCXX_END_NAMESPACE_VERSION