]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/guard.cc
Use atomics in guard.cc.
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / guard.cc
1 // Copyright (C) 2002-2015 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 3, or (at your option)
8 // any later version.
9
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.
14
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.
18
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/>.
23
24 // Written by Mark Mitchell, CodeSourcery LLC, <mark@codesourcery.com>
25 // Thread support written by Jason Merrill, Red Hat Inc. <jason@redhat.com>
26
27 #include <bits/c++config.h>
28 #include <cxxabi.h>
29 #include <exception>
30 #include <new>
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)
35 # include <climits>
36 # include <syscall.h>
37 # include <unistd.h>
38 # define _GLIBCXX_USE_FUTEX
39 # define _GLIBCXX_FUTEX_WAIT 0
40 # define _GLIBCXX_FUTEX_WAKE 1
41 #endif
42
43 // The IA64/generic ABI uses the first byte of the guard variable.
44 // The ARM EABI uses the least significant bit.
45
46 // Thread-safe static local initialization support.
47 #ifdef __GTHREADS
48 # ifndef _GLIBCXX_USE_FUTEX
49 namespace
50 {
51 // A single mutex controlling all static initializations.
52 static __gnu_cxx::__recursive_mutex* static_mutex;
53
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;
57
58 static void init()
59 { static_mutex = new (&fake_mutex) __gnu_cxx::__recursive_mutex(); }
60
61 __gnu_cxx::__recursive_mutex&
62 get_static_mutex()
63 {
64 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
65 __gthread_once(&once, init);
66 return *static_mutex;
67 }
68
69 // Simple wrapper for exception safety.
70 struct mutex_wrapper
71 {
72 bool unlock;
73 mutex_wrapper() : unlock(true)
74 { get_static_mutex().lock(); }
75
76 ~mutex_wrapper()
77 {
78 if (unlock)
79 static_mutex->unlock();
80 }
81 };
82 }
83 # endif
84
85 # if defined(__GTHREAD_HAS_COND) && !defined(_GLIBCXX_USE_FUTEX)
86 namespace
87 {
88 // A single condition variable controlling all static initializations.
89 static __gnu_cxx::__cond* static_cond;
90
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;
95
96 static void init_static_cond()
97 { static_cond = new (&fake_cond) __gnu_cxx::__cond(); }
98
99 __gnu_cxx::__cond&
100 get_static_cond()
101 {
102 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
103 __gthread_once(&once, init_static_cond);
104 return *static_cond;
105 }
106 }
107 # endif
108
109 # ifndef _GLIBCXX_GUARD_TEST_AND_ACQUIRE
110
111 // Test the guard variable with a memory load with
112 // acquire semantics.
113
114 inline bool
115 __test_and_acquire (__cxxabiv1::__guard *g)
116 {
117 unsigned char __c;
118 unsigned char *__p = reinterpret_cast<unsigned char *>(g);
119 __atomic_load (__p, &__c, __ATOMIC_ACQUIRE);
120 return _GLIBCXX_GUARD_TEST(&__c);
121 }
122 # define _GLIBCXX_GUARD_TEST_AND_ACQUIRE(G) __test_and_acquire (G)
123 # endif
124
125 # ifndef _GLIBCXX_GUARD_SET_AND_RELEASE
126
127 // Set the guard variable to 1 with memory order release semantics.
128
129 inline void
130 __set_and_release (__cxxabiv1::__guard *g)
131 {
132 unsigned char *__p = reinterpret_cast<unsigned char *>(g);
133 unsigned char val = 1;
134 __atomic_store (__p, &val, __ATOMIC_RELEASE);
135 }
136 # define _GLIBCXX_GUARD_SET_AND_RELEASE(G) __set_and_release (G)
137 # endif
138
139 #else /* !__GTHREADS */
140
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)
144
145 #endif /* __GTHREADS */
146
147 //
148 // Here are C++ run-time routines for guarded initialization of static
149 // variables. There are 4 scenarios under which these routines are called:
150 //
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.
156 //
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.
161 //
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.
168 //
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.
175
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
182 // initialized.
183 // _GLIBCXX_GUARD_PENDING_BIT The guarded static var is being initialized
184 // and no other thread is waiting for its
185 // initialization.
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.
189
190 namespace __cxxabiv1
191 {
192 #ifdef _GLIBCXX_USE_FUTEX
193 namespace
194 {
195 static inline int __guard_test_bit (const int __byte, const int __val)
196 {
197 union { int __i; char __c[sizeof (int)]; } __u = { 0 };
198 __u.__c[__byte] = __val;
199 return __u.__i;
200 }
201 }
202 #endif
203
204 static inline int
205 init_in_progress_flag(__guard* g)
206 { return ((char *)g)[1]; }
207
208 static inline void
209 set_init_in_progress_flag(__guard* g, int v)
210 { ((char *)g)[1] = v; }
211
212 static inline void
213 throw_recursive_init_exception()
214 {
215 #if __cpp_exceptions
216 throw __gnu_cxx::recursive_init_error();
217 #else
218 // Use __builtin_trap so we don't require abort().
219 __builtin_trap();
220 #endif
221 }
222
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.
225 static int
226 acquire(__guard *g)
227 {
228 // Quit if the object is already initialized.
229 if (_GLIBCXX_GUARD_TEST(g))
230 return 0;
231
232 if (init_in_progress_flag(g))
233 throw_recursive_init_exception();
234
235 set_init_in_progress_flag(g, 1);
236 return 1;
237 }
238
239 extern "C"
240 int __cxa_guard_acquire (__guard *g)
241 {
242 #ifdef __GTHREADS
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
245 // guard test.
246 if (_GLIBCXX_GUARD_TEST_AND_ACQUIRE (g))
247 return 0;
248
249 # ifdef _GLIBCXX_USE_FUTEX
250 // If __atomic_* and futex syscall are supported, don't use any global
251 // mutex.
252 if (__gthread_active_p ())
253 {
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;
258
259 while (1)
260 {
261 int expected(0);
262 if (__atomic_compare_exchange_n(gi, &expected, pending_bit, false,
263 __ATOMIC_ACQ_REL,
264 __ATOMIC_ACQUIRE))
265 {
266 // This thread should do the initialization.
267 return 1;
268 }
269
270 if (expected == guard_bit)
271 {
272 // Already initialized.
273 return 0;
274 }
275
276 if (expected == pending_bit)
277 {
278 // Use acquire here.
279 int newv = expected | waiting_bit;
280 if (!__atomic_compare_exchange_n(gi, &expected, newv, false,
281 __ATOMIC_ACQ_REL,
282 __ATOMIC_ACQUIRE))
283 {
284 if (expected == guard_bit)
285 {
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.
290 return 0;
291 }
292 if (expected == 0)
293 continue;
294 }
295
296 expected = newv;
297 }
298
299 syscall (SYS_futex, gi, _GLIBCXX_FUTEX_WAIT, expected, 0);
300 }
301 }
302 # else
303 if (__gthread_active_p ())
304 {
305 mutex_wrapper mw;
306
307 while (1) // When this loop is executing, mutex is locked.
308 {
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
313
314 if (init_in_progress_flag(g))
315 {
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
319 // this.
320 get_static_cond().wait_recursive(&get_static_mutex());
321 }
322 else
323 {
324 set_init_in_progress_flag(g, 1);
325 return 1; // The mutex will be unlocked via wrapper.
326 }
327 # else
328 // This provides compatibility with older systems not supporting
329 // POSIX like condition variables.
330 if (acquire(g))
331 {
332 mw.unlock = false;
333 return 1; // The mutex still locked.
334 }
335 return 0; // The mutex will be unlocked via wrapper.
336 # endif
337 }
338 }
339 # endif
340 #endif
341
342 return acquire (g);
343 }
344
345 extern "C"
346 void __cxa_guard_abort (__guard *g) throw ()
347 {
348 #ifdef _GLIBCXX_USE_FUTEX
349 // If __atomic_* and futex syscall are supported, don't use any global
350 // mutex.
351 if (__gthread_active_p ())
352 {
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);
356
357 if ((old & waiting_bit) != 0)
358 syscall (SYS_futex, gi, _GLIBCXX_FUTEX_WAKE, INT_MAX);
359 return;
360 }
361 #elif defined(__GTHREAD_HAS_COND)
362 if (__gthread_active_p())
363 {
364 mutex_wrapper mw;
365
366 set_init_in_progress_flag(g, 0);
367
368 // If we abort, we still need to wake up all other threads waiting for
369 // the condition variable.
370 get_static_cond().broadcast();
371 return;
372 }
373 #endif
374
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();
381 #endif
382 }
383
384 extern "C"
385 void __cxa_guard_release (__guard *g) throw ()
386 {
387 #ifdef _GLIBCXX_USE_FUTEX
388 // If __atomic_* and futex syscall are supported, don't use any global
389 // mutex.
390 if (__gthread_active_p ())
391 {
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);
396
397 if ((old & waiting_bit) != 0)
398 syscall (SYS_futex, gi, _GLIBCXX_FUTEX_WAKE, INT_MAX);
399 return;
400 }
401 #elif defined(__GTHREAD_HAS_COND)
402 if (__gthread_active_p())
403 {
404 mutex_wrapper mw;
405
406 set_init_in_progress_flag(g, 0);
407 _GLIBCXX_GUARD_SET_AND_RELEASE(g);
408
409 get_static_cond().broadcast();
410 return;
411 }
412 #endif
413
414 set_init_in_progress_flag(g, 0);
415 _GLIBCXX_GUARD_SET_AND_RELEASE (g);
416
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();
422 #endif
423 }
424 }