atomic_arch.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #define a_ll a_ll
  2. static inline int a_ll(volatile int *p)
  3. {
  4. int v;
  5. __asm__ __volatile__ ("ldaxr %w0,%1" : "=r"(v) : "Q"(*p));
  6. return v;
  7. }
  8. #define a_sc a_sc
  9. static inline int a_sc(volatile int *p, int v)
  10. {
  11. int r;
  12. __asm__ __volatile__ ("stlxr %w0,%w2,%1" : "=&r"(r), "=Q"(*p) : "r"(v) : "memory");
  13. return !r;
  14. }
  15. #define a_barrier a_barrier
  16. static inline void a_barrier()
  17. {
  18. __asm__ __volatile__ ("dmb ish" : : : "memory");
  19. }
  20. #define a_cas a_cas
  21. static inline int a_cas(volatile int *p, int t, int s)
  22. {
  23. int old;
  24. do {
  25. old = a_ll(p);
  26. if (old != t) {
  27. a_barrier();
  28. break;
  29. }
  30. } while (!a_sc(p, s));
  31. return old;
  32. }
  33. #define a_ll_p a_ll_p
  34. static inline void *a_ll_p(volatile void *p)
  35. {
  36. void *v;
  37. __asm__ __volatile__ ("ldaxr %0, %1" : "=r"(v) : "Q"(*(void *volatile *)p));
  38. return v;
  39. }
  40. #define a_sc_p a_sc_p
  41. static inline int a_sc_p(volatile int *p, void *v)
  42. {
  43. int r;
  44. __asm__ __volatile__ ("stlxr %w0,%2,%1" : "=&r"(r), "=Q"(*(void *volatile *)p) : "r"(v) : "memory");
  45. return !r;
  46. }
  47. #define a_cas_p a_cas_p
  48. static inline void *a_cas_p(volatile void *p, void *t, void *s)
  49. {
  50. void *old;
  51. do {
  52. old = a_ll_p(p);
  53. if (old != t) {
  54. a_barrier();
  55. break;
  56. }
  57. } while (!a_sc_p(p, s));
  58. return old;
  59. }
  60. #define a_ctz_64 a_ctz_64
  61. static inline int a_ctz_64(uint64_t x)
  62. {
  63. __asm__(
  64. " rbit %0, %1\n"
  65. " clz %0, %0\n"
  66. : "=r"(x) : "r"(x));
  67. return x;
  68. }
  69. #define a_clz_64 a_clz_64
  70. static inline int a_clz_64(uint64_t x)
  71. {
  72. __asm__("clz %0, %1" : "=r"(x) : "r"(x));
  73. return x;
  74. }