1 // Copyright (C) 2002, 2004, 2006, 2008, 2009, 2010, 2011
2 // Free Software Foundation, Inc.
4 // This file is part of GCC.
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)
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.
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.
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/>.
25 // Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com>
26 // Thread support written by Jason Merrill, Red Hat Inc. <jason@redhat.com>
28 #include <bits/c++config.h>
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)
38 # define _GLIBCXX_USE_FUTEX
39 # define _GLIBCXX_FUTEX_WAIT 0
40 # define _GLIBCXX_FUTEX_WAKE 1
43 // The IA64/generic ABI uses the first byte of the guard variable.
44 // The ARM EABI uses the least significant bit.
46 // Thread-safe static local initialization support.
48 # ifndef _GLIBCXX_USE_FUTEX
51 // A single mutex controlling all static initializations.
52 static __gnu_cxx::__recursive_mutex
* static_mutex
;
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
;
59 { static_mutex
= new (&fake_mutex
) __gnu_cxx::__recursive_mutex(); }
61 __gnu_cxx::__recursive_mutex
&
64 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
65 __gthread_once(&once
, init
);
69 // Simple wrapper for exception safety.
73 mutex_wrapper() : unlock(true)
74 { get_static_mutex().lock(); }
79 static_mutex
->unlock();
85 # if defined(__GTHREAD_HAS_COND) && !defined(_GLIBCXX_USE_FUTEX)
88 // A single conditional variable controlling all static initializations.
89 static __gnu_cxx::__cond
* static_cond
;
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
;
96 static void init_static_cond()
97 { static_cond
= new (&fake_cond
) __gnu_cxx::__cond(); }
102 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
103 __gthread_once(&once
, init_static_cond
);
109 # ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
111 __test_and_acquire (__cxxabiv1::__guard
*g
)
113 bool b
= _GLIBCXX_GUARD_TEST (g
);
114 _GLIBCXX_READ_MEM_BARRIER
;
117 # define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
120 # ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
122 __set_and_release (__cxxabiv1::__guard
*g
)
124 _GLIBCXX_WRITE_MEM_BARRIER
;
125 _GLIBCXX_GUARD_SET (g
);
127 # define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
130 #else /* !__GTHREADS */
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)
136 #endif /* __GTHREADS */
139 // Here are C++ run-time routines for guarded initiailization of static
140 // variables. There are 4 scenarios under which these routines are called:
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.
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.
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.
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.
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
174 // _GLIBCXX_GUARD_PENDING_BIT The guarded static var is being initialized
175 // and no other thread is waiting for its
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.
183 #ifdef _GLIBCXX_USE_FUTEX
186 static inline int __guard_test_bit (const int __byte
, const int __val
)
188 union { int __i
; char __c
[sizeof (int)]; } __u
= { 0 };
189 __u
.__c
[__byte
] = __val
;
196 init_in_progress_flag(__guard
* g
)
197 { return ((char *)g
)[1]; }
200 set_init_in_progress_flag(__guard
* g
, int v
)
201 { ((char *)g
)[1] = v
; }
204 throw_recursive_init_exception()
207 throw __gnu_cxx::recursive_init_error();
209 // Use __builtin_trap so we don't require abort().
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.
219 // Quit if the object is already initialized.
220 if (_GLIBCXX_GUARD_TEST(g
))
223 if (init_in_progress_flag(g
))
224 throw_recursive_init_exception();
226 set_init_in_progress_flag(g
, 1);
231 int __cxa_guard_acquire (__guard
*g
)
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
237 if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g
))
240 # ifdef _GLIBCXX_USE_FUTEX
241 // If __sync_* and futex syscall are supported, don't use any global
243 if (__gthread_active_p ())
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
;
252 int old
= __sync_val_compare_and_swap (gi
, 0, pending_bit
);
254 return 1; // This thread should do the initialization.
256 if (old
== guard_bit
)
257 return 0; // Already initialized.
259 if (old
== pending_bit
)
261 int newv
= old
| waiting_bit
;
262 if (__sync_val_compare_and_swap (gi
, old
, newv
) != old
)
268 syscall (SYS_futex
, gi
, _GLIBCXX_FUTEX_WAIT
, old
, 0);
272 if (__gthread_active_p ())
276 while (1) // When this loop is executing, mutex is locked.
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
283 if (init_in_progress_flag(g
))
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
289 get_static_cond().wait_recursive(&get_static_mutex());
293 set_init_in_progress_flag(g
, 1);
294 return 1; // The mutex will be unlocked via wrapper.
297 // This provides compatibility with older systems not supporting
298 // POSIX like conditional variables.
302 return 1; // The mutex still locked.
304 return 0; // The mutex will be unlocked via wrapper.
315 void __cxa_guard_abort (__guard
*g
) throw ()
317 #ifdef _GLIBCXX_USE_FUTEX
318 // If __sync_* and futex syscall are supported, don't use any global
320 if (__gthread_active_p ())
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);
326 if ((old
& waiting_bit
) != 0)
327 syscall (SYS_futex
, gi
, _GLIBCXX_FUTEX_WAKE
, INT_MAX
);
330 #elif defined(__GTHREAD_HAS_COND)
331 if (__gthread_active_p())
335 set_init_in_progress_flag(g
, 0);
337 // If we abort, we still need to wake up all other threads waiting for
338 // the conditional variable.
339 get_static_cond().broadcast();
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();
354 void __cxa_guard_release (__guard
*g
) throw ()
356 #ifdef _GLIBCXX_USE_FUTEX
357 // If __sync_* and futex syscall are supported, don't use any global
359 if (__gthread_active_p ())
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
);
366 if ((old
& waiting_bit
) != 0)
367 syscall (SYS_futex
, gi
, _GLIBCXX_FUTEX_WAKE
, INT_MAX
);
370 #elif defined(__GTHREAD_HAS_COND)
371 if (__gthread_active_p())
375 set_init_in_progress_flag(g
, 0);
376 _GLIBCXX_GUARD_SET_AND_RELEASE(g
);
378 get_static_cond().broadcast();
383 set_init_in_progress_flag(g
, 0);
384 _GLIBCXX_GUARD_SET_AND_RELEASE (g
);
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();