PandA-2024.02
atomic.h
Go to the documentation of this file.
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 long a_cas_l(volatile void *p, long t, long s)
74 {
75  return a_cas(p, t, s);
76 }
77 
78 static inline int a_swap(volatile int *x, int v)
79 {
80  int old;
81  do old = *x;
82  while (__k_cas(old, v, x));
83  return old;
84 }
85 
86 static inline int a_fetch_add(volatile int *x, int v)
87 {
88  int old;
89  do old = *x;
90  while (__k_cas(old, old+v, x));
91  return old;
92 }
93 
94 static inline void a_inc(volatile int *x)
95 {
96  a_fetch_add(x, 1);
97 }
98 
99 static inline void a_dec(volatile int *x)
100 {
101  a_fetch_add(x, -1);
102 }
103 
104 static inline void a_store(volatile int *p, int x)
105 {
106  while (__k_cas(*p, x, p));
107 }
108 
109 static inline void a_spin()
110 {
111 }
112 
113 static inline void a_crash()
114 {
115  *(volatile char *)0=0;
116 }
117 
118 static inline void a_and(volatile int *p, int v)
119 {
120  int old;
121  do old = *p;
122  while (__k_cas(old, old&v, p));
123 }
124 
125 static inline void a_or(volatile int *p, int v)
126 {
127  int old;
128  do old = *p;
129  while (__k_cas(old, old|v, p));
130 }
131 
132 static inline void a_or_l(volatile void *p, long v)
133 {
134  a_or(p, v);
135 }
136 
137 static inline void a_and_64(volatile uint64_t *p, uint64_t v)
138 {
139  union { uint64_t v; uint32_t r[2]; } u = { v };
140  a_and((int *)p, u.r[0]);
141  a_and((int *)p+1, u.r[1]);
142 }
143 
144 static inline void a_or_64(volatile uint64_t *p, uint64_t v)
145 {
146  union { uint64_t v; uint32_t r[2]; } u = { v };
147  a_or((int *)p, u.r[0]);
148  a_or((int *)p+1, u.r[1]);
149 }
150 
151 #endif
static void a_and(volatile int *p, int v)
Definition: atomic.h:118
static int a_fetch_add(volatile int *x, int v)
Definition: atomic.h:86
#define __k_cas
Definition: atomic.h:54
static void a_and_64(volatile uint64_t *p, uint64_t v)
Definition: atomic.h:137
static void a_inc(volatile int *x)
Definition: atomic.h:94
static void a_or(volatile int *p, int v)
Definition: atomic.h:125
static int a_ctz_64(uint64_t x)
Definition: atomic.h:15
static int a_ctz_l(unsigned long x)
Definition: atomic.h:6
static void a_spin()
Definition: atomic.h:109
static void a_store(volatile int *p, int x)
Definition: atomic.h:104
static void a_dec(volatile int *x)
Definition: atomic.h:99
static int a_swap(volatile int *x, int v)
Definition: atomic.h:78
static void a_or_l(volatile void *p, long v)
Definition: atomic.h:132
static void * a_cas_p(volatile void *p, void *t, void *s)
Definition: atomic.h:68
static void a_crash()
Definition: atomic.h:113
x
Return the smallest n such that 2^n >= _x.
static int a_cas(volatile int *p, int t, int s)
Definition: atomic.h:57
static long a_cas_l(volatile void *p, long t, long s)
Definition: atomic.h:73
static void a_or_64(volatile uint64_t *p, uint64_t v)
Definition: atomic.h:144

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13