blob: 2a44c79d1e69e572bee8396de0404d914b2a3679 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * one_time_construction.cpp
3 *
4 * Copyright 2006 The Android Open Source Project
5 *
6 * This file contains C++ ABI support functions for one time
7 * constructors as defined in the "Run-time ABI for the ARM Architecture"
8 * section 4.4.2
9 */
10
11#include <stddef.h>
12#include <sys/atomics.h>
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070013#include <bionic_futex.h>
14#include <bionic_atomic_inline.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080015
16extern "C" int __cxa_guard_acquire(int volatile * gv)
17{
18 // 0 -> 2, return 1
19 // 2 -> 6, wait and return 0
20 // 6 untouched, wait and return 0
21 // 1 untouched, return 0
22retry:
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070023 if (__atomic_cmpxchg(0, 0x2, gv) == 0) {
24 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080025 return 1;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070026 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080027 __atomic_cmpxchg(0x2, 0x6, gv); // Indicate there is a waiter
28 __futex_wait(gv, 0x6, NULL);
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070029
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030 if(*gv != 1) // __cxa_guard_abort was called, let every thread try since there is no return code for this condition
31 goto retry;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070032
33 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034 return 0;
35}
36
37extern "C" void __cxa_guard_release(int volatile * gv)
38{
39 // 2 -> 1
40 // 6 -> 1, and wake
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070041 ANDROID_MEMBAR_FULL();
42 if (__atomic_cmpxchg(0x2, 0x1, gv) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043 return;
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070044 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045
46 *gv = 0x1;
47 __futex_wake(gv, 0x7fffffff);
48}
49
50extern "C" void __cxa_guard_abort(int volatile * gv)
51{
David 'Digit' Turnerd4667802010-06-11 13:18:41 -070052 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053 *gv = 0;
54 __futex_wake(gv, 0x7fffffff);
55}