OSDN Git Service

2011-02-09 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / guard.cc
1 // Copyright (C) 2002, 2004, 2006, 2008, 2009, 2010, 2011
2 // Free Software Foundation, Inc.
3 //  
4 // This file is part of GCC.
5 //
6 // GCC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // GCC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 // Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com>
26 // Thread support written by Jason Merrill, Red Hat Inc. <jason@redhat.com>
27
28 #include <bits/c++config.h>
29 #include <cxxabi.h>
30 #include <exception>
31 #include <new>
32 #include <ext/atomicity.h>
33 #include <ext/concurrence.h>
34 #if defined(__GTHREADS) && defined(__GTHREAD_HAS_COND) \
35     && defined(_GLIBCXX_ATOMIC_BUILTINS_4) && defined(_GLIBCXX_HAVE_LINUX_FUTEX)
36 # include <climits>
37 # include <syscall.h>
38 # define _GLIBCXX_USE_FUTEX
39 # define _GLIBCXX_FUTEX_WAIT 0
40 # define _GLIBCXX_FUTEX_WAKE 1
41 #endif
42
43 // The IA64/generic ABI uses the first byte of the guard variable.
44 // The ARM EABI uses the least significant bit.
45
46 // Thread-safe static local initialization support.
47 #ifdef __GTHREADS
48 # ifndef _GLIBCXX_USE_FUTEX
49 namespace
50 {
51   // A single mutex controlling all static initializations.
52   static __gnu_cxx::__recursive_mutex* static_mutex;  
53
54   typedef char fake_recursive_mutex[sizeof(__gnu_cxx::__recursive_mutex)]
55   __attribute__ ((aligned(__alignof__(__gnu_cxx::__recursive_mutex))));
56   fake_recursive_mutex fake_mutex;
57
58   static void init()
59   { static_mutex =  new (&fake_mutex) __gnu_cxx::__recursive_mutex(); }
60
61   __gnu_cxx::__recursive_mutex&
62   get_static_mutex()
63   {
64     static __gthread_once_t once = __GTHREAD_ONCE_INIT;
65     __gthread_once(&once, init);
66     return *static_mutex;
67   }
68
69   // Simple wrapper for exception safety.
70   struct mutex_wrapper
71   {
72     bool unlock;
73     mutex_wrapper() : unlock(true)
74     { get_static_mutex().lock(); }
75
76     ~mutex_wrapper()
77     {
78       if (unlock)
79         static_mutex->unlock();
80     }
81   };
82 }
83 # endif
84
85 # if defined(__GTHREAD_HAS_COND) && !defined(_GLIBCXX_USE_FUTEX)
86 namespace
87 {
88   // A single conditional variable controlling all static initializations.
89   static __gnu_cxx::__cond* static_cond;  
90
91   // using a fake type to avoid initializing a static class.
92   typedef char fake_cond_t[sizeof(__gnu_cxx::__cond)]
93   __attribute__ ((aligned(__alignof__(__gnu_cxx::__cond))));
94   fake_cond_t fake_cond;
95
96   static void init_static_cond()
97   { static_cond =  new (&fake_cond) __gnu_cxx::__cond(); }
98
99   __gnu_cxx::__cond&
100   get_static_cond()
101   {
102     static __gthread_once_t once = __GTHREAD_ONCE_INIT;
103     __gthread_once(&once, init_static_cond);
104     return *static_cond;
105   }
106 }
107 # endif
108
109 # ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
110 inline bool
111 __test_and_acquire (__cxxabiv1::__guard *g)
112 {
113   bool b = _GLIBCXX_GUARD_TEST (g);
114   _GLIBCXX_READ_MEM_BARRIER;
115   return b;
116 }
117 #  define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
118 # endif
119
120 # ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
121 inline void
122 __set_and_release (__cxxabiv1::__guard *g)
123 {
124   _GLIBCXX_WRITE_MEM_BARRIER;
125   _GLIBCXX_GUARD_SET (g);
126 }
127 #  define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
128 # endif
129
130 #else /* !__GTHREADS */
131
132 # undef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
133 # undef _GLIBCXX_GUARD_SET_AND_RELEASE
134 # define _GLIBCXX_GUARD_SET_AND_RELEASE(G) _GLIBCXX_GUARD_SET (G)
135
136 #endif /* __GTHREADS */
137
138 //
139 // Here are C++ run-time routines for guarded initiailization of static
140 // variables. There are 4 scenarios under which these routines are called:
141 //
142 //   1. Threads not supported (__GTHREADS not defined)
143 //   2. Threads are supported but not enabled at run-time.
144 //   3. Threads enabled at run-time but __gthreads_* are not fully POSIX.
145 //   4. Threads enabled at run-time and __gthreads_* support all POSIX threads
146 //      primitives we need here.
147 //
148 // The old code supported scenarios 1-3 but was broken since it used a global
149 // mutex for all threads and had the mutex locked during the whole duration of
150 // initlization of a guarded static variable. The following created a dead-lock
151 // with the old code.
152 //
153 //      Thread 1 acquires the global mutex.
154 //      Thread 1 starts initializing static variable.
155 //      Thread 1 creates thread 2 during initialization.
156 //      Thread 2 attempts to acuqire mutex to initialize another variable.
157 //      Thread 2 blocks since thread 1 is locking the mutex.
158 //      Thread 1 waits for result from thread 2 and also blocks. A deadlock.
159 //
160 // The new code here can handle this situation and thus is more robust. Howere,
161 // we need to use the POSIX thread conditional variable, which is not supported
162 // in all platforms, notably older versions of Microsoft Windows. The gthr*.h
163 // headers define a symbol __GTHREAD_HAS_COND for platforms that support POSIX
164 // like conditional variables. For platforms that do not support conditional
165 // variables, we need to fall back to the old code.
166
167 // If _GLIBCXX_USE_FUTEX, no global mutex or conditional variable is used,
168 // only atomic operations are used together with futex syscall.
169 // Valid values of the first integer in guard are:
170 // 0                              No thread encountered the guarded init
171 //                                yet or it has been aborted.
172 // _GLIBCXX_GUARD_BIT             The guarded static var has been successfully
173 //                                initialized.
174 // _GLIBCXX_GUARD_PENDING_BIT     The guarded static var is being initialized
175 //                                and no other thread is waiting for its
176 //                                initialization.
177 // (_GLIBCXX_GUARD_PENDING_BIT    The guarded static var is being initialized
178 //  | _GLIBCXX_GUARD_WAITING_BIT) and some other threads are waiting until
179 //                                it is initialized.
180
181 namespace __cxxabiv1 
182 {
183 #ifdef _GLIBCXX_USE_FUTEX
184   namespace
185   {
186     static inline int __guard_test_bit (const int __byte, const int __val)
187     {
188       union { int __i; char __c[sizeof (int)]; } __u = { 0 };
189       __u.__c[__byte] = __val;
190       return __u.__i;
191     }
192   }
193 #endif
194
195   static inline int
196   init_in_progress_flag(__guard* g)
197   { return ((char *)g)[1]; }
198
199   static inline void
200   set_init_in_progress_flag(__guard* g, int v)
201   { ((char *)g)[1] = v; }
202
203   static inline void
204   throw_recursive_init_exception()
205   {
206 #ifdef __EXCEPTIONS
207         throw __gnu_cxx::recursive_init_error();
208 #else
209         // Use __builtin_trap so we don't require abort().
210         __builtin_trap();
211 #endif
212   }
213
214   // acuire() is a helper function used to acquire guard if thread support is
215   // not compiled in or is compiled in but not enabled at run-time.
216   static int
217   acquire(__guard *g)
218   {
219     // Quit if the object is already initialized.
220     if (_GLIBCXX_GUARD_TEST(g))
221       return 0;
222
223     if (init_in_progress_flag(g))
224       throw_recursive_init_exception();
225
226     set_init_in_progress_flag(g, 1);
227     return 1;
228   }
229
230   extern "C"
231   int __cxa_guard_acquire (__guard *g) 
232   {
233 #ifdef __GTHREADS
234     // If the target can reorder loads, we need to insert a read memory
235     // barrier so that accesses to the guarded variable happen after the
236     // guard test.
237     if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g))
238       return 0;
239
240 # ifdef _GLIBCXX_USE_FUTEX
241     // If __sync_* and futex syscall are supported, don't use any global
242     // mutex.
243     if (__gthread_active_p ())
244       {
245         int *gi = (int *) (void *) g;
246         const int guard_bit = _GLIBCXX_GUARD_BIT;
247         const int pending_bit = _GLIBCXX_GUARD_PENDING_BIT;
248         const int waiting_bit = _GLIBCXX_GUARD_WAITING_BIT;
249
250         while (1)
251           {
252             int old = __sync_val_compare_and_swap (gi, 0, pending_bit);
253             if (old == 0)
254               return 1; // This thread should do the initialization.
255
256             if (old == guard_bit)
257               return 0; // Already initialized.
258
259             if (old == pending_bit)
260               {
261                 int newv = old | waiting_bit;
262                 if (__sync_val_compare_and_swap (gi, old, newv) != old)
263                   continue;
264
265                 old = newv;
266               }
267
268             syscall (SYS_futex, gi, _GLIBCXX_FUTEX_WAIT, old, 0);
269           }
270       }
271 # else
272     if (__gthread_active_p ())
273       {
274         mutex_wrapper mw;
275
276         while (1)       // When this loop is executing, mutex is locked.
277           {
278 #  ifdef __GTHREAD_HAS_COND
279             // The static is already initialized.
280             if (_GLIBCXX_GUARD_TEST(g))
281               return 0; // The mutex will be unlocked via wrapper
282
283             if (init_in_progress_flag(g))
284               {
285                 // The guarded static is currently being initialized by
286                 // another thread, so we release mutex and wait for the
287                 // conditional variable. We will lock the mutex again after
288                 // this.
289                 get_static_cond().wait_recursive(&get_static_mutex());
290               }
291             else
292               {
293                 set_init_in_progress_flag(g, 1);
294                 return 1; // The mutex will be unlocked via wrapper.
295               }
296 #  else
297             // This provides compatibility with older systems not supporting
298             // POSIX like conditional variables.
299             if (acquire(g))
300               {
301                 mw.unlock = false;
302                 return 1; // The mutex still locked.
303               }
304             return 0; // The mutex will be unlocked via wrapper.
305 #  endif
306           }
307       }
308 # endif
309 #endif
310
311     return acquire (g);
312   }
313
314   extern "C"
315   void __cxa_guard_abort (__guard *g) throw ()
316   {
317 #ifdef _GLIBCXX_USE_FUTEX
318     // If __sync_* and futex syscall are supported, don't use any global
319     // mutex.
320     if (__gthread_active_p ())
321       {
322         int *gi = (int *) (void *) g;
323         const int waiting_bit = _GLIBCXX_GUARD_WAITING_BIT;
324         int old = __sync_lock_test_and_set (gi, 0);
325
326         if ((old & waiting_bit) != 0)
327           syscall (SYS_futex, gi, _GLIBCXX_FUTEX_WAKE, INT_MAX);
328         return;
329       }
330 #elif defined(__GTHREAD_HAS_COND)
331     if (__gthread_active_p())
332       { 
333         mutex_wrapper mw;
334
335         set_init_in_progress_flag(g, 0);
336
337         // If we abort, we still need to wake up all other threads waiting for
338         // the conditional variable.
339         get_static_cond().broadcast();
340         return;
341       } 
342 #endif
343
344     set_init_in_progress_flag(g, 0);
345 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
346     // This provides compatibility with older systems not supporting POSIX like
347     // conditional variables.
348     if (__gthread_active_p ())
349       static_mutex->unlock();
350 #endif
351   }
352
353   extern "C"
354   void __cxa_guard_release (__guard *g) throw ()
355   {
356 #ifdef _GLIBCXX_USE_FUTEX
357     // If __sync_* and futex syscall are supported, don't use any global
358     // mutex.
359     if (__gthread_active_p ())
360       {
361         int *gi = (int *) (void *) g;
362         const int guard_bit = _GLIBCXX_GUARD_BIT;
363         const int waiting_bit = _GLIBCXX_GUARD_WAITING_BIT;
364         int old = __sync_lock_test_and_set (gi, guard_bit);
365
366         if ((old & waiting_bit) != 0)
367           syscall (SYS_futex, gi, _GLIBCXX_FUTEX_WAKE, INT_MAX);
368         return;
369       }
370 #elif defined(__GTHREAD_HAS_COND)
371     if (__gthread_active_p())
372       {
373         mutex_wrapper mw;
374
375         set_init_in_progress_flag(g, 0);
376         _GLIBCXX_GUARD_SET_AND_RELEASE(g);
377
378         get_static_cond().broadcast();
379         return;
380       } 
381 #endif
382
383     set_init_in_progress_flag(g, 0);
384     _GLIBCXX_GUARD_SET_AND_RELEASE (g);
385
386 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
387     // This provides compatibility with older systems not supporting POSIX like
388     // conditional variables.
389     if (__gthread_active_p())
390       static_mutex->unlock();
391 #endif
392   }
393 }