atomic.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. static inline void a_spin()
  46. {
  47. a_cas(&(int){0}, 0, 0);
  48. }
  49. static inline void a_crash()
  50. {
  51. *(volatile char *)0=0;
  52. }
  53. static inline void a_or_l(volatile void *p, long v)
  54. {
  55. a_or(p, v);
  56. }
  57. static inline void a_and_64(volatile uint64_t *p, uint64_t v)
  58. {
  59. union { uint64_t v; uint32_t r[2]; } u = { v };
  60. a_and((int *)p, u.r[0]);
  61. a_and((int *)p+1, u.r[1]);
  62. }
  63. static inline void a_or_64(volatile uint64_t *p, uint64_t v)
  64. {
  65. union { uint64_t v; uint32_t r[2]; } u = { v };
  66. a_or((int *)p, u.r[0]);
  67. a_or((int *)p+1, u.r[1]);
  68. }
  69. #endif