1 // Support for concurrent programing -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006
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
41 #include <bits/gthr.h>
42 #include <bits/functexcept.h>
44 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
46 // Available locking policies:
47 // _S_single single-threaded code that doesn't need to be locked.
48 // _S_mutex multi-threaded code that requires additional support
49 // from gthr.h or abstraction layers in concurrence.h.
50 // _S_atomic multi-threaded code using atomic operations.
51 enum _Lock_policy { _S_single, _S_mutex, _S_atomic };
53 // Compile time constant that indicates prefered locking policy in
54 // the current configuration.
55 static const _Lock_policy __default_lock_policy =
57 // NB: This macro doesn't actually exist yet in the compiler, but is
58 // set somewhat haphazardly at configure time.
59 #ifdef _GLIBCXX_ATOMIC_BUILTINS
68 // NB: As this is used in libsupc++, need to only depend on
69 // exception. No stdexception classes, no use of std::string.
70 class __concurrence_lock_error : public std::exception
75 { return "__gnu_cxx::__concurrence_lock_error"; }
78 class __concurrence_unlock_error : public std::exception
83 { return "__gnu_cxx::__concurrence_unlock_error"; }
86 // Substitute for concurrence_error object in the case of -fno-exceptions.
88 __throw_concurrence_lock_error()
91 throw __concurrence_lock_error();
98 __throw_concurrence_unlock_error()
101 throw __concurrence_unlock_error();
110 __gthread_mutex_t _M_mutex;
112 __mutex(const __mutex&);
113 __mutex& operator=(const __mutex&);
119 if (__gthread_active_p())
121 #if defined __GTHREAD_MUTEX_INIT
122 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
125 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
134 if (__gthread_active_p())
136 if (__gthread_mutex_lock(&_M_mutex) != 0)
137 __throw_concurrence_lock_error();
145 if (__gthread_active_p())
147 if (__gthread_mutex_unlock(&_M_mutex) != 0)
148 __throw_concurrence_unlock_error();
154 class __recursive_mutex
157 __gthread_recursive_mutex_t _M_mutex;
159 __recursive_mutex(const __recursive_mutex&);
160 __recursive_mutex& operator=(const __recursive_mutex&);
166 if (__gthread_active_p())
168 #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
169 __gthread_recursive_mutex_t __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
172 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
181 if (__gthread_active_p())
183 if (__gthread_recursive_mutex_lock(&_M_mutex) != 0)
184 __throw_concurrence_lock_error();
192 if (__gthread_active_p())
194 if (__gthread_recursive_mutex_unlock(&_M_mutex) != 0)
195 __throw_concurrence_unlock_error();
201 /// @brief Scoped lock idiom.
202 // Acquire the mutex here with a constructor call, then release with
203 // the destructor call in accordance with RAII style.
207 typedef __mutex __mutex_type;
210 __mutex_type& _M_device;
212 __scoped_lock(const __scoped_lock&);
213 __scoped_lock& operator=(const __scoped_lock&);
216 explicit __scoped_lock(__mutex_type& __name) : _M_device(__name)
217 { _M_device.lock(); }
219 ~__scoped_lock() throw()
220 { _M_device.unlock(); }
223 _GLIBCXX_END_NAMESPACE