atomic.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef _INTERNAL_ATOMIC_H
  2. #define _INTERNAL_ATOMIC_H
  3. #include <stdint.h>
  4. static inline int a_ctz_64(uint64_t x)
  5. {
  6. __asm__( "bsf %1,%0" : "=r"(x) : "r"(x) );
  7. return x;
  8. }
  9. static inline int a_ctz_l(unsigned long x)
  10. {
  11. __asm__( "bsf %1,%0" : "=r"(x) : "r"(x) );
  12. return x;
  13. }
  14. static inline void a_and_64(volatile uint64_t *p, uint64_t v)
  15. {
  16. __asm__( "lock ; and %1, %0"
  17. : "=m"(*p) : "r"(v) : "memory" );
  18. }
  19. static inline void a_or_64(volatile uint64_t *p, uint64_t v)
  20. {
  21. __asm__( "lock ; or %1, %0"
  22. : "=m"(*p) : "r"(v) : "memory" );
  23. }
  24. static inline void a_or_l(volatile void *p, long v)
  25. {
  26. __asm__( "lock ; or %1, %0"
  27. : "=m"(*(long *)p) : "r"(v) : "memory" );
  28. }
  29. static inline void *a_cas_p(volatile void *p, void *t, void *s)
  30. {
  31. __asm__( "lock ; cmpxchg %3, %1"
  32. : "=a"(t), "=m"(*(long *)p) : "a"(t), "r"(s) : "memory" );
  33. return t;
  34. }
  35. static inline int a_cas(volatile int *p, int t, int s)
  36. {
  37. __asm__( "lock ; cmpxchg %3, %1"
  38. : "=a"(t), "=m"(*p) : "a"(t), "r"(s) : "memory" );
  39. return t;
  40. }
  41. static inline void a_or(volatile void *p, int v)
  42. {
  43. __asm__( "lock ; or %1, %0"
  44. : "=m"(*(int *)p) : "r"(v) : "memory" );
  45. }
  46. static inline void a_and(volatile void *p, int v)
  47. {
  48. __asm__( "lock ; and %1, %0"
  49. : "=m"(*(int *)p) : "r"(v) : "memory" );
  50. }
  51. static inline int a_swap(volatile int *x, int v)
  52. {
  53. __asm__( "xchg %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory" );
  54. return v;
  55. }
  56. static inline int a_fetch_add(volatile int *x, int v)
  57. {
  58. __asm__( "lock ; xadd %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory" );
  59. return v;
  60. }
  61. static inline void a_inc(volatile int *x)
  62. {
  63. __asm__( "lock ; incl %0" : "=m"(*x) : "m"(*x) : "memory" );
  64. }
  65. static inline void a_dec(volatile int *x)
  66. {
  67. __asm__( "lock ; decl %0" : "=m"(*x) : "m"(*x) : "memory" );
  68. }
  69. static inline void a_store(volatile int *p, int x)
  70. {
  71. __asm__( "mov %1, %0" : "=m"(*p) : "r"(x) : "memory" );
  72. }
  73. static inline void a_spin()
  74. {
  75. __asm__ __volatile__( "pause" : : : "memory" );
  76. }
  77. static inline void a_crash()
  78. {
  79. __asm__ __volatile__( "hlt" : : : "memory" );
  80. }
  81. #endif