1 // Support for concurrent programing -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file concurrence.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
36 #ifndef _CONCURRENCE_H
37 #define _CONCURRENCE_H 1
40 #include <bits/gthr.h>
41 #include <bits/functexcept.h>
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
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
);
170 if (__gthread_active_p())
172 if (__gthread_mutex_lock(&_M_mutex
) != 0)
173 __throw_concurrence_lock_error();
181 if (__gthread_active_p())
183 if (__gthread_mutex_unlock(&_M_mutex
) != 0)
184 __throw_concurrence_unlock_error();
189 __gthread_mutex_t
* gthread_mutex(void)
190 { return &_M_mutex
; }
193 class __recursive_mutex
196 __gthread_recursive_mutex_t _M_mutex
;
198 __recursive_mutex(const __recursive_mutex
&);
199 __recursive_mutex
& operator=(const __recursive_mutex
&);
205 if (__gthread_active_p())
207 #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
208 __gthread_recursive_mutex_t __tmp
= __GTHREAD_RECURSIVE_MUTEX_INIT
;
211 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex
);
220 if (__gthread_active_p())
222 if (__gthread_recursive_mutex_lock(&_M_mutex
) != 0)
223 __throw_concurrence_lock_error();
231 if (__gthread_active_p())
233 if (__gthread_recursive_mutex_unlock(&_M_mutex
) != 0)
234 __throw_concurrence_unlock_error();
239 __gthread_recursive_mutex_t
* gthread_recursive_mutex(void)
240 { return &_M_mutex
; }
243 /// @brief Scoped lock idiom.
244 // Acquire the mutex here with a constructor call, then release with
245 // the destructor call in accordance with RAII style.
249 typedef __mutex __mutex_type
;
252 __mutex_type
& _M_device
;
254 __scoped_lock(const __scoped_lock
&);
255 __scoped_lock
& operator=(const __scoped_lock
&);
258 explicit __scoped_lock(__mutex_type
& __name
) : _M_device(__name
)
259 { _M_device
.lock(); }
261 ~__scoped_lock() throw()
262 { _M_device
.unlock(); }
265 #ifdef __GTHREAD_HAS_COND
269 __gthread_cond_t _M_cond
;
271 __cond(const __cond
&);
272 __cond
& operator=(const __cond
&);
278 if (__gthread_active_p())
280 #if defined __GTHREAD_COND_INIT
281 __gthread_cond_t __tmp
= __GTHREAD_COND_INIT
;
284 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_cond
);
293 if (__gthread_active_p())
295 if (__gthread_cond_broadcast(&_M_cond
) != 0)
296 __throw_concurrence_broadcast_error();
301 void wait(__mutex
*mutex
)
305 if (__gthread_cond_wait(&_M_cond
, mutex
->gthread_mutex()) != 0)
306 __throw_concurrence_wait_error();
311 void wait_recursive(__recursive_mutex
*mutex
)
315 if (__gthread_cond_wait_recursive(&_M_cond
,
316 mutex
->gthread_recursive_mutex())
318 __throw_concurrence_wait_error();
325 _GLIBCXX_END_NAMESPACE