* libsupc++/guard.cc (struct mutex_wrapper): Move into
[official-gcc.git] / libstdc++-v3 / libsupc++ / guard.cc
blob6cf83b1c1f4238fc5c2d34ce1e8c7693962cc2fd
1 // Copyright (C) 2002, 2004, 2006 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 2, 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 // 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>
33 #include <cxxabi.h>
34 #include <exception>
35 #include <new>
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.
43 #ifdef __GTHREADS
44 namespace
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;
53 static void init()
54 { static_mutex = new (&fake_mutex) __gnu_cxx::__recursive_mutex(); }
56 __gnu_cxx::__recursive_mutex&
57 get_static_mutex()
59 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
60 __gthread_once(&once, init);
61 return *static_mutex;
64 // Simple wrapper for exception safety.
65 struct mutex_wrapper
67 bool unlock;
68 mutex_wrapper() : unlock(true)
69 { get_static_mutex().lock(); }
71 ~mutex_wrapper()
73 if (unlock)
74 static_mutex->unlock();
79 #ifdef __GTHREAD_HAS_COND
80 namespace
82 // A single conditional variable controlling all static initializations.
83 static __gnu_cxx::__cond* static_cond;
85 // using a fake type to avoid initializing a static class.
86 typedef char fake_cond_t[sizeof(__gnu_cxx::__cond)]
87 __attribute__ ((aligned(__alignof__(__gnu_cxx::__cond))));
88 fake_cond_t fake_cond;
90 static void init_static_cond()
91 { static_cond = new (&fake_cond) __gnu_cxx::__cond(); }
93 __gnu_cxx::__cond&
94 get_static_cond()
96 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
97 __gthread_once(&once, init_static_cond);
98 return *static_cond;
101 #endif
103 #ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
104 inline bool
105 __test_and_acquire (__cxxabiv1::__guard *g)
107 bool b = _GLIBCXX_GUARD_TEST (g);
108 _GLIBCXX_READ_MEM_BARRIER;
109 return b;
111 #define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
112 #endif
114 #ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
115 inline void
116 __set_and_release (__cxxabiv1::__guard *g)
118 _GLIBCXX_WRITE_MEM_BARRIER;
119 _GLIBCXX_GUARD_SET (g);
121 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
122 #endif
124 #else /* !__GTHREADS */
126 #undef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
127 #undef _GLIBCXX_GUARD_SET_AND_RELEASE
128 #define _GLIBCXX_GUARD_SET_AND_RELEASE(G) _GLIBCXX_GUARD_SET (G)
130 #endif /* __GTHREADS */
132 namespace __gnu_cxx
134 // 6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
135 // while the object is being initialized, the behavior is undefined.
137 // Since we already have a library function to handle locking, we might
138 // as well check for this situation and throw an exception.
139 // We use the second byte of the guard variable to remember that we're
140 // in the middle of an initialization.
141 class recursive_init_error: public std::exception
143 public:
144 recursive_init_error() throw() { }
145 virtual ~recursive_init_error() throw ();
148 recursive_init_error::~recursive_init_error() throw() { }
152 // Here are C++ run-time routines for guarded initiailization of static
153 // variables. There are 4 scenarios under which these routines are called:
155 // 1. Threads not supported (__GTHREADS not defined)
156 // 2. Threads are supported but not enabled at run-time.
157 // 3. Threads enabled at run-time but __gthreads_* are not fully POSIX.
158 // 4. Threads enabled at run-time and __gthreads_* support all POSIX threads
159 // primitives we need here.
161 // The old code supported scenarios 1-3 but was broken since it used a global
162 // mutex for all threads and had the mutex locked during the whole duration of
163 // initlization of a guarded static variable. The following created a dead-lock
164 // with the old code.
166 // Thread 1 acquires the global mutex.
167 // Thread 1 starts initializing static variable.
168 // Thread 1 creates thread 2 during initialization.
169 // Thread 2 attempts to acuqire mutex to initialize another variable.
170 // Thread 2 blocks since thread 1 is locking the mutex.
171 // Thread 1 waits for result from thread 2 and also blocks. A deadlock.
173 // The new code here can handle this situation and thus is more robust. Howere,
174 // we need to use the POSIX thread conditional variable, which is not supported
175 // in all platforms, notably older versions of Microsoft Windows. The gthr*.h
176 // headers define a symbol __GTHREAD_HAS_COND for platforms that support POSIX
177 // like conditional variables. For platforms that do not support conditional
178 // variables, we need to fall back to the old code.
179 namespace __cxxabiv1
181 static inline int
182 init_in_progress_flag(__guard* g)
183 { return ((char *)g)[1]; }
185 static inline void
186 set_init_in_progress_flag(__guard* g, int v)
187 { ((char *)g)[1] = v; }
189 static inline void
190 throw_recursive_init_exception()
192 #ifdef __EXCEPTIONS
193 throw __gnu_cxx::recursive_init_error();
194 #else
195 // Use __builtin_trap so we don't require abort().
196 __builtin_trap();
197 #endif
200 // acuire() is a helper function used to acquire guard if thread support is
201 // not compiled in or is compiled in but not enabled at run-time.
202 static int
203 acquire(__guard *g)
205 // Quit if the object is already initialized.
206 if (_GLIBCXX_GUARD_TEST(g))
207 return 0;
209 if (init_in_progress_flag(g))
210 throw_recursive_init_exception();
212 set_init_in_progress_flag(g, 1);
213 return 1;
216 extern "C"
217 int __cxa_guard_acquire (__guard *g)
219 #ifdef __GTHREADS
220 // If the target can reorder loads, we need to insert a read memory
221 // barrier so that accesses to the guarded variable happen after the
222 // guard test.
223 if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g))
224 return 0;
226 if (__gthread_active_p ())
228 mutex_wrapper mw;
230 while (1) // When this loop is executing, mutex is locked.
232 #ifdef __GTHREAD_HAS_COND
233 // The static is allready initialized.
234 if (_GLIBCXX_GUARD_TEST(g))
235 return 0; // The mutex will be unlocked via wrapper
237 if (init_in_progress_flag(g))
239 // The guarded static is currently being initialized by
240 // another thread, so we release mutex and wait for the
241 // conditional variable. We will lock the mutex again after
242 // this.
243 get_static_cond().wait_recursive(&get_static_mutex());
245 else
247 set_init_in_progress_flag(g, 1);
248 return 1; // The mutex will be unlocked via wrapper.
250 #else
251 // This provides compatibility with older systems not supporting
252 // POSIX like conditional variables.
253 if (acquire(g))
255 mw.unlock = false;
256 return 1; // The mutex still locked.
258 return 0; // The mutex will be unlocked via wrapper.
259 #endif
262 #endif
264 return acquire (g);
267 extern "C"
268 void __cxa_guard_abort (__guard *g)
270 #ifdef __GTHREAD_HAS_COND
271 if (__gthread_active_p())
273 mutex_wrapper mw;
275 set_init_in_progress_flag(g, 0);
277 // If we abort, we still need to wake up all other threads waiting for
278 // the conditional variable.
279 get_static_cond().broadcast();
280 return;
282 #endif
284 set_init_in_progress_flag(g, 0);
285 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
286 // This provides compatibility with older systems not supporting POSIX like
287 // conditional variables.
288 if (__gthread_active_p ())
289 static_mutex->unlock();
290 #endif
293 extern "C"
294 void __cxa_guard_release (__guard *g)
296 #ifdef __GTHREAD_HAS_COND
297 if (__gthread_active_p())
299 mutex_wrapper mw;
301 set_init_in_progress_flag(g, 0);
302 _GLIBCXX_GUARD_SET_AND_RELEASE(g);
304 get_static_cond().broadcast();
305 return;
307 #endif
309 set_init_in_progress_flag(g, 0);
310 _GLIBCXX_GUARD_SET_AND_RELEASE (g);
312 #if defined(__GTHREADS) && !defined(__GTHREAD_HAS_COND)
313 // This provides compatibility with older systems not supporting POSIX like
314 // conditional variables.
315 if (__gthread_active_p())
316 static_mutex->unlock();
317 #endif