atomic.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. int __sh_cas(volatile int *, int, int);
  22. int __sh_swap(volatile int *, int);
  23. int __sh_fetch_add(volatile int *, int);
  24. void __sh_store(volatile int *, int);
  25. void __sh_and(volatile int *, int);
  26. void __sh_or(volatile int *, int);
  27. #define a_cas(p,t,s) __sh_cas(p,t,s)
  28. #define a_swap(x,v) __sh_swap(x,v)
  29. #define a_fetch_add(x,v) __sh_fetch_add(x, v)
  30. #define a_store(x,v) __sh_store(x, v)
  31. #define a_and(x,v) __sh_and(x, v)
  32. #define a_or(x,v) __sh_or(x, v)
  33. static inline void *a_cas_p(volatile void *p, void *t, void *s)
  34. {
  35. return (void *)a_cas(p, (int)t, (int)s);
  36. }
  37. static inline void a_inc(volatile int *x)
  38. {
  39. a_fetch_add(x, 1);
  40. }
  41. static inline void a_dec(volatile int *x)
  42. {
  43. a_fetch_add(x, -1);
  44. }
  45. #define a_spin a_barrier
  46. static inline void a_barrier()
  47. {
  48. a_cas(&(int){0}, 0, 0);
  49. }
  50. static inline void a_crash()
  51. {
  52. *(volatile char *)0=0;
  53. }
  54. static inline void a_or_l(volatile void *p, long v)
  55. {
  56. a_or(p, v);
  57. }
  58. static inline void a_and_64(volatile uint64_t *p, uint64_t v)
  59. {
  60. union { uint64_t v; uint32_t r[2]; } u = { v };
  61. a_and((int *)p, u.r[0]);
  62. a_and((int *)p+1, u.r[1]);
  63. }
  64. static inline void a_or_64(volatile uint64_t *p, uint64_t v)
  65. {
  66. union { uint64_t v; uint32_t r[2]; } u = { v };
  67. a_or((int *)p, u.r[0]);
  68. a_or((int *)p+1, u.r[1]);
  69. }
  70. #endif