1 // Copyright (C) 2002-2015 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 && (ATOMIC_INT_LOCK_FREE > 1) && 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 condition 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 the guard variable with a memory load with
112 // acquire semantics.
115 __test_and_acquire (__cxxabiv1::__guard
*g
)
118 unsigned char *__p
= reinterpret_cast<unsigned char *>(g
);
119 __atomic_load (__p
, &__c
, __ATOMIC_ACQUIRE
);
120 return _GLIBCXX_GUARD_TEST(&__c
);
122 # define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
125 # ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
127 // Set the guard variable to 1 with memory order release semantics.
130 __set_and_release (__cxxabiv1::__guard
*g
)
132 unsigned char *__p
= reinterpret_cast<unsigned char *>(g
);
133 unsigned char val
= 1;
134 __atomic_store (__p
, &val
, __ATOMIC_RELEASE
);
136 # define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
139 #else /* !__GTHREADS */
141 # undef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
142 # undef _GLIBCXX_GUARD_SET_AND_RELEASE
143 # define _GLIBCXX_GUARD_SET_AND_RELEASE(G) _GLIBCXX_GUARD_SET (G)
145 #endif /* __GTHREADS */
148 // Here are C++ run-time routines for guarded initialization of static
149 // variables. There are 4 scenarios under which these routines are called:
151 // 1. Threads not supported (__GTHREADS not defined)
152 // 2. Threads are supported but not enabled at run-time.
153 // 3. Threads enabled at run-time but __gthreads_* are not fully POSIX.
154 // 4. Threads enabled at run-time and __gthreads_* support all POSIX threads
155 // primitives we need here.
157 // The old code supported scenarios 1-3 but was broken since it used a global
158 // mutex for all threads and had the mutex locked during the whole duration of
159 // initialization of a guarded static variable. The following created a
160 // dead-lock with the old code.
162 // Thread 1 acquires the global mutex.
163 // Thread 1 starts initializing static variable.
164 // Thread 1 creates thread 2 during initialization.
165 // Thread 2 attempts to acquire mutex to initialize another variable.
166 // Thread 2 blocks since thread 1 is locking the mutex.
167 // Thread 1 waits for result from thread 2 and also blocks. A deadlock.
169 // The new code here can handle this situation and thus is more robust. However,
170 // we need to use the POSIX thread condition variable, which is not supported
171 // in all platforms, notably older versions of Microsoft Windows. The gthr*.h
172 // headers define a symbol __GTHREAD_HAS_COND for platforms that support POSIX
173 // like condition variables. For platforms that do not support condition
174 // variables, we need to fall back to the old code.
176 // If _GLIBCXX_USE_FUTEX, no global mutex or condition variable is used,
177 // only atomic operations are used together with futex syscall.
178 // Valid values of the first integer in guard are:
179 // 0 No thread encountered the guarded init
180 // yet or it has been aborted.
181 // _GLIBCXX_GUARD_BIT The guarded static var has been successfully
183 // _GLIBCXX_GUARD_PENDING_BIT The guarded static var is being initialized
184 // and no other thread is waiting for its
186 // (_GLIBCXX_GUARD_PENDING_BIT The guarded static var is being initialized
187 // | _GLIBCXX_GUARD_WAITING_BIT) and some other threads are waiting until
188 // it is initialized.
192 #ifdef _GLIBCXX_USE_FUTEX
195 static inline int __guard_test_bit (const int __byte
, const int __val
)
197 union { int __i
; char __c
[sizeof (int)]; } __u
= { 0 };
198 __u
.__c
[__byte
] = __val
;
205 init_in_progress_flag(__guard
* g
)
206 { return ((char *)g
)[1]; }
209 set_init_in_progress_flag(__guard
* g
, int v
)
210 { ((char *)g
)[1] = v
; }
213 throw_recursive_init_exception()
216 throw __gnu_cxx::recursive_init_error();
218 // Use __builtin_trap so we don't require abort().
223 // acquire() is a helper function used to acquire guard if thread support is
224 // not compiled in or is compiled in but not enabled at run-time.
228 // Quit if the object is already initialized.
229 if (_GLIBCXX_GUARD_TEST(g
))
232 if (init_in_progress_flag(g
))
233 throw_recursive_init_exception();
235 set_init_in_progress_flag(g
, 1);
240 int __cxa_guard_acquire (__guard
*g
)
243 // If the target can reorder loads, we need to insert a read memory
244 // barrier so that accesses to the guarded variable happen after the
246 if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g
))
249 # ifdef _GLIBCXX_USE_FUTEX
250 // If __atomic_* and futex syscall are supported, don't use any global
252 if (__gthread_active_p ())
254 int *gi
= (int *) (void *) g
;
255 const int guard_bit
= _GLIBCXX_GUARD_BIT
;
256 const int pending_bit
= _GLIBCXX_GUARD_PENDING_BIT
;
257 const int waiting_bit
= _GLIBCXX_GUARD_WAITING_BIT
;
262 if (__atomic_compare_exchange_n(gi
, &expected
, pending_bit
, false,
266 // This thread should do the initialization.
270 if (expected
== guard_bit
)
272 // Already initialized.
276 if (expected
== pending_bit
)
279 int newv
= expected
| waiting_bit
;
280 if (!__atomic_compare_exchange_n(gi
, &expected
, newv
, false,
284 if (expected
== guard_bit
)
286 // Make a thread that failed to set the
287 // waiting bit exit the function earlier,
288 // if it detects that another thread has
289 // successfully finished initialising.
299 syscall (SYS_futex
, gi
, _GLIBCXX_FUTEX_WAIT
, expected
, 0);
303 if (__gthread_active_p ())
307 while (1) // When this loop is executing, mutex is locked.
309 # ifdef __GTHREAD_HAS_COND
310 // The static is already initialized.
311 if (_GLIBCXX_GUARD_TEST(g
))
312 return 0; // The mutex will be unlocked via wrapper
314 if (init_in_progress_flag(g
))
316 // The guarded static is currently being initialized by
317 // another thread, so we release mutex and wait for the
318 // condition variable. We will lock the mutex again after
320 get_static_cond().wait_recursive(&get_static_mutex());
324 set_init_in_progress_flag(g
, 1);
325 return 1; // The mutex will be unlocked via wrapper.
328 // This provides compatibility with older systems not supporting
329 // POSIX like condition variables.
333 return 1; // The mutex still locked.
335 return 0; // The mutex will be unlocked via wrapper.
346 void __cxa_guard_abort (__guard
*g
) throw ()
348 #ifdef _GLIBCXX_USE_FUTEX
349 // If __atomic_* and futex syscall are supported, don't use any global
351 if (__gthread_active_p ())
353 int *gi
= (int *) (void *) g
;
354 const int waiting_bit
= _GLIBCXX_GUARD_WAITING_BIT
;
355 int old
= __atomic_exchange_n (gi
, 0, __ATOMIC_ACQ_REL
);
357 if ((old
& waiting_bit
) != 0)
358 syscall (SYS_futex
, gi
, _GLIBCXX_FUTEX_WAKE
, INT_MAX
);
361 #elif defined(__GTHREAD_HAS_COND)
362 if (__gthread_active_p())
366 set_init_in_progress_flag(g
, 0);
368 // If we abort, we still need to wake up all other threads waiting for
369 // the condition variable.
370 get_static_cond().broadcast();
375 set_init_in_progress_flag(g
, 0);
376 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
377 // This provides compatibility with older systems not supporting POSIX like
378 // condition variables.
379 if (__gthread_active_p ())
380 static_mutex
->unlock();
385 void __cxa_guard_release (__guard
*g
) throw ()
387 #ifdef _GLIBCXX_USE_FUTEX
388 // If __atomic_* and futex syscall are supported, don't use any global
390 if (__gthread_active_p ())
392 int *gi
= (int *) (void *) g
;
393 const int guard_bit
= _GLIBCXX_GUARD_BIT
;
394 const int waiting_bit
= _GLIBCXX_GUARD_WAITING_BIT
;
395 int old
= __atomic_exchange_n (gi
, guard_bit
, __ATOMIC_ACQ_REL
);
397 if ((old
& waiting_bit
) != 0)
398 syscall (SYS_futex
, gi
, _GLIBCXX_FUTEX_WAKE
, INT_MAX
);
401 #elif defined(__GTHREAD_HAS_COND)
402 if (__gthread_active_p())
406 set_init_in_progress_flag(g
, 0);
407 _GLIBCXX_GUARD_SET_AND_RELEASE(g
);
409 get_static_cond().broadcast();
414 set_init_in_progress_flag(g
, 0);
415 _GLIBCXX_GUARD_SET_AND_RELEASE (g
);
417 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
418 // This provides compatibility with older systems not supporting POSIX like
419 // condition variables.
420 if (__gthread_active_p())
421 static_mutex
->unlock();