OSDN Git Service

2007-10-09 Zhou Drangon <drangon.mail@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / guard.cc
1 // Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
2 //  
3 // This file is part of GCC.
4 //
5 // GCC is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or (at your option)
8 // any later version.
9
10 // GCC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING.  If not, write to
17 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
18 // Boston, MA 02110-1301, USA. 
19
20 // As a special exception, you may use this file as part of a free software
21 // library without restriction.  Specifically, if other files instantiate
22 // templates or use macros or inline functions from this file, or you compile
23 // this file and link it with other files to produce an executable, this
24 // file does not by itself cause the resulting executable to be covered by
25 // the GNU General Public License.  This exception does not however
26 // invalidate any other reasons why the executable file might be covered by
27 // the GNU General Public License.
28
29 // Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com>
30 // Thread support written by Jason Merrill, Red Hat Inc. <jason@redhat.com>
31
32 #include <bits/c++config.h>
33 #include <cxxabi.h>
34 #include <exception>
35 #include <new>
36 #include <ext/atomicity.h>
37 #include <ext/concurrence.h>
38
39 // The IA64/generic ABI uses the first byte of the guard variable.
40 // The ARM EABI uses the least significant bit.
41
42 // Thread-safe static local initialization support.
43 #ifdef __GTHREADS
44 namespace
45 {
46   // A single mutex controlling all static initializations.
47   static __gnu_cxx::__recursive_mutex* static_mutex;  
48
49   typedef char fake_recursive_mutex[sizeof(__gnu_cxx::__recursive_mutex)]
50   __attribute__ ((aligned(__alignof__(__gnu_cxx::__recursive_mutex))));
51   fake_recursive_mutex fake_mutex;
52
53   static void init()
54   { static_mutex =  new (&fake_mutex) __gnu_cxx::__recursive_mutex(); }
55
56   __gnu_cxx::__recursive_mutex&
57   get_static_mutex()
58   {
59     static __gthread_once_t once = __GTHREAD_ONCE_INIT;
60     __gthread_once(&once, init);
61     return *static_mutex;
62   }
63 }
64
65 #ifdef __GTHREAD_HAS_COND
66 namespace
67 {
68   // A single conditional variable controlling all static initializations.
69   static __gnu_cxx::__cond* static_cond;  
70
71   // using a fake type to avoid initializing a static class.
72   typedef char fake_cond_t[sizeof(__gnu_cxx::__cond)]
73   __attribute__ ((aligned(__alignof__(__gnu_cxx::__cond))));
74   fake_cond_t fake_cond;
75
76   static void init_static_cond()
77   { static_cond =  new (&fake_cond) __gnu_cxx::__cond(); }
78
79   __gnu_cxx::__cond&
80   get_static_cond()
81   {
82     static __gthread_once_t once = __GTHREAD_ONCE_INIT;
83     __gthread_once(&once, init_static_cond);
84     return *static_cond;
85   }
86 }
87 #endif
88
89 #ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
90 inline bool
91 __test_and_acquire (__cxxabiv1::__guard *g)
92 {
93   bool b = _GLIBCXX_GUARD_TEST (g);
94   _GLIBCXX_READ_MEM_BARRIER;
95   return b;
96 }
97 #define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
98 #endif
99
100 #ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
101 inline void
102 __set_and_release (__cxxabiv1::__guard *g)
103 {
104   _GLIBCXX_WRITE_MEM_BARRIER;
105   _GLIBCXX_GUARD_SET (g);
106 }
107 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
108 #endif
109
110 #else /* !__GTHREADS */
111
112 #undef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
113 #undef _GLIBCXX_GUARD_SET_AND_RELEASE
114 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) _GLIBCXX_GUARD_SET (G)
115
116 #endif /* __GTHREADS */
117
118 namespace __gnu_cxx
119 {
120   // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
121   // while the object is being initialized, the behavior is undefined.
122
123   // Since we already have a library function to handle locking, we might
124   // as well check for this situation and throw an exception.
125   // We use the second byte of the guard variable to remember that we're
126   // in the middle of an initialization.
127   class recursive_init_error: public std::exception
128   {
129   public:
130     recursive_init_error() throw() { }
131     virtual ~recursive_init_error() throw ();
132   };
133
134   recursive_init_error::~recursive_init_error() throw() { }
135 }
136
137 //
138 // Here are C++ run-time routines for guarded initiailization of static
139 // variables. There are 4 scenarios under which these routines are called:
140 //
141 //   1. Threads not supported (__GTHREADS not defined)
142 //   2. Threads are supported but not enabled at run-time.
143 //   3. Threads enabled at run-time but __gthreads_* are not fully POSIX.
144 //   4. Threads enabled at run-time and __gthreads_* support all POSIX threads
145 //      primitives we need here.
146 //
147 // The old code supported scenarios 1-3 but was broken since it used a global
148 // mutex for all threads and had the mutex locked during the whole duration of
149 // initlization of a guarded static variable. The following created a dead-lock
150 // with the old code.
151 //
152 //      Thread 1 acquires the global mutex.
153 //      Thread 1 starts initializing static variable.
154 //      Thread 1 creates thread 2 during initialization.
155 //      Thread 2 attempts to acuqire mutex to initialize another variable.
156 //      Thread 2 blocks since thread 1 is locking the mutex.
157 //      Thread 1 waits for result from thread 2 and also blocks. A deadlock.
158 //
159 // The new code here can handle this situation and thus is more robust. Howere,
160 // we need to use the POSIX thread conditional variable, which is not supported
161 // in all platforms, notably older versions of Microsoft Windows. The gthr*.h
162 // headers define a symbol __GTHREAD_HAS_COND for platforms that support POSIX
163 // like conditional variables. For platforms that do not support conditional
164 // variables, we need to fall back to the old code.
165 namespace __cxxabiv1 
166 {
167   static inline int
168   init_in_progress_flag(__guard* g)
169   { return ((char *)g)[1]; }
170
171   static inline void
172   set_init_in_progress_flag(__guard* g, int v)
173   { ((char *)g)[1] = v; }
174
175   static inline void
176   throw_recursive_init_exception()
177   {
178 #ifdef __EXCEPTIONS
179         throw __gnu_cxx::recursive_init_error();
180 #else
181         // Use __builtin_trap so we don't require abort().
182         __builtin_trap();
183 #endif
184   }
185
186   // acuire() is a helper function used to acquire guard if thread support is
187   // not compiled in or is compiled in but not enabled at run-time.
188   static int
189   acquire(__guard *g)
190   {
191     // Quit if the object is already initialized.
192     if (_GLIBCXX_GUARD_TEST(g))
193       return 0;
194
195     if (init_in_progress_flag(g))
196         throw_recursive_init_exception();
197
198     set_init_in_progress_flag(g, 1);
199     return 1;
200   }
201
202   // Simple wrapper for exception safety.
203   struct mutex_wrapper
204   {
205 #ifdef __GTHREADS
206     bool unlock;
207     mutex_wrapper() : unlock(true)
208     { get_static_mutex().lock(); }
209
210     ~mutex_wrapper()
211     {
212       if (unlock)
213         static_mutex->unlock();
214     }
215 #endif
216   };
217
218   extern "C"
219   int __cxa_guard_acquire (__guard *g) 
220   {
221 #ifdef __GTHREADS
222     // If the target can reorder loads, we need to insert a read memory
223     // barrier so that accesses to the guarded variable happen after the
224     // guard test.
225     if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g))
226       return 0;
227
228     if (__gthread_active_p ())
229       {
230         mutex_wrapper mw;
231
232         while (1)       // When this loop is executing, mutex is locked.
233           {
234 #ifdef __GTHREAD_HAS_COND
235             // The static is allready initialized.
236             if (_GLIBCXX_GUARD_TEST(g))
237               return 0; // The mutex will be unlocked via wrapper
238
239             if (init_in_progress_flag(g))
240               {
241                 // The guarded static is currently being initialized by
242                 // another thread, so we release mutex and wait for the
243                 // conditional variable. We will lock the mutex again after
244                 // this.
245                 get_static_cond().wait_recursive(&get_static_mutex());
246               }
247             else
248               {
249                 set_init_in_progress_flag(g, 1);
250                 return 1; // The mutex will be unlocked via wrapper.
251               }
252 #else
253             // This provides compatibility with older systems not supporting
254             // POSIX like conditional variables.
255             if (acquire(g))
256               {
257                 mw.unlock = false;
258                 return 1; // The mutex still locked.
259               }
260             return 0; // The mutex will be unlocked via wrapper.
261 #endif
262           }
263       }
264 #endif
265
266     return acquire (g);
267   }
268
269   extern "C"
270   void __cxa_guard_abort (__guard *g)
271   {
272 #ifdef __GTHREAD_HAS_COND
273     if (__gthread_active_p())
274       { 
275         mutex_wrapper mw;
276
277         set_init_in_progress_flag(g, 0);
278
279         // If we abort, we still need to wake up all other threads waiting for
280         // the conditional variable.
281         get_static_cond().broadcast();
282         return;
283       } 
284 #endif
285
286     set_init_in_progress_flag(g, 0);
287 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
288     // This provides compatibility with older systems not supporting POSIX like
289     // conditional variables.
290     if (__gthread_active_p ())
291       static_mutex->unlock();
292 #endif
293   }
294
295   extern "C"
296   void __cxa_guard_release (__guard *g)
297   {
298 #ifdef __GTHREAD_HAS_COND
299     if (__gthread_active_p())
300       {
301         mutex_wrapper mw;
302
303         set_init_in_progress_flag(g, 0);
304         _GLIBCXX_GUARD_SET_AND_RELEASE(g);
305
306         get_static_cond().broadcast();
307         return;
308       } 
309 #endif
310
311     set_init_in_progress_flag(g, 0);
312     _GLIBCXX_GUARD_SET_AND_RELEASE (g);
313
314 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
315     // This provides compatibility with older systems not supporting POSIX like
316     // conditional variables.
317     if (__gthread_active_p())
318       static_mutex->unlock();
319 #endif
320   }
321 }