1 // Support for concurrent programing -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009
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
35 #include <bits/gthr.h>
36 #include <bits/functexcept.h>
38 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
40 // Available locking policies:
41 // _S_single single-threaded code that doesn't need to be locked.
42 // _S_mutex multi-threaded code that requires additional support
43 // from gthr.h or abstraction layers in concurrence.h.
44 // _S_atomic multi-threaded code using atomic operations.
45 enum _Lock_policy
{ _S_single
, _S_mutex
, _S_atomic
};
47 // Compile time constant that indicates prefered locking policy in
48 // the current configuration.
49 static const _Lock_policy __default_lock_policy
=
51 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) \
52 && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4))
61 // NB: As this is used in libsupc++, need to only depend on
62 // exception. No stdexception classes, no use of std::string.
63 class __concurrence_lock_error
: public std::exception
68 { return "__gnu_cxx::__concurrence_lock_error"; }
71 class __concurrence_unlock_error
: public std::exception
76 { return "__gnu_cxx::__concurrence_unlock_error"; }
79 class __concurrence_broadcast_error
: public std::exception
84 { return "__gnu_cxx::__concurrence_broadcast_error"; }
87 class __concurrence_wait_error
: public std::exception
92 { return "__gnu_cxx::__concurrence_wait_error"; }
95 // Substitute for concurrence_error object in the case of -fno-exceptions.
97 __throw_concurrence_lock_error()
100 throw __concurrence_lock_error();
107 __throw_concurrence_unlock_error()
110 throw __concurrence_unlock_error();
116 #ifdef __GTHREAD_HAS_COND
118 __throw_concurrence_broadcast_error()
121 throw __concurrence_broadcast_error();
128 __throw_concurrence_wait_error()
131 throw __concurrence_wait_error();
141 __gthread_mutex_t _M_mutex
;
143 __mutex(const __mutex
&);
144 __mutex
& operator=(const __mutex
&);
150 if (__gthread_active_p())
152 #if defined __GTHREAD_MUTEX_INIT
153 __gthread_mutex_t __tmp
= __GTHREAD_MUTEX_INIT
;
156 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex
);
165 if (__gthread_active_p())
167 if (__gthread_mutex_lock(&_M_mutex
) != 0)
168 __throw_concurrence_lock_error();
176 if (__gthread_active_p())
178 if (__gthread_mutex_unlock(&_M_mutex
) != 0)
179 __throw_concurrence_unlock_error();
184 __gthread_mutex_t
* gthread_mutex(void)
185 { return &_M_mutex
; }
188 class __recursive_mutex
191 __gthread_recursive_mutex_t _M_mutex
;
193 __recursive_mutex(const __recursive_mutex
&);
194 __recursive_mutex
& operator=(const __recursive_mutex
&);
200 if (__gthread_active_p())
202 #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
203 __gthread_recursive_mutex_t __tmp
= __GTHREAD_RECURSIVE_MUTEX_INIT
;
206 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex
);
215 if (__gthread_active_p())
217 if (__gthread_recursive_mutex_lock(&_M_mutex
) != 0)
218 __throw_concurrence_lock_error();
226 if (__gthread_active_p())
228 if (__gthread_recursive_mutex_unlock(&_M_mutex
) != 0)
229 __throw_concurrence_unlock_error();
234 __gthread_recursive_mutex_t
* gthread_recursive_mutex(void)
235 { return &_M_mutex
; }
238 /// Scoped lock idiom.
239 // Acquire the mutex here with a constructor call, then release with
240 // the destructor call in accordance with RAII style.
244 typedef __mutex __mutex_type
;
247 __mutex_type
& _M_device
;
249 __scoped_lock(const __scoped_lock
&);
250 __scoped_lock
& operator=(const __scoped_lock
&);
253 explicit __scoped_lock(__mutex_type
& __name
) : _M_device(__name
)
254 { _M_device
.lock(); }
256 ~__scoped_lock() throw()
257 { _M_device
.unlock(); }
260 #ifdef __GTHREAD_HAS_COND
264 __gthread_cond_t _M_cond
;
266 __cond(const __cond
&);
267 __cond
& operator=(const __cond
&);
273 if (__gthread_active_p())
275 #if defined __GTHREAD_COND_INIT
276 __gthread_cond_t __tmp
= __GTHREAD_COND_INIT
;
279 __GTHREAD_COND_INIT_FUNCTION(&_M_cond
);
288 if (__gthread_active_p())
290 if (__gthread_cond_broadcast(&_M_cond
) != 0)
291 __throw_concurrence_broadcast_error();
296 void wait(__mutex
*mutex
)
300 if (__gthread_cond_wait(&_M_cond
, mutex
->gthread_mutex()) != 0)
301 __throw_concurrence_wait_error();
306 void wait_recursive(__recursive_mutex
*mutex
)
310 if (__gthread_cond_wait_recursive(&_M_cond
,
311 mutex
->gthread_recursive_mutex())
313 __throw_concurrence_wait_error();
320 _GLIBCXX_END_NAMESPACE