OSDN Git Service

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