atomic.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /*
  17. * On IA-64, counter must always be volatile to ensure that that the
  18. * memory accesses are ordered.
  19. */
  20. typedef struct { volatile __s32 counter; } atomic_t;
  21. typedef struct { volatile __s64 counter; } atomic64_t;
  22. #define ATOMIC_INIT(i) ((atomic_t) { (i) })
  23. #define ATOMIC64_INIT(i) ((atomic64_t) { (i) })
  24. #define atomic_read(v) ((v)->counter)
  25. #define atomic64_read(v) ((v)->counter)
  26. #define atomic_set(v,i) (((v)->counter) = (i))
  27. #define atomic64_set(v,i) (((v)->counter) = (i))
  28. static __inline__ int
  29. ia64_atomic_add (int i, atomic_t *v)
  30. {
  31. __s32 old, new;
  32. CMPXCHG_BUGCHECK_DECL
  33. do {
  34. CMPXCHG_BUGCHECK(v);
  35. old = atomic_read(v);
  36. new = old + i;
  37. } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old);
  38. return new;
  39. }
  40. static __inline__ int
  41. ia64_atomic64_add (__s64 i, atomic64_t *v)
  42. {
  43. __s64 old, new;
  44. CMPXCHG_BUGCHECK_DECL
  45. do {
  46. CMPXCHG_BUGCHECK(v);
  47. old = atomic_read(v);
  48. new = old + i;
  49. } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old);
  50. return new;
  51. }
  52. static __inline__ int
  53. ia64_atomic_sub (int i, atomic_t *v)
  54. {
  55. __s32 old, new;
  56. CMPXCHG_BUGCHECK_DECL
  57. do {
  58. CMPXCHG_BUGCHECK(v);
  59. old = atomic_read(v);
  60. new = old - i;
  61. } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old);
  62. return new;
  63. }
  64. static __inline__ int
  65. ia64_atomic64_sub (__s64 i, atomic64_t *v)
  66. {
  67. __s64 old, new;
  68. CMPXCHG_BUGCHECK_DECL
  69. do {
  70. CMPXCHG_BUGCHECK(v);
  71. old = atomic_read(v);
  72. new = old - i;
  73. } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old);
  74. return new;
  75. }
  76. #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
  77. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  78. #define atomic_add_unless(v, a, u) \
  79. ({ \
  80. int c, old; \
  81. c = atomic_read(v); \
  82. for (;;) { \
  83. if (unlikely(c == (u))) \
  84. break; \
  85. old = atomic_cmpxchg((v), c, c + (a)); \
  86. if (likely(old == c)) \
  87. break; \
  88. c = old; \
  89. } \
  90. c != (u); \
  91. })
  92. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  93. #define atomic_add_return(i,v) \
  94. ({ \
  95. int __ia64_aar_i = (i); \
  96. (__builtin_constant_p(i) \
  97. && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \
  98. || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \
  99. || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \
  100. || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \
  101. ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
  102. : ia64_atomic_add(__ia64_aar_i, v); \
  103. })
  104. #define atomic64_add_return(i,v) \
  105. ({ \
  106. long __ia64_aar_i = (i); \
  107. (__builtin_constant_p(i) \
  108. && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \
  109. || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \
  110. || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \
  111. || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \
  112. ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
  113. : ia64_atomic64_add(__ia64_aar_i, v); \
  114. })
  115. /*
  116. * Atomically add I to V and return TRUE if the resulting value is
  117. * negative.
  118. */
  119. static __inline__ int
  120. atomic_add_negative (int i, atomic_t *v)
  121. {
  122. return atomic_add_return(i, v) < 0;
  123. }
  124. static __inline__ int
  125. atomic64_add_negative (__s64 i, atomic64_t *v)
  126. {
  127. return atomic64_add_return(i, v) < 0;
  128. }
  129. #define atomic_sub_return(i,v) \
  130. ({ \
  131. int __ia64_asr_i = (i); \
  132. (__builtin_constant_p(i) \
  133. && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \
  134. || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \
  135. || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \
  136. || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \
  137. ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
  138. : ia64_atomic_sub(__ia64_asr_i, v); \
  139. })
  140. #define atomic64_sub_return(i,v) \
  141. ({ \
  142. long __ia64_asr_i = (i); \
  143. (__builtin_constant_p(i) \
  144. && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \
  145. || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \
  146. || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \
  147. || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \
  148. ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
  149. : ia64_atomic64_sub(__ia64_asr_i, v); \
  150. })
  151. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  152. #define atomic_inc_return(v) atomic_add_return(1, (v))
  153. #define atomic64_dec_return(v) atomic64_sub_return(1, (v))
  154. #define atomic64_inc_return(v) atomic64_add_return(1, (v))
  155. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  156. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  157. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  158. #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
  159. #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
  160. #define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
  161. #define atomic_add(i,v) atomic_add_return((i), (v))
  162. #define atomic_sub(i,v) atomic_sub_return((i), (v))
  163. #define atomic_inc(v) atomic_add(1, (v))
  164. #define atomic_dec(v) atomic_sub(1, (v))
  165. #define atomic64_add(i,v) atomic64_add_return((i), (v))
  166. #define atomic64_sub(i,v) atomic64_sub_return((i), (v))
  167. #define atomic64_inc(v) atomic64_add(1, (v))
  168. #define atomic64_dec(v) atomic64_sub(1, (v))
  169. /* Atomic operations are already serializing */
  170. #define smp_mb__before_atomic_dec() barrier()
  171. #define smp_mb__after_atomic_dec() barrier()
  172. #define smp_mb__before_atomic_inc() barrier()
  173. #define smp_mb__after_atomic_inc() barrier()
  174. #include <asm-generic/atomic.h>
  175. #endif /* _ASM_IA64_ATOMIC_H */