atomic.h 2.5 KB

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