summaryrefslogtreecommitdiffstats
path: root/libstdc++/src/one_time_construction.cpp
blob: 304afb808c3cf3ae37c22976905d9d462925cbc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * one_time_construction.cpp
 *
 * Copyright 2006 The Android Open Source Project
 *
 * This file contains C++ ABI support functions for one time
 * constructors as defined in the "Run-time ABI for the ARM Architecture"
 * section 4.4.2
 */

#include <stddef.h>
#include <sys/atomics.h>

extern "C" int __futex_wait(volatile void *ftx, int val, const struct timespec *timeout);
extern "C" int __futex_wake(volatile void *ftx, int count);


extern "C" int __cxa_guard_acquire(int volatile * gv)
{
    // 0 -> 2, return 1
    // 2 -> 6, wait and return 0
    // 6 untouched, wait and return 0
    // 1 untouched, return 0
retry:
    if (__atomic_cmpxchg(0, 0x2, gv) == 0)
        return 1;

    __atomic_cmpxchg(0x2, 0x6, gv); // Indicate there is a waiter
    __futex_wait(gv, 0x6, NULL);
    if(*gv != 1) // __cxa_guard_abort was called, let every thread try since there is no return code for this condition
        goto retry;
    return 0;
}

extern "C" void __cxa_guard_release(int volatile * gv)
{
    // 2 -> 1
    // 6 -> 1, and wake
    if (__atomic_cmpxchg(0x2, 0x1, gv) == 0)
        return;

    *gv = 0x1;
    __futex_wake(gv, 0x7fffffff);
}

extern "C" void __cxa_guard_abort(int volatile * gv)
{
    *gv = 0;
    __futex_wake(gv, 0x7fffffff);
}