blob: 5b0d57d0b44aa59bfc45a72fd3bcd808ef1e90d9 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Elliott Hughes15b641a2014-05-14 18:18:55 -07002 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080015 */
16
17#include <stddef.h>
Fengwei Yinee18fb42012-03-28 17:25:17 +080018#include <endian.h>
Elliott Hugheseb847bc2013-10-09 15:50:50 -070019
20#include "private/bionic_atomic_inline.h"
21#include "private/bionic_futex.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080022
Elliott Hughes387d4b72012-08-09 15:17:46 -070023// This file contains C++ ABI support functions for one time
24// constructors as defined in the "Run-time ABI for the ARM Architecture"
25// section 4.4.2
26//
Fengwei Yinee18fb42012-03-28 17:25:17 +080027// ARM C++ ABI and Itanium/x86 C++ ABI has different definition for
28// one time construction:
29//
30// ARM C++ ABI defines the LSB of guard variable should be tested
31// by compiler-generated code before calling __cxa_guard_acquire et al.
32//
33// The Itanium/x86 C++ ABI defines the low-order _byte_ should be
34// tested instead.
35//
36// Meanwhile, guard variable are 32bit aligned for ARM, and 64bit
37// aligned for x86.
38//
39// Reference documentation:
40//
41// section 3.2.3 of ARM IHI 0041C (for ARM)
42// section 3.3.2 of the Itanium C++ ABI specification v1.83 (for x86).
43//
44// There is no C++ ABI available for other ARCH. But the gcc source
45// shows all other ARCH follow the definition of Itanium/x86 C++ ABI.
46
Fengwei Yinee18fb42012-03-28 17:25:17 +080047#if defined(__arm__)
Elliott Hughes15b641a2014-05-14 18:18:55 -070048// The ARM C++ ABI mandates that guard variables are 32-bit aligned, 32-bit
49// values. The LSB is tested by the compiler-generated code before calling
Fengwei Yinee18fb42012-03-28 17:25:17 +080050// __cxa_guard_acquire.
Elliott Hughes15b641a2014-05-14 18:18:55 -070051union _guard_t {
Fengwei Yinee18fb42012-03-28 17:25:17 +080052 int volatile state;
53 int32_t aligner;
Elliott Hughes15b641a2014-05-14 18:18:55 -070054};
Fengwei Yinee18fb42012-03-28 17:25:17 +080055
56const static int ready = 0x1;
57const static int pending = 0x2;
58const static int waiting = 0x6;
59
Elliott Hughes15b641a2014-05-14 18:18:55 -070060#else
61// The Itanium/x86 C++ ABI (used by all other architectures) mandates that
62// guard variables are 64-bit aligned, 64-bit values. The LSB is tested by
63// the compiler-generated code before calling __cxa_guard_acquire.
64union _guard_t {
Fengwei Yinee18fb42012-03-28 17:25:17 +080065 int volatile state;
66 int64_t aligner;
Elliott Hughes15b641a2014-05-14 18:18:55 -070067};
Fengwei Yinee18fb42012-03-28 17:25:17 +080068
69const static int ready = letoh32(0x1);
70const static int pending = letoh32(0x100);
71const static int waiting = letoh32(0x10000);
72#endif
73
Elliott Hughes15b641a2014-05-14 18:18:55 -070074extern "C" int __cxa_guard_acquire(_guard_t* gv) {
Fengwei Yinee18fb42012-03-28 17:25:17 +080075 // 0 -> pending, return 1
76 // pending -> waiting, wait and return 0
77 // waiting: untouched, wait and return 0
78 // ready: untouched, return 0
79
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080retry:
Elliott Hughes762a4fe2012-04-16 14:40:26 -070081 if (__bionic_cmpxchg(0, pending, &gv->state) == 0) {
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070082 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083 return 1;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070084 }
Elliott Hughes762a4fe2012-04-16 14:40:26 -070085 __bionic_cmpxchg(pending, waiting, &gv->state); // Indicate there is a waiter
Fengwei Yinee18fb42012-03-28 17:25:17 +080086 __futex_wait(&gv->state, waiting, NULL);
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070087
Elliott Hughes15b641a2014-05-14 18:18:55 -070088 if (gv->state != ready) {
89 // __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 -080090 goto retry;
Elliott Hughes15b641a2014-05-14 18:18:55 -070091 }
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070092
93 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094 return 0;
95}
96
Elliott Hughes15b641a2014-05-14 18:18:55 -070097extern "C" void __cxa_guard_release(_guard_t* gv) {
Fengwei Yinee18fb42012-03-28 17:25:17 +080098 // pending -> ready
99 // waiting -> ready, and wake
100
David 'Digit' Turnerd4667802010-06-11 13:18:41 -0700101 ANDROID_MEMBAR_FULL();
Elliott Hughes762a4fe2012-04-16 14:40:26 -0700102 if (__bionic_cmpxchg(pending, ready, &gv->state) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103 return;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -0700104 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105
Fengwei Yinee18fb42012-03-28 17:25:17 +0800106 gv->state = ready;
107 __futex_wake(&gv->state, 0x7fffffff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108}
109
Elliott Hughes15b641a2014-05-14 18:18:55 -0700110extern "C" void __cxa_guard_abort(_guard_t* gv) {
David 'Digit' Turnerd4667802010-06-11 13:18:41 -0700111 ANDROID_MEMBAR_FULL();
Fengwei Yinee18fb42012-03-28 17:25:17 +0800112 gv->state= 0;
113 __futex_wake(&gv->state, 0x7fffffff);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114}