atomic.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  2. * Copyright (C) 2006 Kyle McMartin <kyle@parisc-linux.org>
  3. */
  4. #ifndef _ASM_PARISC_ATOMIC_H_
  5. #define _ASM_PARISC_ATOMIC_H_
  6. #include <linux/types.h>
  7. #include <asm/cmpxchg.h>
  8. #include <asm/barrier.h>
  9. /*
  10. * Atomic operations that C can't guarantee us. Useful for
  11. * resource counting etc..
  12. *
  13. * And probably incredibly slow on parisc. OTOH, we don't
  14. * have to write any serious assembly. prumpf
  15. */
  16. #ifdef CONFIG_SMP
  17. #include <asm/spinlock.h>
  18. #include <asm/cache.h> /* we use L1_CACHE_BYTES */
  19. /* Use an array of spinlocks for our atomic_ts.
  20. * Hash function to index into a different SPINLOCK.
  21. * Since "a" is usually an address, use one spinlock per cacheline.
  22. */
  23. # define ATOMIC_HASH_SIZE 4
  24. # define ATOMIC_HASH(a) (&(__atomic_hash[ (((unsigned long) (a))/L1_CACHE_BYTES) & (ATOMIC_HASH_SIZE-1) ]))
  25. extern arch_spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;
  26. /* Can't use raw_spin_lock_irq because of #include problems, so
  27. * this is the substitute */
  28. #define _atomic_spin_lock_irqsave(l,f) do { \
  29. arch_spinlock_t *s = ATOMIC_HASH(l); \
  30. local_irq_save(f); \
  31. arch_spin_lock(s); \
  32. } while(0)
  33. #define _atomic_spin_unlock_irqrestore(l,f) do { \
  34. arch_spinlock_t *s = ATOMIC_HASH(l); \
  35. arch_spin_unlock(s); \
  36. local_irq_restore(f); \
  37. } while(0)
  38. #else
  39. # define _atomic_spin_lock_irqsave(l,f) do { local_irq_save(f); } while (0)
  40. # define _atomic_spin_unlock_irqrestore(l,f) do { local_irq_restore(f); } while (0)
  41. #endif
  42. /*
  43. * Note that we need not lock read accesses - aligned word writes/reads
  44. * are atomic, so a reader never sees inconsistent values.
  45. */
  46. static __inline__ void atomic_set(atomic_t *v, int i)
  47. {
  48. unsigned long flags;
  49. _atomic_spin_lock_irqsave(v, flags);
  50. v->counter = i;
  51. _atomic_spin_unlock_irqrestore(v, flags);
  52. }
  53. static __inline__ int atomic_read(const atomic_t *v)
  54. {
  55. return ACCESS_ONCE((v)->counter);
  56. }
  57. /* exported interface */
  58. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  59. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  60. /**
  61. * __atomic_add_unless - add unless the number is a given value
  62. * @v: pointer of type atomic_t
  63. * @a: the amount to add to v...
  64. * @u: ...unless v is equal to u.
  65. *
  66. * Atomically adds @a to @v, so long as it was not @u.
  67. * Returns the old value of @v.
  68. */
  69. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  70. {
  71. int c, old;
  72. c = atomic_read(v);
  73. for (;;) {
  74. if (unlikely(c == (u)))
  75. break;
  76. old = atomic_cmpxchg((v), c, c + (a));
  77. if (likely(old == c))
  78. break;
  79. c = old;
  80. }
  81. return c;
  82. }
  83. #define ATOMIC_OP(op, c_op) \
  84. static __inline__ void atomic_##op(int i, atomic_t *v) \
  85. { \
  86. unsigned long flags; \
  87. \
  88. _atomic_spin_lock_irqsave(v, flags); \
  89. v->counter c_op i; \
  90. _atomic_spin_unlock_irqrestore(v, flags); \
  91. } \
  92. #define ATOMIC_OP_RETURN(op, c_op) \
  93. static __inline__ int atomic_##op##_return(int i, atomic_t *v) \
  94. { \
  95. unsigned long flags; \
  96. int ret; \
  97. \
  98. _atomic_spin_lock_irqsave(v, flags); \
  99. ret = (v->counter c_op i); \
  100. _atomic_spin_unlock_irqrestore(v, flags); \
  101. \
  102. return ret; \
  103. }
  104. #define ATOMIC_OPS(op, c_op) ATOMIC_OP(op, c_op) ATOMIC_OP_RETURN(op, c_op)
  105. ATOMIC_OPS(add, +=)
  106. ATOMIC_OPS(sub, -=)
  107. #undef ATOMIC_OPS
  108. #undef ATOMIC_OP_RETURN
  109. #undef ATOMIC_OP
  110. #define atomic_inc(v) (atomic_add( 1,(v)))
  111. #define atomic_dec(v) (atomic_add( -1,(v)))
  112. #define atomic_inc_return(v) (atomic_add_return( 1,(v)))
  113. #define atomic_dec_return(v) (atomic_add_return( -1,(v)))
  114. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  115. /*
  116. * atomic_inc_and_test - increment and test
  117. * @v: pointer of type atomic_t
  118. *
  119. * Atomically increments @v by 1
  120. * and returns true if the result is zero, or false for all
  121. * other cases.
  122. */
  123. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  124. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  125. #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
  126. #define ATOMIC_INIT(i) { (i) }
  127. #ifdef CONFIG_64BIT
  128. #define ATOMIC64_INIT(i) { (i) }
  129. #define ATOMIC64_OP(op, c_op) \
  130. static __inline__ void atomic64_##op(s64 i, atomic64_t *v) \
  131. { \
  132. unsigned long flags; \
  133. \
  134. _atomic_spin_lock_irqsave(v, flags); \
  135. v->counter c_op i; \
  136. _atomic_spin_unlock_irqrestore(v, flags); \
  137. } \
  138. #define ATOMIC64_OP_RETURN(op, c_op) \
  139. static __inline__ s64 atomic64_##op##_return(s64 i, atomic64_t *v) \
  140. { \
  141. unsigned long flags; \
  142. s64 ret; \
  143. \
  144. _atomic_spin_lock_irqsave(v, flags); \
  145. ret = (v->counter c_op i); \
  146. _atomic_spin_unlock_irqrestore(v, flags); \
  147. \
  148. return ret; \
  149. }
  150. #define ATOMIC64_OPS(op, c_op) ATOMIC64_OP(op, c_op) ATOMIC64_OP_RETURN(op, c_op)
  151. ATOMIC64_OPS(add, +=)
  152. ATOMIC64_OPS(sub, -=)
  153. #undef ATOMIC64_OPS
  154. #undef ATOMIC64_OP_RETURN
  155. #undef ATOMIC64_OP
  156. static __inline__ void
  157. atomic64_set(atomic64_t *v, s64 i)
  158. {
  159. unsigned long flags;
  160. _atomic_spin_lock_irqsave(v, flags);
  161. v->counter = i;
  162. _atomic_spin_unlock_irqrestore(v, flags);
  163. }
  164. static __inline__ s64
  165. atomic64_read(const atomic64_t *v)
  166. {
  167. return ACCESS_ONCE((v)->counter);
  168. }
  169. #define atomic64_inc(v) (atomic64_add( 1,(v)))
  170. #define atomic64_dec(v) (atomic64_add( -1,(v)))
  171. #define atomic64_inc_return(v) (atomic64_add_return( 1,(v)))
  172. #define atomic64_dec_return(v) (atomic64_add_return( -1,(v)))
  173. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  174. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  175. #define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
  176. #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i),(v)) == 0)
  177. /* exported interface */
  178. #define atomic64_cmpxchg(v, o, n) \
  179. ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
  180. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  181. /**
  182. * atomic64_add_unless - add unless the number is a given value
  183. * @v: pointer of type atomic64_t
  184. * @a: the amount to add to v...
  185. * @u: ...unless v is equal to u.
  186. *
  187. * Atomically adds @a to @v, so long as it was not @u.
  188. * Returns the old value of @v.
  189. */
  190. static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
  191. {
  192. long c, old;
  193. c = atomic64_read(v);
  194. for (;;) {
  195. if (unlikely(c == (u)))
  196. break;
  197. old = atomic64_cmpxchg((v), c, c + (a));
  198. if (likely(old == c))
  199. break;
  200. c = old;
  201. }
  202. return c != (u);
  203. }
  204. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  205. /*
  206. * atomic64_dec_if_positive - decrement by 1 if old value positive
  207. * @v: pointer of type atomic_t
  208. *
  209. * The function returns the old value of *v minus 1, even if
  210. * the atomic variable, v, was not decremented.
  211. */
  212. static inline long atomic64_dec_if_positive(atomic64_t *v)
  213. {
  214. long c, old, dec;
  215. c = atomic64_read(v);
  216. for (;;) {
  217. dec = c - 1;
  218. if (unlikely(dec < 0))
  219. break;
  220. old = atomic64_cmpxchg((v), c, dec);
  221. if (likely(old == c))
  222. break;
  223. c = old;
  224. }
  225. return dec;
  226. }
  227. #endif /* !CONFIG_64BIT */
  228. #endif /* _ASM_PARISC_ATOMIC_H_ */