OSDN Git Service

2006-09-13 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / concurrence.h
1 // Support for concurrent programing -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
11
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.
16
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,
20 // USA.
21
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.
30
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.
34  */
35
36 #ifndef _CONCURRENCE_H
37 #define _CONCURRENCE_H 1
38
39 #include <bits/gthr.h> 
40 #include <bits/functexcept.h>
41
42 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
43
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 concurrance.h.
48   // _S_atomic    multi-threaded code using atomic operations.
49   enum _Lock_policy { _S_single, _S_mutex, _S_atomic }; 
50
51   // Compile time constant that indicates prefered locking policy in
52   // the current configuration.
53   static const _Lock_policy __default_lock_policy = 
54 #ifdef __GTHREADS
55   // NB: This macro doesn't actually exist yet in the compiler, but is
56   // set somewhat haphazardly at configure time.
57 #ifdef _GLIBCXX_ATOMIC_BUILTINS
58   _S_atomic;
59 #else
60   _S_mutex;
61 #endif
62 #else
63   _S_single;
64 #endif
65
66   class __mutex 
67   {
68   private:
69     __gthread_mutex_t _M_mutex;
70
71     __mutex(const __mutex&);
72     __mutex& operator=(const __mutex&);
73
74   public:
75     __mutex() 
76     { 
77 #if __GTHREADS
78       if (__gthread_active_p())
79         {
80 #if defined __GTHREAD_MUTEX_INIT
81           __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
82           _M_mutex = __tmp;
83 #else
84           __GTHREAD_MUTEX_INIT_FUNCTION(_M_mutex); 
85 #endif
86         }
87 #endif 
88     }
89
90     void lock()
91     { 
92 #if __GTHREADS
93       if (__gthread_active_p())
94         {
95           if (__gthread_mutex_lock(&_M_mutex) != 0)
96             std::__throw_runtime_error("__mutex::lock");
97         }
98 #endif
99     }
100     
101     void unlock()
102     { 
103 #if __GTHREADS
104       if (__gthread_active_p())
105         {
106           if (__gthread_mutex_unlock(&_M_mutex) != 0)
107             std::__throw_runtime_error("__mutex::unlock");
108         }
109 #endif
110     }
111   };
112
113   class __recursive_mutex 
114   {
115   private:
116     __gthread_recursive_mutex_t _M_mutex;
117
118     __recursive_mutex(const __recursive_mutex&);
119     __recursive_mutex& operator=(const __recursive_mutex&);
120
121   public:
122     __recursive_mutex() 
123     { 
124 #if __GTHREADS
125       if (__gthread_active_p())
126         {
127 #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
128           __gthread_recursive_mutex_t __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
129           _M_mutex = __tmp;
130 #else
131           __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(_M_mutex); 
132 #endif
133         }
134 #endif 
135     }
136
137     void lock()
138     { 
139 #if __GTHREADS
140       if (__gthread_active_p())
141         {
142           if (__gthread_recursive_mutex_lock(&_M_mutex) != 0)
143             std::__throw_runtime_error("__recursive_mutex::lock");
144         }
145 #endif
146     }
147     
148     void unlock()
149     { 
150 #if __GTHREADS
151       if (__gthread_active_p())
152         {
153           if (__gthread_recursive_mutex_unlock(&_M_mutex) != 0)
154             std::__throw_runtime_error("__recursive_mutex::unlock");
155         }
156 #endif
157     }
158   };
159
160   /// @brief  Scoped lock idiom.
161   // Acquire the mutex here with a constructor call, then release with
162   // the destructor call in accordance with RAII style.
163   class __scoped_lock
164   {
165   public:
166     typedef __mutex mutex_type;
167
168   private:
169     mutex_type& _M_device;
170
171     __scoped_lock(const __scoped_lock&);
172     __scoped_lock& operator=(const __scoped_lock&);
173
174   public:
175     explicit __scoped_lock(mutex_type& __name) : _M_device(__name)
176     { _M_device.lock(); }
177
178     ~__scoped_lock() throw()
179     { _M_device.unlock(); }
180   };
181
182 _GLIBCXX_END_NAMESPACE
183
184 #endif