1 // Copyright (C) 2002, 2004, 2006, 2008, 2009 Free Software Foundation, Inc.
3 // This file is part of GCC.
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)
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>
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)
37 # define _GLIBCXX_USE_FUTEX
38 # define _GLIBCXX_FUTEX_WAIT 0
39 # define _GLIBCXX_FUTEX_WAKE 1
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.
47 # ifndef _GLIBCXX_USE_FUTEX
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
;
58 { static_mutex
= new (&fake_mutex
) __gnu_cxx::__recursive_mutex(); }
60 __gnu_cxx::__recursive_mutex
&
63 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
64 __gthread_once(&once
, init
);
68 // Simple wrapper for exception safety.
72 mutex_wrapper() : unlock(true)
73 { get_static_mutex().lock(); }
78 static_mutex
->unlock();
84 # if defined(__GTHREAD_HAS_COND) && !defined(_GLIBCXX_USE_FUTEX)
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(); }
101 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
102 __gthread_once(&once
, init_static_cond
);
108 # ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
110 __test_and_acquire (__cxxabiv1::__guard
*g
)
112 bool b
= _GLIBCXX_GUARD_TEST (g
);
113 _GLIBCXX_READ_MEM_BARRIER
;
116 # define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
119 # ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
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)
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 */
139 // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
140 // while the object is being initialized, the behavior is undefined.
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
149 recursive_init_error() throw() { }
150 virtual ~recursive_init_error() throw ();
153 recursive_init_error::~recursive_init_error() throw() { }
157 // Here are C++ run-time routines for guarded initiailization of static
158 // variables. There are 4 scenarios under which these routines are called:
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.
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.
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.
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.
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
192 // _GLIBCXX_GUARD_PENDING_BIT The guarded static var is being initialized
193 // and no other thread is waiting for its
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.
201 #ifdef _GLIBCXX_USE_FUTEX
204 static inline int __guard_test_bit (const int __byte
, const int __val
)
206 union { int __i
; char __c
[sizeof (int)]; } __u
= { 0 };
207 __u
.__c
[__byte
] = __val
;
214 init_in_progress_flag(__guard
* g
)
215 { return ((char *)g
)[1]; }
218 set_init_in_progress_flag(__guard
* g
, int v
)
219 { ((char *)g
)[1] = v
; }
222 throw_recursive_init_exception()
225 throw __gnu_cxx::recursive_init_error();
227 // Use __builtin_trap so we don't require abort().
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.
237 // Quit if the object is already initialized.
238 if (_GLIBCXX_GUARD_TEST(g
))
241 if (init_in_progress_flag(g
))
242 throw_recursive_init_exception();
244 set_init_in_progress_flag(g
, 1);
249 int __cxa_guard_acquire (__guard
*g
)
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
255 if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g
))
258 # ifdef _GLIBCXX_USE_FUTEX
259 // If __sync_* and futex syscall are supported, don't use any global
261 if (__gthread_active_p ())
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
;
270 int old
= __sync_val_compare_and_swap (gi
, 0, pending_bit
);
272 return 1; // This thread should do the initialization.
274 if (old
== guard_bit
)
275 return 0; // Already initialized.
277 if (old
== pending_bit
)
279 int newv
= old
| waiting_bit
;
280 if (__sync_val_compare_and_swap (gi
, old
, newv
) != old
)
286 syscall (SYS_futex
, gi
, _GLIBCXX_FUTEX_WAIT
, old
, 0);
290 if (__gthread_active_p ())
294 while (1) // When this loop is executing, mutex is locked.
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
301 if (init_in_progress_flag(g
))
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
307 get_static_cond().wait_recursive(&get_static_mutex());
311 set_init_in_progress_flag(g
, 1);
312 return 1; // The mutex will be unlocked via wrapper.
315 // This provides compatibility with older systems not supporting
316 // POSIX like conditional variables.
320 return 1; // The mutex still locked.
322 return 0; // The mutex will be unlocked via wrapper.
333 void __cxa_guard_abort (__guard
*g
) throw ()
335 #ifdef _GLIBCXX_USE_FUTEX
336 // If __sync_* and futex syscall are supported, don't use any global
338 if (__gthread_active_p ())
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);
344 if ((old
& waiting_bit
) != 0)
345 syscall (SYS_futex
, gi
, _GLIBCXX_FUTEX_WAKE
, INT_MAX
);
348 #elif defined(__GTHREAD_HAS_COND)
349 if (__gthread_active_p())
353 set_init_in_progress_flag(g
, 0);
355 // If we abort, we still need to wake up all other threads waiting for
356 // the conditional variable.
357 get_static_cond().broadcast();
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();
372 void __cxa_guard_release (__guard
*g
) throw ()
374 #ifdef _GLIBCXX_USE_FUTEX
375 // If __sync_* and futex syscall are supported, don't use any global
377 if (__gthread_active_p ())
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
);
384 if ((old
& waiting_bit
) != 0)
385 syscall (SYS_futex
, gi
, _GLIBCXX_FUTEX_WAKE
, INT_MAX
);
388 #elif defined(__GTHREAD_HAS_COND)
389 if (__gthread_active_p())
393 set_init_in_progress_flag(g
, 0);
394 _GLIBCXX_GUARD_SET_AND_RELEASE(g
);
396 get_static_cond().broadcast();
401 set_init_in_progress_flag(g
, 0);
402 _GLIBCXX_GUARD_SET_AND_RELEASE (g
);
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();