PR debug/45531
[official-gcc.git] / libstdc++-v3 / libsupc++ / guard.cc
blob222e5ec422764cb760cc5fbe37bd477ccb17b474
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.
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.
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.
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/>.
24 // Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com>
25 // Thread support written by Jason Merrill, Red Hat Inc. <jason@redhat.com>
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
42 // The IA64/generic ABI uses the first byte of the guard variable.
43 // The ARM EABI uses the least significant bit.
45 // Thread-safe static local initialization support.
46 #ifdef __GTHREADS
47 # ifndef _GLIBCXX_USE_FUTEX
48 namespace
50 // A single mutex controlling all static initializations.
51 static __gnu_cxx::__recursive_mutex* static_mutex;
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;
57 static void init()
58 { static_mutex = new (&fake_mutex) __gnu_cxx::__recursive_mutex(); }
60 __gnu_cxx::__recursive_mutex&
61 get_static_mutex()
63 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
64 __gthread_once(&once, init);
65 return *static_mutex;
68 // Simple wrapper for exception safety.
69 struct mutex_wrapper
71 bool unlock;
72 mutex_wrapper() : unlock(true)
73 { get_static_mutex().lock(); }
75 ~mutex_wrapper()
77 if (unlock)
78 static_mutex->unlock();
82 # endif
84 # if defined(__GTHREAD_HAS_COND) && !defined(_GLIBCXX_USE_FUTEX)
85 namespace
87 // A single conditional variable controlling all static initializations.
88 static __gnu_cxx::__cond* static_cond;
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;
95 static void init_static_cond()
96 { static_cond = new (&fake_cond) __gnu_cxx::__cond(); }
98 __gnu_cxx::__cond&
99 get_static_cond()
101 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
102 __gthread_once(&once, init_static_cond);
103 return *static_cond;
106 # endif
108 # ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
109 inline bool
110 __test_and_acquire (__cxxabiv1::__guard *g)
112 bool b = _GLIBCXX_GUARD_TEST (g);
113 _GLIBCXX_READ_MEM_BARRIER;
114 return b;
116 # define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
117 # endif
119 # ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
120 inline void
121 __set_and_release (__cxxabiv1::__guard *g)
123 _GLIBCXX_WRITE_MEM_BARRIER;
124 _GLIBCXX_GUARD_SET (g);
126 # define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
127 # endif
129 #else /* !__GTHREADS */
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)
135 #endif /* __GTHREADS */
137 namespace __gnu_cxx
139 recursive_init_error::~recursive_init_error() throw() { }
143 // Here are C++ run-time routines for guarded initiailization of static
144 // variables. There are 4 scenarios under which these routines are called:
146 // 1. Threads not supported (__GTHREADS not defined)
147 // 2. Threads are supported but not enabled at run-time.
148 // 3. Threads enabled at run-time but __gthreads_* are not fully POSIX.
149 // 4. Threads enabled at run-time and __gthreads_* support all POSIX threads
150 // primitives we need here.
152 // The old code supported scenarios 1-3 but was broken since it used a global
153 // mutex for all threads and had the mutex locked during the whole duration of
154 // initlization of a guarded static variable. The following created a dead-lock
155 // with the old code.
157 // Thread 1 acquires the global mutex.
158 // Thread 1 starts initializing static variable.
159 // Thread 1 creates thread 2 during initialization.
160 // Thread 2 attempts to acuqire mutex to initialize another variable.
161 // Thread 2 blocks since thread 1 is locking the mutex.
162 // Thread 1 waits for result from thread 2 and also blocks. A deadlock.
164 // The new code here can handle this situation and thus is more robust. Howere,
165 // we need to use the POSIX thread conditional variable, which is not supported
166 // in all platforms, notably older versions of Microsoft Windows. The gthr*.h
167 // headers define a symbol __GTHREAD_HAS_COND for platforms that support POSIX
168 // like conditional variables. For platforms that do not support conditional
169 // variables, we need to fall back to the old code.
171 // If _GLIBCXX_USE_FUTEX, no global mutex or conditional variable is used,
172 // only atomic operations are used together with futex syscall.
173 // Valid values of the first integer in guard are:
174 // 0 No thread encountered the guarded init
175 // yet or it has been aborted.
176 // _GLIBCXX_GUARD_BIT The guarded static var has been successfully
177 // initialized.
178 // _GLIBCXX_GUARD_PENDING_BIT The guarded static var is being initialized
179 // and no other thread is waiting for its
180 // initialization.
181 // (_GLIBCXX_GUARD_PENDING_BIT The guarded static var is being initialized
182 // | _GLIBCXX_GUARD_WAITING_BIT) and some other threads are waiting until
183 // it is initialized.
185 namespace __cxxabiv1
187 #ifdef _GLIBCXX_USE_FUTEX
188 namespace
190 static inline int __guard_test_bit (const int __byte, const int __val)
192 union { int __i; char __c[sizeof (int)]; } __u = { 0 };
193 __u.__c[__byte] = __val;
194 return __u.__i;
197 #endif
199 static inline int
200 init_in_progress_flag(__guard* g)
201 { return ((char *)g)[1]; }
203 static inline void
204 set_init_in_progress_flag(__guard* g, int v)
205 { ((char *)g)[1] = v; }
207 static inline void
208 throw_recursive_init_exception()
210 #ifdef __EXCEPTIONS
211 throw __gnu_cxx::recursive_init_error();
212 #else
213 // Use __builtin_trap so we don't require abort().
214 __builtin_trap();
215 #endif
218 // acuire() is a helper function used to acquire guard if thread support is
219 // not compiled in or is compiled in but not enabled at run-time.
220 static int
221 acquire(__guard *g)
223 // Quit if the object is already initialized.
224 if (_GLIBCXX_GUARD_TEST(g))
225 return 0;
227 if (init_in_progress_flag(g))
228 throw_recursive_init_exception();
230 set_init_in_progress_flag(g, 1);
231 return 1;
234 extern "C"
235 int __cxa_guard_acquire (__guard *g)
237 #ifdef __GTHREADS
238 // If the target can reorder loads, we need to insert a read memory
239 // barrier so that accesses to the guarded variable happen after the
240 // guard test.
241 if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g))
242 return 0;
244 # ifdef _GLIBCXX_USE_FUTEX
245 // If __sync_* and futex syscall are supported, don't use any global
246 // mutex.
247 if (__gthread_active_p ())
249 int *gi = (int *) (void *) g;
250 const int guard_bit = _GLIBCXX_GUARD_BIT;
251 const int pending_bit = _GLIBCXX_GUARD_PENDING_BIT;
252 const int waiting_bit = _GLIBCXX_GUARD_WAITING_BIT;
254 while (1)
256 int old = __sync_val_compare_and_swap (gi, 0, pending_bit);
257 if (old == 0)
258 return 1; // This thread should do the initialization.
260 if (old == guard_bit)
261 return 0; // Already initialized.
263 if (old == pending_bit)
265 int newv = old | waiting_bit;
266 if (__sync_val_compare_and_swap (gi, old, newv) != old)
267 continue;
269 old = newv;
272 syscall (SYS_futex, gi, _GLIBCXX_FUTEX_WAIT, old, 0);
275 # else
276 if (__gthread_active_p ())
278 mutex_wrapper mw;
280 while (1) // When this loop is executing, mutex is locked.
282 # ifdef __GTHREAD_HAS_COND
283 // The static is already initialized.
284 if (_GLIBCXX_GUARD_TEST(g))
285 return 0; // The mutex will be unlocked via wrapper
287 if (init_in_progress_flag(g))
289 // The guarded static is currently being initialized by
290 // another thread, so we release mutex and wait for the
291 // conditional variable. We will lock the mutex again after
292 // this.
293 get_static_cond().wait_recursive(&get_static_mutex());
295 else
297 set_init_in_progress_flag(g, 1);
298 return 1; // The mutex will be unlocked via wrapper.
300 # else
301 // This provides compatibility with older systems not supporting
302 // POSIX like conditional variables.
303 if (acquire(g))
305 mw.unlock = false;
306 return 1; // The mutex still locked.
308 return 0; // The mutex will be unlocked via wrapper.
309 # endif
312 # endif
313 #endif
315 return acquire (g);
318 extern "C"
319 void __cxa_guard_abort (__guard *g) throw ()
321 #ifdef _GLIBCXX_USE_FUTEX
322 // If __sync_* and futex syscall are supported, don't use any global
323 // mutex.
324 if (__gthread_active_p ())
326 int *gi = (int *) (void *) g;
327 const int waiting_bit = _GLIBCXX_GUARD_WAITING_BIT;
328 int old = __sync_lock_test_and_set (gi, 0);
330 if ((old & waiting_bit) != 0)
331 syscall (SYS_futex, gi, _GLIBCXX_FUTEX_WAKE, INT_MAX);
332 return;
334 #elif defined(__GTHREAD_HAS_COND)
335 if (__gthread_active_p())
337 mutex_wrapper mw;
339 set_init_in_progress_flag(g, 0);
341 // If we abort, we still need to wake up all other threads waiting for
342 // the conditional variable.
343 get_static_cond().broadcast();
344 return;
346 #endif
348 set_init_in_progress_flag(g, 0);
349 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
350 // This provides compatibility with older systems not supporting POSIX like
351 // conditional variables.
352 if (__gthread_active_p ())
353 static_mutex->unlock();
354 #endif
357 extern "C"
358 void __cxa_guard_release (__guard *g) throw ()
360 #ifdef _GLIBCXX_USE_FUTEX
361 // If __sync_* and futex syscall are supported, don't use any global
362 // mutex.
363 if (__gthread_active_p ())
365 int *gi = (int *) (void *) g;
366 const int guard_bit = _GLIBCXX_GUARD_BIT;
367 const int waiting_bit = _GLIBCXX_GUARD_WAITING_BIT;
368 int old = __sync_lock_test_and_set (gi, guard_bit);
370 if ((old & waiting_bit) != 0)
371 syscall (SYS_futex, gi, _GLIBCXX_FUTEX_WAKE, INT_MAX);
372 return;
374 #elif defined(__GTHREAD_HAS_COND)
375 if (__gthread_active_p())
377 mutex_wrapper mw;
379 set_init_in_progress_flag(g, 0);
380 _GLIBCXX_GUARD_SET_AND_RELEASE(g);
382 get_static_cond().broadcast();
383 return;
385 #endif
387 set_init_in_progress_flag(g, 0);
388 _GLIBCXX_GUARD_SET_AND_RELEASE (g);
390 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
391 // This provides compatibility with older systems not supporting POSIX like
392 // conditional variables.
393 if (__gthread_active_p())
394 static_mutex->unlock();
395 #endif