OSDN Git Service

2007-05-24 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / concurrence.h
1 // Support for concurrent programing -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007
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 <exception>
40 #include <bits/gthr.h> 
41 #include <bits/functexcept.h>
42
43 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
44
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 concurrance.h.
49   // _S_atomic    multi-threaded code using atomic operations.
50   enum _Lock_policy { _S_single, _S_mutex, _S_atomic }; 
51
52   // Compile time constant that indicates prefered locking policy in
53   // the current configuration.
54   static const _Lock_policy __default_lock_policy = 
55 #ifdef __GTHREADS
56 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) \
57      && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4))
58   _S_atomic;
59 #else
60   _S_mutex;
61 #endif
62 #else
63   _S_single;
64 #endif
65
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
69   {
70   public:
71     virtual char const*
72     what() const throw()
73     { return "__gnu_cxx::__concurrence_lock_error"; }
74   };
75
76   class __concurrence_unlock_error : public std::exception
77   {
78   public:
79     virtual char const*
80     what() const throw()
81     { return "__gnu_cxx::__concurrence_unlock_error"; }
82   };
83
84   // Substitute for concurrence_error object in the case of -fno-exceptions.
85   inline void
86   __throw_concurrence_lock_error()
87   {
88 #if __EXCEPTIONS
89     throw __concurrence_lock_error();
90 #else
91     __builtin_abort();
92 #endif
93   }
94
95   inline void
96   __throw_concurrence_unlock_error()
97   {
98 #if __EXCEPTIONS
99     throw __concurrence_unlock_error();
100 #else
101     __builtin_abort();
102 #endif
103   }
104
105   class __mutex 
106   {
107   private:
108     __gthread_mutex_t _M_mutex;
109
110     __mutex(const __mutex&);
111     __mutex& operator=(const __mutex&);
112
113   public:
114     __mutex() 
115     { 
116 #if __GTHREADS
117       if (__gthread_active_p())
118         {
119 #if defined __GTHREAD_MUTEX_INIT
120           __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
121           _M_mutex = __tmp;
122 #else
123           __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex); 
124 #endif
125         }
126 #endif 
127     }
128
129     void lock()
130     {
131 #if __GTHREADS
132       if (__gthread_active_p())
133         {
134           if (__gthread_mutex_lock(&_M_mutex) != 0)
135             __throw_concurrence_lock_error();
136         }
137 #endif
138     }
139     
140     void unlock()
141     {
142 #if __GTHREADS
143       if (__gthread_active_p())
144         {
145           if (__gthread_mutex_unlock(&_M_mutex) != 0)
146             __throw_concurrence_unlock_error();
147         }
148 #endif
149     }
150   };
151
152   class __recursive_mutex 
153   {
154   private:
155     __gthread_recursive_mutex_t _M_mutex;
156
157     __recursive_mutex(const __recursive_mutex&);
158     __recursive_mutex& operator=(const __recursive_mutex&);
159
160   public:
161     __recursive_mutex() 
162     { 
163 #if __GTHREADS
164       if (__gthread_active_p())
165         {
166 #if defined __GTHREAD_RECURSIVE_MUTEX_INIT
167           __gthread_recursive_mutex_t __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
168           _M_mutex = __tmp;
169 #else
170           __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex); 
171 #endif
172         }
173 #endif 
174     }
175
176     void lock()
177     { 
178 #if __GTHREADS
179       if (__gthread_active_p())
180         {
181           if (__gthread_recursive_mutex_lock(&_M_mutex) != 0)
182             __throw_concurrence_lock_error();
183         }
184 #endif
185     }
186     
187     void unlock()
188     { 
189 #if __GTHREADS
190       if (__gthread_active_p())
191         {
192           if (__gthread_recursive_mutex_unlock(&_M_mutex) != 0)
193             __throw_concurrence_unlock_error();
194         }
195 #endif
196     }
197   };
198
199   /// @brief  Scoped lock idiom.
200   // Acquire the mutex here with a constructor call, then release with
201   // the destructor call in accordance with RAII style.
202   class __scoped_lock
203   {
204   public:
205     typedef __mutex __mutex_type;
206
207   private:
208     __mutex_type& _M_device;
209
210     __scoped_lock(const __scoped_lock&);
211     __scoped_lock& operator=(const __scoped_lock&);
212
213   public:
214     explicit __scoped_lock(__mutex_type& __name) : _M_device(__name)
215     { _M_device.lock(); }
216
217     ~__scoped_lock() throw()
218     { _M_device.unlock(); }
219   };
220
221 _GLIBCXX_END_NAMESPACE
222
223 #endif