atomic.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef _INTERNAL_ATOMIC_H
  2. #define _INTERNAL_ATOMIC_H
  3. #include <stdint.h>
  4. static inline int a_ctz_l(unsigned long x)
  5. {
  6. static const char debruijn32[32] = {
  7. 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13,
  8. 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14
  9. };
  10. return debruijn32[(x&-x)*0x076be629 >> 27];
  11. }
  12. static inline int a_ctz_64(uint64_t x)
  13. {
  14. uint32_t y = x;
  15. if (!y) {
  16. y = x>>32;
  17. return 32 + a_ctz_l(y);
  18. }
  19. return a_ctz_l(y);
  20. }
  21. static inline int a_cas(volatile int *p, int t, int s)
  22. {
  23. int dummy;
  24. __asm__ __volatile__(
  25. ".set push\n"
  26. ".set mips2\n"
  27. ".set noreorder\n"
  28. "1: ll %0, 0(%2)\n"
  29. " bne %0, %3, 1f\n"
  30. " addu %1, %4, $0\n"
  31. " sc %1, 0(%2)\n"
  32. " beq %1, $0, 1b\n"
  33. " nop\n"
  34. "1: \n"
  35. ".set pop\n"
  36. : "=&r"(t), "=&r"(dummy) : "r"(p), "r"(t), "r"(s) : "memory" );
  37. return t;
  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. static inline long a_cas_l(volatile void *p, long t, long s)
  44. {
  45. return a_cas(p, t, s);
  46. }
  47. static inline int a_swap(volatile int *x, int v)
  48. {
  49. int old, dummy;
  50. __asm__ __volatile__(
  51. ".set push\n"
  52. ".set mips2\n"
  53. ".set noreorder\n"
  54. "1: ll %0, 0(%2)\n"
  55. " addu %1, %3, $0\n"
  56. " sc %1, 0(%2)\n"
  57. " beq %1, $0, 1b\n"
  58. " nop\n"
  59. "1: \n"
  60. ".set pop\n"
  61. : "=&r"(old), "=&r"(dummy) : "r"(x), "r"(v) : "memory" );
  62. return old;
  63. }
  64. static inline int a_fetch_add(volatile int *x, int v)
  65. {
  66. int old, dummy;
  67. __asm__ __volatile__(
  68. ".set push\n"
  69. ".set mips2\n"
  70. ".set noreorder\n"
  71. "1: ll %0, 0(%2)\n"
  72. " addu %1, %0, %3\n"
  73. " sc %1, 0(%2)\n"
  74. " beq %1, $0, 1b\n"
  75. " nop\n"
  76. "1: \n"
  77. ".set pop\n"
  78. : "=&r"(old), "=&r"(dummy) : "r"(x), "r"(v) : "memory" );
  79. return old;
  80. }
  81. static inline void a_inc(volatile int *x)
  82. {
  83. int dummy;
  84. __asm__ __volatile__(
  85. ".set push\n"
  86. ".set mips2\n"
  87. ".set noreorder\n"
  88. "1: ll %0, 0(%1)\n"
  89. " addu %0, %0, 1\n"
  90. " sc %0, 0(%1)\n"
  91. " beq %0, $0, 1b\n"
  92. " nop\n"
  93. "1: \n"
  94. ".set pop\n"
  95. : "=&r"(dummy) : "r"(x) : "memory" );
  96. }
  97. static inline void a_dec(volatile int *x)
  98. {
  99. int dummy;
  100. __asm__ __volatile__(
  101. ".set push\n"
  102. ".set mips2\n"
  103. ".set noreorder\n"
  104. "1: ll %0, 0(%1)\n"
  105. " subu %0, %0, 1\n"
  106. " sc %0, 0(%1)\n"
  107. " beq %0, $0, 1b\n"
  108. " nop\n"
  109. "1: \n"
  110. ".set pop\n"
  111. : "=&r"(dummy) : "r"(x) : "memory" );
  112. }
  113. static inline void a_store(volatile int *p, int x)
  114. {
  115. int dummy;
  116. __asm__ __volatile__(
  117. ".set push\n"
  118. ".set mips2\n"
  119. ".set noreorder\n"
  120. "1: ll %0, 0(%1)\n"
  121. " addu %0, %2, $0\n"
  122. " sc %0, 0(%1)\n"
  123. " beq %0, $0, 1b\n"
  124. " nop\n"
  125. "1: \n"
  126. ".set pop\n"
  127. : "=&r"(dummy) : "r"(p), "r"(x) : "memory" );
  128. }
  129. static inline void a_spin()
  130. {
  131. }
  132. static inline void a_crash()
  133. {
  134. *(volatile char *)0=0;
  135. }
  136. static inline void a_and(volatile int *p, int v)
  137. {
  138. int dummy;
  139. __asm__ __volatile__(
  140. ".set push\n"
  141. ".set mips2\n"
  142. ".set noreorder\n"
  143. "1: ll %0, 0(%1)\n"
  144. " and %0, %0, %2\n"
  145. " sc %0, 0(%1)\n"
  146. " beq %0, $0, 1b\n"
  147. " nop\n"
  148. "1: \n"
  149. ".set pop\n"
  150. : "=&r"(dummy) : "r"(p), "r"(v) : "memory" );
  151. }
  152. static inline void a_or(volatile int *p, int v)
  153. {
  154. int dummy;
  155. __asm__ __volatile__(
  156. ".set push\n"
  157. ".set mips2\n"
  158. ".set noreorder\n"
  159. "1: ll %0, 0(%1)\n"
  160. " or %0, %0, %2\n"
  161. " sc %0, 0(%1)\n"
  162. " beq %0, $0, 1b\n"
  163. " nop\n"
  164. "1: \n"
  165. ".set pop\n"
  166. : "=&r"(dummy) : "r"(p), "r"(v) : "memory" );
  167. }
  168. static inline void a_and_64(volatile uint64_t *p, uint64_t v)
  169. {
  170. union { uint64_t v; uint32_t r[2]; } u = { v };
  171. a_and((int *)p, u.r[0]);
  172. a_and((int *)p+1, u.r[1]);
  173. }
  174. static inline void a_or_64(volatile uint64_t *p, uint64_t v)
  175. {
  176. union { uint64_t v; uint32_t r[2]; } u = { v };
  177. a_or((int *)p, u.r[0]);
  178. a_or((int *)p+1, u.r[1]);
  179. }
  180. #endif