add working a_spin() atomic for non-x86 targets
[musl] / arch / arm / atomic.h
1 #ifndef _INTERNAL_ATOMIC_H
2 #define _INTERNAL_ATOMIC_H
3
4 #include <stdint.h>
5
6 static inline int a_ctz_l(unsigned long x)
7 {
8         static const char debruijn32[32] = {
9                 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13,
10                 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14
11         };
12         return debruijn32[(x&-x)*0x076be629 >> 27];
13 }
14
15 static inline int a_ctz_64(uint64_t x)
16 {
17         uint32_t y = x;
18         if (!y) {
19                 y = x>>32;
20                 return 32 + a_ctz_l(y);
21         }
22         return a_ctz_l(y);
23 }
24
25 #if ((__ARM_ARCH_6__ || __ARM_ARCH_6K__ || __ARM_ARCH_6ZK__) && !__thumb__) \
26  || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH >= 7
27
28 #if __ARM_ARCH_7A__ || __ARM_ARCH_7R__ ||  __ARM_ARCH >= 7
29 #define MEM_BARRIER "dmb ish"
30 #else
31 #define MEM_BARRIER "mcr p15,0,r0,c7,c10,5"
32 #endif
33
34 static inline int __k_cas(int t, int s, volatile int *p)
35 {
36         int ret;
37         __asm__(
38                 "       " MEM_BARRIER "\n"
39                 "1:     ldrex %0,%3\n"
40                 "       subs %0,%0,%1\n"
41 #ifdef __thumb__
42                 "       itt eq\n"
43 #endif
44                 "       strexeq %0,%2,%3\n"
45                 "       teqeq %0,#1\n"
46                 "       beq 1b\n"
47                 "       " MEM_BARRIER "\n"
48                 : "=&r"(ret)
49                 : "r"(t), "r"(s), "Q"(*p)
50                 : "memory", "cc" );
51         return ret;
52 }
53 #else
54 #define __k_cas ((int (*)(int, int, volatile int *))0xffff0fc0)
55 #endif
56
57 static inline int a_cas(volatile int *p, int t, int s)
58 {
59         int old;
60         for (;;) {
61                 if (!__k_cas(t, s, p))
62                         return t;
63                 if ((old=*p) != t)
64                         return old;
65         }
66 }
67
68 static inline void *a_cas_p(volatile void *p, void *t, void *s)
69 {
70         return (void *)a_cas(p, (int)t, (int)s);
71 }
72
73 static inline int a_swap(volatile int *x, int v)
74 {
75         int old;
76         do old = *x;
77         while (__k_cas(old, v, x));
78         return old;
79 }
80
81 static inline int a_fetch_add(volatile int *x, int v)
82 {
83         int old;
84         do old = *x;
85         while (__k_cas(old, old+v, x));
86         return old;
87 }
88
89 static inline void a_inc(volatile int *x)
90 {
91         a_fetch_add(x, 1);
92 }
93
94 static inline void a_dec(volatile int *x)
95 {
96         a_fetch_add(x, -1);
97 }
98
99 static inline void a_store(volatile int *p, int x)
100 {
101         while (__k_cas(*p, x, p));
102 }
103
104 static inline void a_spin()
105 {
106         __k_cas(&(int){0}, 0, 0));
107 }
108
109 static inline void a_crash()
110 {
111         *(volatile char *)0=0;
112 }
113
114 static inline void a_and(volatile int *p, int v)
115 {
116         int old;
117         do old = *p;
118         while (__k_cas(old, old&v, p));
119 }
120
121 static inline void a_or(volatile int *p, int v)
122 {
123         int old;
124         do old = *p;
125         while (__k_cas(old, old|v, p));
126 }
127
128 static inline void a_or_l(volatile void *p, long v)
129 {
130         a_or(p, v);
131 }
132
133 static inline void a_and_64(volatile uint64_t *p, uint64_t v)
134 {
135         union { uint64_t v; uint32_t r[2]; } u = { v };
136         a_and((int *)p, u.r[0]);
137         a_and((int *)p+1, u.r[1]);
138 }
139
140 static inline void a_or_64(volatile uint64_t *p, uint64_t v)
141 {
142         union { uint64_t v; uint32_t r[2]; } u = { v };
143         a_or((int *)p, u.r[0]);
144         a_or((int *)p+1, u.r[1]);
145 }
146
147 #endif