blob: 8ba18df2b6bcba6be24973542a16bfdda87de7f1 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002 * Copyright 2006 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 */
4
5#include <stddef.h>
6#include <sys/atomics.h>
Fengwei Yinee18fb42012-03-28 17:25:17 +08007#include <endian.h>
Elliott Hughes52d62332012-07-27 17:40:29 -07008#include <private/bionic_futex.h>
9#include <private/bionic_atomic_inline.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080010
Elliott Hughes387d4b72012-08-09 15:17:46 -070011// This file contains C++ ABI support functions for one time
12// constructors as defined in the "Run-time ABI for the ARM Architecture"
13// section 4.4.2
14//
Fengwei Yinee18fb42012-03-28 17:25:17 +080015// ARM C++ ABI and Itanium/x86 C++ ABI has different definition for
16// one time construction:
17//
18// ARM C++ ABI defines the LSB of guard variable should be tested
19// by compiler-generated code before calling __cxa_guard_acquire et al.
20//
21// The Itanium/x86 C++ ABI defines the low-order _byte_ should be
22// tested instead.
23//
24// Meanwhile, guard variable are 32bit aligned for ARM, and 64bit
25// aligned for x86.
26//
27// Reference documentation:
28//
29// section 3.2.3 of ARM IHI 0041C (for ARM)
30// section 3.3.2 of the Itanium C++ ABI specification v1.83 (for x86).
31//
32// There is no C++ ABI available for other ARCH. But the gcc source
33// shows all other ARCH follow the definition of Itanium/x86 C++ ABI.
34
35
36#if defined(__arm__)
37// The ARM C++ ABI mandates that guard variable are
38// 32-bit aligned, 32-bit values. And only its LSB is tested by
39// the compiler-generated code before calling
40// __cxa_guard_acquire.
41//
42typedef union {
43 int volatile state;
44 int32_t aligner;
45} _guard_t;
46
47const static int ready = 0x1;
48const static int pending = 0x2;
49const static int waiting = 0x6;
50
51#else // GCC sources indicates all none-arm follow the same ABI
52// The Itanium/x86 C++ ABI mandates that guard variables
53// are 64-bit aligned, 64-bit values. Also, the least-significant
54// byte is tested by the compiler-generated code before, we calling
55// __cxa_guard_acquire. We can access it through the first
56// 32-bit word in the union below.
57//
58typedef union {
59 int volatile state;
60 int64_t aligner;
61} _guard_t;
62
63const static int ready = letoh32(0x1);
64const static int pending = letoh32(0x100);
65const static int waiting = letoh32(0x10000);
66#endif
67
68extern "C" int __cxa_guard_acquire(_guard_t* gv)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069{
Fengwei Yinee18fb42012-03-28 17:25:17 +080070 // 0 -> pending, return 1
71 // pending -> waiting, wait and return 0
72 // waiting: untouched, wait and return 0
73 // ready: untouched, return 0
74
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075retry:
Elliott Hughes762a4fe2012-04-16 14:40:26 -070076 if (__bionic_cmpxchg(0, pending, &gv->state) == 0) {
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070077 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078 return 1;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070079 }
Elliott Hughes762a4fe2012-04-16 14:40:26 -070080 __bionic_cmpxchg(pending, waiting, &gv->state); // Indicate there is a waiter
Fengwei Yinee18fb42012-03-28 17:25:17 +080081 __futex_wait(&gv->state, waiting, NULL);
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070082
Fengwei Yinee18fb42012-03-28 17:25:17 +080083 if (gv->state != ready) // __cxa_guard_abort was called, let every thread try since there is no return code for this condition
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084 goto retry;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070085
86 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087 return 0;
88}
89
Fengwei Yinee18fb42012-03-28 17:25:17 +080090extern "C" void __cxa_guard_release(_guard_t* gv)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091{
Fengwei Yinee18fb42012-03-28 17:25:17 +080092 // pending -> ready
93 // waiting -> ready, and wake
94
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070095 ANDROID_MEMBAR_FULL();
Elliott Hughes762a4fe2012-04-16 14:40:26 -070096 if (__bionic_cmpxchg(pending, ready, &gv->state) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097 return;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070098 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099
Fengwei Yinee18fb42012-03-28 17:25:17 +0800100 gv->state = ready;
101 __futex_wake(&gv->state, 0x7fffffff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102}
103
Fengwei Yinee18fb42012-03-28 17:25:17 +0800104extern "C" void __cxa_guard_abort(_guard_t* gv)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105{
David 'Digit' Turnerd4667802010-06-11 13:18:41 -0700106 ANDROID_MEMBAR_FULL();
Fengwei Yinee18fb42012-03-28 17:25:17 +0800107 gv->state= 0;
108 __futex_wake(&gv->state, 0x7fffffff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109}