1 // Copyright (C) 2002, 2004, 2006 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 2, 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 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING. If not, write to
17 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
18 // Boston, MA 02110-1301, USA.
20 // As a special exception, you may use this file as part of a free software
21 // library without restriction. Specifically, if other files instantiate
22 // templates or use macros or inline functions from this file, or you compile
23 // this file and link it with other files to produce an executable, this
24 // file does not by itself cause the resulting executable to be covered by
25 // the GNU General Public License. This exception does not however
26 // invalidate any other reasons why the executable file might be covered by
27 // the GNU General Public License.
29 // Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com>
30 // Thread support written by Jason Merrill, Red Hat Inc. <jason@redhat.com>
32 #include <bits/c++config.h>
36 #include <ext/atomicity.h>
37 #include <ext/concurrence.h>
39 // The IA64/generic ABI uses the first byte of the guard variable.
40 // The ARM EABI uses the least significant bit.
42 // Thread-safe static local initialization support.
46 // A single mutex controlling all static initializations.
47 static __gnu_cxx::__recursive_mutex* static_mutex;
49 typedef char fake_recursive_mutex[sizeof(__gnu_cxx::__recursive_mutex)]
50 __attribute__ ((aligned(__alignof__(__gnu_cxx::__recursive_mutex))));
51 fake_recursive_mutex fake_mutex;
54 { static_mutex = new (&fake_mutex) __gnu_cxx::__recursive_mutex(); }
56 __gnu_cxx::__recursive_mutex&
59 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
60 __gthread_once(&once, init);
65 #ifdef __GTHREAD_HAS_COND
68 // A single conditional variable controlling all static initializations.
69 static __gnu_cxx::__cond* static_cond;
71 // using a fake type to avoid initializing a static class.
72 typedef char fake_cond_t[sizeof(__gnu_cxx::__cond)]
73 __attribute__ ((aligned(__alignof__(__gnu_cxx::__cond))));
74 fake_cond_t fake_cond;
76 static void init_static_cond()
77 { static_cond = new (&fake_cond) __gnu_cxx::__cond(); }
82 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
83 __gthread_once(&once, init_static_cond);
89 #ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
91 __test_and_acquire (__cxxabiv1::__guard *g)
93 bool b = _GLIBCXX_GUARD_TEST (g);
94 _GLIBCXX_READ_MEM_BARRIER;
97 #define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
100 #ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
102 __set_and_release (__cxxabiv1::__guard *g)
104 _GLIBCXX_WRITE_MEM_BARRIER;
105 _GLIBCXX_GUARD_SET (g);
107 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
110 #else /* !__GTHREADS */
112 #undef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
113 #undef _GLIBCXX_GUARD_SET_AND_RELEASE
114 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) _GLIBCXX_GUARD_SET (G)
116 #endif /* __GTHREADS */
120 // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
121 // while the object is being initialized, the behavior is undefined.
123 // Since we already have a library function to handle locking, we might
124 // as well check for this situation and throw an exception.
125 // We use the second byte of the guard variable to remember that we're
126 // in the middle of an initialization.
127 class recursive_init_error: public std::exception
130 recursive_init_error() throw() { }
131 virtual ~recursive_init_error() throw ();
134 recursive_init_error::~recursive_init_error() throw() { }
138 // Here are C++ run-time routines for guarded initiailization of static
139 // variables. There are 4 scenarios under which these routines are called:
141 // 1. Threads not supported (__GTHREADS not defined)
142 // 2. Threads are supported but not enabled at run-time.
143 // 3. Threads enabled at run-time but __gthreads_* are not fully POSIX.
144 // 4. Threads enabled at run-time and __gthreads_* support all POSIX threads
145 // primitives we need here.
147 // The old code supported scenarios 1-3 but was broken since it used a global
148 // mutex for all threads and had the mutex locked during the whole duration of
149 // initlization of a guarded static variable. The following created a dead-lock
150 // with the old code.
152 // Thread 1 acquires the global mutex.
153 // Thread 1 starts initializing static variable.
154 // Thread 1 creates thread 2 during initialization.
155 // Thread 2 attempts to acuqire mutex to initialize another variable.
156 // Thread 2 blocks since thread 1 is locking the mutex.
157 // Thread 1 waits for result from thread 2 and also blocks. A deadlock.
159 // The new code here can handle this situation and thus is more robust. Howere,
160 // we need to use the POSIX thread conditional variable, which is not supported
161 // in all platforms, notably older versions of Microsoft Windows. The gthr*.h
162 // headers define a symbol __GTHREAD_HAS_COND for platforms that support POSIX
163 // like conditional variables. For platforms that do not support conditional
164 // variables, we need to fall back to the old code.
168 init_in_progress_flag(__guard* g)
169 { return ((char *)g)[1]; }
172 set_init_in_progress_flag(__guard* g, int v)
173 { ((char *)g)[1] = v; }
176 throw_recursive_init_exception()
179 throw __gnu_cxx::recursive_init_error();
181 // Use __builtin_trap so we don't require abort().
186 // acuire() is a helper function used to acquire guard if thread support is
187 // not compiled in or is compiled in but not enabled at run-time.
191 // Quit if the object is already initialized.
192 if (_GLIBCXX_GUARD_TEST(g))
195 if (init_in_progress_flag(g))
196 throw_recursive_init_exception();
198 set_init_in_progress_flag(g, 1);
202 // Simple wrapper for exception safety.
207 mutex_wrapper() : unlock(true)
208 { get_static_mutex().lock(); }
213 static_mutex->unlock();
219 int __cxa_guard_acquire (__guard *g)
222 // If the target can reorder loads, we need to insert a read memory
223 // barrier so that accesses to the guarded variable happen after the
225 if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g))
228 if (__gthread_active_p ())
232 while (1) // When this loop is executing, mutex is locked.
234 #ifdef __GTHREAD_HAS_COND
235 // The static is allready initialized.
236 if (_GLIBCXX_GUARD_TEST(g))
237 return 0; // The mutex will be unlocked via wrapper
239 if (init_in_progress_flag(g))
241 // The guarded static is currently being initialized by
242 // another thread, so we release mutex and wait for the
243 // conditional variable. We will lock the mutex again after
245 get_static_cond().wait_recursive(&get_static_mutex());
249 set_init_in_progress_flag(g, 1);
250 return 1; // The mutex will be unlocked via wrapper.
253 // This provides compatibility with older systems not supporting
254 // POSIX like conditional variables.
258 return 1; // The mutex still locked.
260 return 0; // The mutex will be unlocked via wrapper.
270 void __cxa_guard_abort (__guard *g)
272 #ifdef __GTHREAD_HAS_COND
273 if (__gthread_active_p())
277 set_init_in_progress_flag(g, 0);
279 // If we abort, we still need to wake up all other threads waiting for
280 // the conditional variable.
281 get_static_cond().broadcast();
286 set_init_in_progress_flag(g, 0);
287 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
288 // This provides compatibility with older systems not supporting POSIX like
289 // conditional variables.
290 if (__gthread_active_p ())
291 static_mutex->unlock();
296 void __cxa_guard_release (__guard *g)
298 #ifdef __GTHREAD_HAS_COND
299 if (__gthread_active_p())
303 set_init_in_progress_flag(g, 0);
304 _GLIBCXX_GUARD_SET_AND_RELEASE(g);
306 get_static_cond().broadcast();
311 set_init_in_progress_flag(g, 0);
312 _GLIBCXX_GUARD_SET_AND_RELEASE (g);
314 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
315 // This provides compatibility with older systems not supporting POSIX like
316 // conditional variables.
317 if (__gthread_active_p())
318 static_mutex->unlock();