atomic.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef _ASM_IA64_ATOMIC_H
  2. #define _ASM_IA64_ATOMIC_H
  3. /*
  4. * Atomic operations that C can't guarantee us. Useful for
  5. * resource counting etc..
  6. *
  7. * NOTE: don't mess with the types below! The "unsigned long" and
  8. * "int" types were carefully placed so as to ensure proper operation
  9. * of the macros.
  10. *
  11. * Copyright (C) 1998, 1999, 2002-2003 Hewlett-Packard Co
  12. * David Mosberger-Tang <davidm@hpl.hp.com>
  13. */
  14. #include <linux/types.h>
  15. #include <asm/intrinsics.h>
  16. #include <asm/barrier.h>
  17. #define ATOMIC_INIT(i) { (i) }
  18. #define ATOMIC64_INIT(i) { (i) }
  19. #define atomic_read(v) ACCESS_ONCE((v)->counter)
  20. #define atomic64_read(v) ACCESS_ONCE((v)->counter)
  21. #define atomic_set(v,i) (((v)->counter) = (i))
  22. #define atomic64_set(v,i) (((v)->counter) = (i))
  23. #define ATOMIC_OP(op, c_op) \
  24. static __inline__ int \
  25. ia64_atomic_##op (int i, atomic_t *v) \
  26. { \
  27. __s32 old, new; \
  28. CMPXCHG_BUGCHECK_DECL \
  29. \
  30. do { \
  31. CMPXCHG_BUGCHECK(v); \
  32. old = atomic_read(v); \
  33. new = old c_op i; \
  34. } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old); \
  35. return new; \
  36. }
  37. ATOMIC_OP(add, +)
  38. ATOMIC_OP(sub, -)
  39. #undef ATOMIC_OP
  40. #define atomic_add_return(i,v) \
  41. ({ \
  42. int __ia64_aar_i = (i); \
  43. (__builtin_constant_p(i) \
  44. && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \
  45. || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \
  46. || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \
  47. || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \
  48. ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
  49. : ia64_atomic_add(__ia64_aar_i, v); \
  50. })
  51. #define atomic_sub_return(i,v) \
  52. ({ \
  53. int __ia64_asr_i = (i); \
  54. (__builtin_constant_p(i) \
  55. && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \
  56. || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \
  57. || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \
  58. || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \
  59. ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
  60. : ia64_atomic_sub(__ia64_asr_i, v); \
  61. })
  62. #define ATOMIC64_OP(op, c_op) \
  63. static __inline__ long \
  64. ia64_atomic64_##op (__s64 i, atomic64_t *v) \
  65. { \
  66. __s64 old, new; \
  67. CMPXCHG_BUGCHECK_DECL \
  68. \
  69. do { \
  70. CMPXCHG_BUGCHECK(v); \
  71. old = atomic64_read(v); \
  72. new = old c_op i; \
  73. } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old); \
  74. return new; \
  75. }
  76. ATOMIC64_OP(add, +)
  77. ATOMIC64_OP(sub, -)
  78. #undef ATOMIC64_OP
  79. #define atomic64_add_return(i,v) \
  80. ({ \
  81. long __ia64_aar_i = (i); \
  82. (__builtin_constant_p(i) \
  83. && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \
  84. || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \
  85. || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \
  86. || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \
  87. ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
  88. : ia64_atomic64_add(__ia64_aar_i, v); \
  89. })
  90. #define atomic64_sub_return(i,v) \
  91. ({ \
  92. long __ia64_asr_i = (i); \
  93. (__builtin_constant_p(i) \
  94. && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \
  95. || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \
  96. || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \
  97. || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \
  98. ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
  99. : ia64_atomic64_sub(__ia64_asr_i, v); \
  100. })
  101. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
  102. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  103. #define atomic64_cmpxchg(v, old, new) \
  104. (cmpxchg(&((v)->counter), old, new))
  105. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  106. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  107. {
  108. int c, old;
  109. c = atomic_read(v);
  110. for (;;) {
  111. if (unlikely(c == (u)))
  112. break;
  113. old = atomic_cmpxchg((v), c, c + (a));
  114. if (likely(old == c))
  115. break;
  116. c = old;
  117. }
  118. return c;
  119. }
  120. static __inline__ long atomic64_add_unless(atomic64_t *v, long a, long u)
  121. {
  122. long c, old;
  123. c = atomic64_read(v);
  124. for (;;) {
  125. if (unlikely(c == (u)))
  126. break;
  127. old = atomic64_cmpxchg((v), c, c + (a));
  128. if (likely(old == c))
  129. break;
  130. c = old;
  131. }
  132. return c != (u);
  133. }
  134. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  135. /*
  136. * Atomically add I to V and return TRUE if the resulting value is
  137. * negative.
  138. */
  139. static __inline__ int
  140. atomic_add_negative (int i, atomic_t *v)
  141. {
  142. return atomic_add_return(i, v) < 0;
  143. }
  144. static __inline__ long
  145. atomic64_add_negative (__s64 i, atomic64_t *v)
  146. {
  147. return atomic64_add_return(i, v) < 0;
  148. }
  149. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  150. #define atomic_inc_return(v) atomic_add_return(1, (v))
  151. #define atomic64_dec_return(v) atomic64_sub_return(1, (v))
  152. #define atomic64_inc_return(v) atomic64_add_return(1, (v))
  153. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  154. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  155. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  156. #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
  157. #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
  158. #define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
  159. #define atomic_add(i,v) (void)atomic_add_return((i), (v))
  160. #define atomic_sub(i,v) (void)atomic_sub_return((i), (v))
  161. #define atomic_inc(v) atomic_add(1, (v))
  162. #define atomic_dec(v) atomic_sub(1, (v))
  163. #define atomic64_add(i,v) (void)atomic64_add_return((i), (v))
  164. #define atomic64_sub(i,v) (void)atomic64_sub_return((i), (v))
  165. #define atomic64_inc(v) atomic64_add(1, (v))
  166. #define atomic64_dec(v) atomic64_sub(1, (v))
  167. #endif /* _ASM_IA64_ATOMIC_H */