atomic.h 1.7 KB

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