atomic.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 READ_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_FETCH_OP(op, c_op) \
  105. static __inline__ int atomic_fetch_##op(int i, atomic_t *v) \
  106. { \
  107. unsigned long flags; \
  108. int ret; \
  109. \
  110. _atomic_spin_lock_irqsave(v, flags); \
  111. ret = v->counter; \
  112. v->counter c_op i; \
  113. _atomic_spin_unlock_irqrestore(v, flags); \
  114. \
  115. return ret; \
  116. }
  117. #define ATOMIC_OPS(op, c_op) \
  118. ATOMIC_OP(op, c_op) \
  119. ATOMIC_OP_RETURN(op, c_op) \
  120. ATOMIC_FETCH_OP(op, c_op)
  121. ATOMIC_OPS(add, +=)
  122. ATOMIC_OPS(sub, -=)
  123. #undef ATOMIC_OPS
  124. #define ATOMIC_OPS(op, c_op) \
  125. ATOMIC_OP(op, c_op) \
  126. ATOMIC_FETCH_OP(op, c_op)
  127. ATOMIC_OPS(and, &=)
  128. ATOMIC_OPS(or, |=)
  129. ATOMIC_OPS(xor, ^=)
  130. #undef ATOMIC_OPS
  131. #undef ATOMIC_FETCH_OP
  132. #undef ATOMIC_OP_RETURN
  133. #undef ATOMIC_OP
  134. #define atomic_inc(v) (atomic_add( 1,(v)))
  135. #define atomic_dec(v) (atomic_add( -1,(v)))
  136. #define atomic_inc_return(v) (atomic_add_return( 1,(v)))
  137. #define atomic_dec_return(v) (atomic_add_return( -1,(v)))
  138. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  139. /*
  140. * atomic_inc_and_test - increment and test
  141. * @v: pointer of type atomic_t
  142. *
  143. * Atomically increments @v by 1
  144. * and returns true if the result is zero, or false for all
  145. * other cases.
  146. */
  147. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  148. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  149. #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
  150. #define ATOMIC_INIT(i) { (i) }
  151. #ifdef CONFIG_64BIT
  152. #define ATOMIC64_INIT(i) { (i) }
  153. #define ATOMIC64_OP(op, c_op) \
  154. static __inline__ void atomic64_##op(s64 i, atomic64_t *v) \
  155. { \
  156. unsigned long flags; \
  157. \
  158. _atomic_spin_lock_irqsave(v, flags); \
  159. v->counter c_op i; \
  160. _atomic_spin_unlock_irqrestore(v, flags); \
  161. } \
  162. #define ATOMIC64_OP_RETURN(op, c_op) \
  163. static __inline__ s64 atomic64_##op##_return(s64 i, atomic64_t *v) \
  164. { \
  165. unsigned long flags; \
  166. s64 ret; \
  167. \
  168. _atomic_spin_lock_irqsave(v, flags); \
  169. ret = (v->counter c_op i); \
  170. _atomic_spin_unlock_irqrestore(v, flags); \
  171. \
  172. return ret; \
  173. }
  174. #define ATOMIC64_FETCH_OP(op, c_op) \
  175. static __inline__ s64 atomic64_fetch_##op(s64 i, atomic64_t *v) \
  176. { \
  177. unsigned long flags; \
  178. s64 ret; \
  179. \
  180. _atomic_spin_lock_irqsave(v, flags); \
  181. ret = v->counter; \
  182. v->counter c_op i; \
  183. _atomic_spin_unlock_irqrestore(v, flags); \
  184. \
  185. return ret; \
  186. }
  187. #define ATOMIC64_OPS(op, c_op) \
  188. ATOMIC64_OP(op, c_op) \
  189. ATOMIC64_OP_RETURN(op, c_op) \
  190. ATOMIC64_FETCH_OP(op, c_op)
  191. ATOMIC64_OPS(add, +=)
  192. ATOMIC64_OPS(sub, -=)
  193. #undef ATOMIC64_OPS
  194. #define ATOMIC64_OPS(op, c_op) \
  195. ATOMIC64_OP(op, c_op) \
  196. ATOMIC64_FETCH_OP(op, c_op)
  197. ATOMIC64_OPS(and, &=)
  198. ATOMIC64_OPS(or, |=)
  199. ATOMIC64_OPS(xor, ^=)
  200. #undef ATOMIC64_OPS
  201. #undef ATOMIC64_FETCH_OP
  202. #undef ATOMIC64_OP_RETURN
  203. #undef ATOMIC64_OP
  204. static __inline__ void
  205. atomic64_set(atomic64_t *v, s64 i)
  206. {
  207. unsigned long flags;
  208. _atomic_spin_lock_irqsave(v, flags);
  209. v->counter = i;
  210. _atomic_spin_unlock_irqrestore(v, flags);
  211. }
  212. static __inline__ s64
  213. atomic64_read(const atomic64_t *v)
  214. {
  215. return ACCESS_ONCE((v)->counter);
  216. }
  217. #define atomic64_inc(v) (atomic64_add( 1,(v)))
  218. #define atomic64_dec(v) (atomic64_add( -1,(v)))
  219. #define atomic64_inc_return(v) (atomic64_add_return( 1,(v)))
  220. #define atomic64_dec_return(v) (atomic64_add_return( -1,(v)))
  221. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  222. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  223. #define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
  224. #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i),(v)) == 0)
  225. /* exported interface */
  226. #define atomic64_cmpxchg(v, o, n) \
  227. ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
  228. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  229. /**
  230. * atomic64_add_unless - add unless the number is a given value
  231. * @v: pointer of type atomic64_t
  232. * @a: the amount to add to v...
  233. * @u: ...unless v is equal to u.
  234. *
  235. * Atomically adds @a to @v, so long as it was not @u.
  236. * Returns the old value of @v.
  237. */
  238. static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
  239. {
  240. long c, old;
  241. c = atomic64_read(v);
  242. for (;;) {
  243. if (unlikely(c == (u)))
  244. break;
  245. old = atomic64_cmpxchg((v), c, c + (a));
  246. if (likely(old == c))
  247. break;
  248. c = old;
  249. }
  250. return c != (u);
  251. }
  252. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  253. /*
  254. * atomic64_dec_if_positive - decrement by 1 if old value positive
  255. * @v: pointer of type atomic_t
  256. *
  257. * The function returns the old value of *v minus 1, even if
  258. * the atomic variable, v, was not decremented.
  259. */
  260. static inline long atomic64_dec_if_positive(atomic64_t *v)
  261. {
  262. long c, old, dec;
  263. c = atomic64_read(v);
  264. for (;;) {
  265. dec = c - 1;
  266. if (unlikely(dec < 0))
  267. break;
  268. old = atomic64_cmpxchg((v), c, dec);
  269. if (likely(old == c))
  270. break;
  271. c = old;
  272. }
  273. return dec;
  274. }
  275. #endif /* !CONFIG_64BIT */
  276. #endif /* _ASM_PARISC_ATOMIC_H_ */