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>
40 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
42 // Available locking policies:
43 // _S_single single-threaded code that doesn't need to be locked.
44 // _S_mutex multi-threaded code that requires additional support
45 // from gthr.h or abstraction layers in concurrence.h.
46 // _S_atomic multi-threaded code using atomic operations.
47 enum _Lock_policy
{ _S_single
, _S_mutex
, _S_atomic
};
49 // Compile time constant that indicates prefered locking policy in
50 // the current configuration.
51 static const _Lock_policy __default_lock_policy
=
53 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) \
54 && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4))
63 // NB: As this is used in libsupc++, need to only depend on
64 // exception. No stdexception classes, no use of std::string.
65 class __concurrence_lock_error
: public std::exception
70 { return "__gnu_cxx::__concurrence_lock_error"; }
73 class __concurrence_unlock_error
: public std::exception
78 { return "__gnu_cxx::__concurrence_unlock_error"; }
81 class __concurrence_broadcast_error
: public std::exception
86 { return "__gnu_cxx::__concurrence_broadcast_error"; }
89 class __concurrence_wait_error
: public std::exception
94 { return "__gnu_cxx::__concurrence_wait_error"; }
97 // Substitute for concurrence_error object in the case of -fno-exceptions.
99 __throw_concurrence_lock_error()
102 throw __concurrence_lock_error();
109 __throw_concurrence_unlock_error()
112 throw __concurrence_unlock_error();
118 #ifdef __GTHREAD_HAS_COND
120 __throw_concurrence_broadcast_error()
123 throw __concurrence_broadcast_error();
130 __throw_concurrence_wait_error()
133 throw __concurrence_wait_error();
143 __gthread_mutex_t _M_mutex
;
145 __mutex(const __mutex
&);
146 __mutex
& operator=(const __mutex
&);
152 if (__gthread_active_p())
154 #if defined __GTHREAD_MUTEX_INIT
155 __gthread_mutex_t __tmp
= __GTHREAD_MUTEX_INIT
;
158 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex
);
167 if (__gthread_active_p())
169 if (__gthread_mutex_lock(&_M_mutex
) != 0)
170 __throw_concurrence_lock_error();
178 if (__gthread_active_p())
180 if (__gthread_mutex_unlock(&_M_mutex
) != 0)
181 __throw_concurrence_unlock_error();
186 __gthread_mutex_t
* gthread_mutex(void)
187 { return &_M_mutex
; }
190 class __recursive_mutex
193 __gthread_recursive_mutex_t _M_mutex
;
195 __recursive_mutex(const __recursive_mutex
&);
196 __recursive_mutex
& operator=(const __recursive_mutex
&);
202 if (__gthread_active_p())
204 #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
205 __gthread_recursive_mutex_t __tmp
= __GTHREAD_RECURSIVE_MUTEX_INIT
;
208 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex
);
217 if (__gthread_active_p())
219 if (__gthread_recursive_mutex_lock(&_M_mutex
) != 0)
220 __throw_concurrence_lock_error();
228 if (__gthread_active_p())
230 if (__gthread_recursive_mutex_unlock(&_M_mutex
) != 0)
231 __throw_concurrence_unlock_error();
236 __gthread_recursive_mutex_t
* gthread_recursive_mutex(void)
237 { return &_M_mutex
; }
240 /// Scoped lock idiom.
241 // Acquire the mutex here with a constructor call, then release with
242 // the destructor call in accordance with RAII style.
246 typedef __mutex __mutex_type
;
249 __mutex_type
& _M_device
;
251 __scoped_lock(const __scoped_lock
&);
252 __scoped_lock
& operator=(const __scoped_lock
&);
255 explicit __scoped_lock(__mutex_type
& __name
) : _M_device(__name
)
256 { _M_device
.lock(); }
258 ~__scoped_lock() throw()
259 { _M_device
.unlock(); }
262 #ifdef __GTHREAD_HAS_COND
266 __gthread_cond_t _M_cond
;
268 __cond(const __cond
&);
269 __cond
& operator=(const __cond
&);
275 if (__gthread_active_p())
277 #if defined __GTHREAD_COND_INIT
278 __gthread_cond_t __tmp
= __GTHREAD_COND_INIT
;
281 __GTHREAD_COND_INIT_FUNCTION(&_M_cond
);
290 if (__gthread_active_p())
292 if (__gthread_cond_broadcast(&_M_cond
) != 0)
293 __throw_concurrence_broadcast_error();
298 void wait(__mutex
*mutex
)
302 if (__gthread_cond_wait(&_M_cond
, mutex
->gthread_mutex()) != 0)
303 __throw_concurrence_wait_error();
308 void wait_recursive(__recursive_mutex
*mutex
)
312 if (__gthread_cond_wait_recursive(&_M_cond
,
313 mutex
->gthread_recursive_mutex())
315 __throw_concurrence_wait_error();
322 _GLIBCXX_END_NAMESPACE