summaryrefslogtreecommitdiffstats
path: root/src/libc++/builtins.C
blob: 04ea744f927c3ef339fa4637a5835b30188e8b33 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdint.h>
#include <stdlib.h>

#include <arch/ppc.H>

void* operator new(size_t s)
{
    return malloc(s);
};

void* operator new[](size_t s)
{
    return malloc(s);
};

void* operator new(size_t s, void *place)
{
    return place;
}

void* operator new[](size_t s, void *place)
{
    return place;
}

void operator delete(void* p)
{
    return free(p);
};

void operator delete[](void* p)
{
    return free(p);
};

extern "C" int __cxa_guard_acquire(volatile uint64_t* gv)
{
    // States:
    //     0 -> uninitialized
    //     1 -> locked
    //     2 -> unlocked and initialized
    
    uint32_t v = __sync_val_compare_and_swap((volatile uint32_t*)gv, 0, 1);
    if (v == 0)
	return 1;
    if (v == 2)
	return 0;
    
    // Wait for peer thread to perform initialization (state 2).
    while(2 != *(volatile uint32_t*)gv);
    
    // Instruction barrier to ensure value is set before later loads execute.
    isync(); 
    
    return 0;
}

extern "C" void __cxa_guard_release(volatile uint64_t* gv)
{
    // Memory barrier to ensure all preceeding writes have completed before
    // releasing guard.
    lwsync();

    (*(volatile uint32_t*)gv) = 2;
    return;
}


extern "C" int __cxa_atexit(void (*)(void*), void*, void*)
{
    return 0;
}

extern "C" void __cxa_pure_virtual()
{
    // TODO: Add better code for invalid pure virtual call.
    while(1);
}

void*   __dso_handle = (void*) &__dso_handle;
OpenPOWER on IntegriCloud