OLD | NEW |
(Empty) | |
| 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 static inline int a_cas(volatile int *p, int t, int s) |
| 26 { |
| 27 __asm__("1: l.lwa %0, %1\n" |
| 28 " l.sfeq %0, %2\n" |
| 29 " l.bnf 1f\n" |
| 30 " l.nop\n" |
| 31 " l.swa %1, %3\n" |
| 32 " l.bnf 1b\n" |
| 33 " l.nop\n" |
| 34 "1: \n" |
| 35 : "=&r"(t), "+m"(*p) : "r"(t), "r"(s) : "cc", "memory" ); |
| 36 return t; |
| 37 } |
| 38 |
| 39 static inline void *a_cas_p(volatile void *p, void *t, void *s) |
| 40 { |
| 41 return (void *)a_cas(p, (int)t, (int)s); |
| 42 } |
| 43 |
| 44 static inline int a_swap(volatile int *x, int v) |
| 45 { |
| 46 int old; |
| 47 do old = *x; |
| 48 while (a_cas(x, old, v) != old); |
| 49 return old; |
| 50 } |
| 51 |
| 52 static inline int a_fetch_add(volatile int *x, int v) |
| 53 { |
| 54 int old; |
| 55 do old = *x; |
| 56 while (a_cas(x, old, old+v) != old); |
| 57 return old; |
| 58 } |
| 59 |
| 60 static inline void a_inc(volatile int *x) |
| 61 { |
| 62 a_fetch_add(x, 1); |
| 63 } |
| 64 |
| 65 static inline void a_dec(volatile int *x) |
| 66 { |
| 67 a_fetch_add(x, -1); |
| 68 } |
| 69 |
| 70 static inline void a_store(volatile int *p, int x) |
| 71 { |
| 72 a_swap(p, x); |
| 73 } |
| 74 |
| 75 #define a_spin a_barrier |
| 76 |
| 77 static inline void a_barrier() |
| 78 { |
| 79 a_cas(&(int){0}, 0, 0); |
| 80 } |
| 81 |
| 82 static inline void a_crash() |
| 83 { |
| 84 *(volatile char *)0=0; |
| 85 } |
| 86 |
| 87 static inline void a_and(volatile int *p, int v) |
| 88 { |
| 89 int old; |
| 90 do old = *p; |
| 91 while (a_cas(p, old, old&v) != old); |
| 92 } |
| 93 |
| 94 static inline void a_or(volatile int *p, int v) |
| 95 { |
| 96 int old; |
| 97 do old = *p; |
| 98 while (a_cas(p, old, old|v) != old); |
| 99 } |
| 100 |
| 101 static inline void a_or_l(volatile void *p, long v) |
| 102 { |
| 103 a_or(p, v); |
| 104 } |
| 105 |
| 106 static inline void a_and_64(volatile uint64_t *p, uint64_t v) |
| 107 { |
| 108 union { uint64_t v; uint32_t r[2]; } u = { v }; |
| 109 a_and((int *)p, u.r[0]); |
| 110 a_and((int *)p+1, u.r[1]); |
| 111 } |
| 112 |
| 113 static inline void a_or_64(volatile uint64_t *p, uint64_t v) |
| 114 { |
| 115 union { uint64_t v; uint32_t r[2]; } u = { v }; |
| 116 a_or((int *)p, u.r[0]); |
| 117 a_or((int *)p+1, u.r[1]); |
| 118 } |
| 119 |
| 120 #endif |
OLD | NEW |