blob: 55743111b27d01f896151bd81498d35ac84c9e84 [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 Hugheseb847bc2013-10-09 15:50:50 -07008
9#include "private/bionic_atomic_inline.h"
10#include "private/bionic_futex.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080011
Elliott Hughes387d4b72012-08-09 15:17:46 -070012// This file contains C++ ABI support functions for one time
13// constructors as defined in the "Run-time ABI for the ARM Architecture"
14// section 4.4.2
15//
Fengwei Yinee18fb42012-03-28 17:25:17 +080016// ARM C++ ABI and Itanium/x86 C++ ABI has different definition for
17// one time construction:
18//
19// ARM C++ ABI defines the LSB of guard variable should be tested
20// by compiler-generated code before calling __cxa_guard_acquire et al.
21//
22// The Itanium/x86 C++ ABI defines the low-order _byte_ should be
23// tested instead.
24//
25// Meanwhile, guard variable are 32bit aligned for ARM, and 64bit
26// aligned for x86.
27//
28// Reference documentation:
29//
30// section 3.2.3 of ARM IHI 0041C (for ARM)
31// section 3.3.2 of the Itanium C++ ABI specification v1.83 (for x86).
32//
33// There is no C++ ABI available for other ARCH. But the gcc source
34// shows all other ARCH follow the definition of Itanium/x86 C++ ABI.
35
36
37#if defined(__arm__)
38// The ARM C++ ABI mandates that guard variable are
39// 32-bit aligned, 32-bit values. And only its LSB is tested by
40// the compiler-generated code before calling
41// __cxa_guard_acquire.
42//
43typedef union {
44 int volatile state;
45 int32_t aligner;
46} _guard_t;
47
48const static int ready = 0x1;
49const static int pending = 0x2;
50const static int waiting = 0x6;
51
52#else // GCC sources indicates all none-arm follow the same ABI
53// The Itanium/x86 C++ ABI mandates that guard variables
54// are 64-bit aligned, 64-bit values. Also, the least-significant
55// byte is tested by the compiler-generated code before, we calling
56// __cxa_guard_acquire. We can access it through the first
57// 32-bit word in the union below.
58//
59typedef union {
60 int volatile state;
61 int64_t aligner;
62} _guard_t;
63
64const static int ready = letoh32(0x1);
65const static int pending = letoh32(0x100);
66const static int waiting = letoh32(0x10000);
67#endif
68
69extern "C" int __cxa_guard_acquire(_guard_t* gv)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070{
Fengwei Yinee18fb42012-03-28 17:25:17 +080071 // 0 -> pending, return 1
72 // pending -> waiting, wait and return 0
73 // waiting: untouched, wait and return 0
74 // ready: untouched, return 0
75
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076retry:
Elliott Hughes762a4fe2012-04-16 14:40:26 -070077 if (__bionic_cmpxchg(0, pending, &gv->state) == 0) {
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070078 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079 return 1;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070080 }
Elliott Hughes762a4fe2012-04-16 14:40:26 -070081 __bionic_cmpxchg(pending, waiting, &gv->state); // Indicate there is a waiter
Fengwei Yinee18fb42012-03-28 17:25:17 +080082 __futex_wait(&gv->state, waiting, NULL);
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070083
Fengwei Yinee18fb42012-03-28 17:25:17 +080084 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 -080085 goto retry;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070086
87 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088 return 0;
89}
90
Fengwei Yinee18fb42012-03-28 17:25:17 +080091extern "C" void __cxa_guard_release(_guard_t* gv)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092{
Fengwei Yinee18fb42012-03-28 17:25:17 +080093 // pending -> ready
94 // waiting -> ready, and wake
95
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070096 ANDROID_MEMBAR_FULL();
Elliott Hughes762a4fe2012-04-16 14:40:26 -070097 if (__bionic_cmpxchg(pending, ready, &gv->state) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098 return;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070099 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100
Fengwei Yinee18fb42012-03-28 17:25:17 +0800101 gv->state = ready;
102 __futex_wake(&gv->state, 0x7fffffff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103}
104
Fengwei Yinee18fb42012-03-28 17:25:17 +0800105extern "C" void __cxa_guard_abort(_guard_t* gv)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106{
David 'Digit' Turnerd4667802010-06-11 13:18:41 -0700107 ANDROID_MEMBAR_FULL();
Fengwei Yinee18fb42012-03-28 17:25:17 +0800108 gv->state= 0;
109 __futex_wake(&gv->state, 0x7fffffff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110}