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 concurrence.h
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
31 #ifndef _CONCURRENCE_H
32 #define _CONCURRENCE_H 1
34 #pragma GCC system_header
37 #include <bits/gthr.h>
38 #include <bits/functexcept.h>
39 #include <bits/cpp_type_traits.h>
40 #include <ext/type_traits.h>
42 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
44 // Available locking policies:
45 // _S_single single-threaded code that doesn't need to be locked.
46 // _S_mutex multi-threaded code that requires additional support
47 // from gthr.h or abstraction layers in concurrence.h.
48 // _S_atomic multi-threaded code using atomic operations.
49 enum _Lock_policy { _S_single, _S_mutex, _S_atomic };
51 // Compile time constant that indicates prefered locking policy in
52 // the current configuration.
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))
65 // NB: As this is used in libsupc++, need to only depend on
66 // exception. No stdexception classes, no use of std::string.
67 class __concurrence_lock_error : public std::exception
72 { return "__gnu_cxx::__concurrence_lock_error"; }
75 class __concurrence_unlock_error : public std::exception
80 { return "__gnu_cxx::__concurrence_unlock_error"; }
83 class __concurrence_broadcast_error : public std::exception
88 { return "__gnu_cxx::__concurrence_broadcast_error"; }
91 class __concurrence_wait_error : public std::exception
96 { return "__gnu_cxx::__concurrence_wait_error"; }
99 // Substitute for concurrence_error object in the case of -fno-exceptions.
101 __throw_concurrence_lock_error()
104 throw __concurrence_lock_error();
111 __throw_concurrence_unlock_error()
114 throw __concurrence_unlock_error();
120 #ifdef __GTHREAD_HAS_COND
122 __throw_concurrence_broadcast_error()
125 throw __concurrence_broadcast_error();
132 __throw_concurrence_wait_error()
135 throw __concurrence_wait_error();
145 __gthread_mutex_t _M_mutex;
147 __mutex(const __mutex&);
148 __mutex& operator=(const __mutex&);
154 if (__gthread_active_p())
156 #if defined __GTHREAD_MUTEX_INIT
157 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
160 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
166 #if __GTHREADS && ! defined __GTHREAD_MUTEX_INIT
169 if (__gthread_active_p())
170 __gthread_mutex_destroy(&_M_mutex);
177 if (__gthread_active_p())
179 if (__gthread_mutex_lock(&_M_mutex) != 0)
180 __throw_concurrence_lock_error();
188 if (__gthread_active_p())
190 if (__gthread_mutex_unlock(&_M_mutex) != 0)
191 __throw_concurrence_unlock_error();
196 __gthread_mutex_t* gthread_mutex(void)
197 { return &_M_mutex; }
200 class __recursive_mutex
203 __gthread_recursive_mutex_t _M_mutex;
205 __recursive_mutex(const __recursive_mutex&);
206 __recursive_mutex& operator=(const __recursive_mutex&);
212 if (__gthread_active_p())
214 #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
215 __gthread_recursive_mutex_t __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
218 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
224 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT
227 if (__gthread_active_p())
228 _S_destroy(&_M_mutex);
235 if (__gthread_active_p())
237 if (__gthread_recursive_mutex_lock(&_M_mutex) != 0)
238 __throw_concurrence_lock_error();
246 if (__gthread_active_p())
248 if (__gthread_recursive_mutex_unlock(&_M_mutex) != 0)
249 __throw_concurrence_unlock_error();
254 __gthread_recursive_mutex_t* gthread_recursive_mutex(void)
255 { return &_M_mutex; }
257 #if __GTHREADS && ! defined __GTHREAD_RECURSIVE_MUTEX_INIT
258 // FIXME: gthreads doesn't define __gthread_recursive_mutex_destroy
259 // so we need to obtain a __gthread_mutex_t to destroy
261 template<typename _Mx, typename _Rm>
263 _S_destroy_win32(_Mx* __mx, _Rm const* __rmx)
265 __mx->counter = __rmx->counter;
266 __mx->sema = __rmx->sema;
267 __gthread_mutex_destroy(__mx);
270 // matches a gthr-win32.h recursive mutex
271 template<typename _Rm>
272 static typename __enable_if<sizeof(&_Rm::sema), void>::__type
273 _S_destroy(_Rm* __mx)
275 __gthread_mutex_t __tmp;
276 _S_destroy_win32(&__tmp, __mx);
279 // matches a recursive mutex with a member 'actual'
280 template<typename _Rm>
281 static typename __enable_if<sizeof(&_Rm::actual), void>::__type
282 _S_destroy(_Rm* __mx)
283 { __gthread_mutex_destroy(&__mx->actual); }
285 // matches when there's only one mutex type
286 template<typename _Rm>
288 __enable_if<std::__are_same<_Rm, __gthread_mutex_t>::__value,
290 _S_destroy(_Rm* __mx)
291 { __gthread_mutex_destroy(__mx); }
295 /// Scoped lock idiom.
296 // Acquire the mutex here with a constructor call, then release with
297 // the destructor call in accordance with RAII style.
301 typedef __mutex __mutex_type;
304 __mutex_type& _M_device;
306 __scoped_lock(const __scoped_lock&);
307 __scoped_lock& operator=(const __scoped_lock&);
310 explicit __scoped_lock(__mutex_type& __name) : _M_device(__name)
311 { _M_device.lock(); }
313 ~__scoped_lock() throw()
314 { _M_device.unlock(); }
317 #ifdef __GTHREAD_HAS_COND
321 __gthread_cond_t _M_cond;
323 __cond(const __cond&);
324 __cond& operator=(const __cond&);
330 if (__gthread_active_p())
332 #if defined __GTHREAD_COND_INIT
333 __gthread_cond_t __tmp = __GTHREAD_COND_INIT;
336 __GTHREAD_COND_INIT_FUNCTION(&_M_cond);
342 #if __GTHREADS && ! defined __GTHREAD_COND_INIT
345 if (__gthread_active_p())
346 __gthread_cond_destroy(&_M_cond);
353 if (__gthread_active_p())
355 if (__gthread_cond_broadcast(&_M_cond) != 0)
356 __throw_concurrence_broadcast_error();
361 void wait(__mutex *mutex)
365 if (__gthread_cond_wait(&_M_cond, mutex->gthread_mutex()) != 0)
366 __throw_concurrence_wait_error();
371 void wait_recursive(__recursive_mutex *mutex)
375 if (__gthread_cond_wait_recursive(&_M_cond,
376 mutex->gthread_recursive_mutex())
378 __throw_concurrence_wait_error();
385 _GLIBCXX_END_NAMESPACE