atomic.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /*
  9. * Atomic operations that C can't guarantee us. Useful for
  10. * resource counting etc..
  11. *
  12. * And probably incredibly slow on parisc. OTOH, we don't
  13. * have to write any serious assembly. prumpf
  14. */
  15. #ifdef CONFIG_SMP
  16. #include <asm/spinlock.h>
  17. #include <asm/cache.h> /* we use L1_CACHE_BYTES */
  18. /* Use an array of spinlocks for our atomic_ts.
  19. * Hash function to index into a different SPINLOCK.
  20. * Since "a" is usually an address, use one spinlock per cacheline.
  21. */
  22. # define ATOMIC_HASH_SIZE 4
  23. # define ATOMIC_HASH(a) (&(__atomic_hash[ (((unsigned long) (a))/L1_CACHE_BYTES) & (ATOMIC_HASH_SIZE-1) ]))
  24. extern arch_spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;
  25. /* Can't use raw_spin_lock_irq because of #include problems, so
  26. * this is the substitute */
  27. #define _atomic_spin_lock_irqsave(l,f) do { \
  28. arch_spinlock_t *s = ATOMIC_HASH(l); \
  29. local_irq_save(f); \
  30. arch_spin_lock(s); \
  31. } while(0)
  32. #define _atomic_spin_unlock_irqrestore(l,f) do { \
  33. arch_spinlock_t *s = ATOMIC_HASH(l); \
  34. arch_spin_unlock(s); \
  35. local_irq_restore(f); \
  36. } while(0)
  37. #else
  38. # define _atomic_spin_lock_irqsave(l,f) do { local_irq_save(f); } while (0)
  39. # define _atomic_spin_unlock_irqrestore(l,f) do { local_irq_restore(f); } while (0)
  40. #endif
  41. /*
  42. * Note that we need not lock read accesses - aligned word writes/reads
  43. * are atomic, so a reader never sees inconsistent values.
  44. */
  45. /* It's possible to reduce all atomic operations to either
  46. * __atomic_add_return, atomic_set and atomic_read (the latter
  47. * is there only for consistency).
  48. */
  49. static __inline__ int __atomic_add_return(int i, atomic_t *v)
  50. {
  51. int ret;
  52. unsigned long flags;
  53. _atomic_spin_lock_irqsave(v, flags);
  54. ret = (v->counter += i);
  55. _atomic_spin_unlock_irqrestore(v, flags);
  56. return ret;
  57. }
  58. static __inline__ void atomic_set(atomic_t *v, int i)
  59. {
  60. unsigned long flags;
  61. _atomic_spin_lock_irqsave(v, flags);
  62. v->counter = i;
  63. _atomic_spin_unlock_irqrestore(v, flags);
  64. }
  65. static __inline__ int atomic_read(const atomic_t *v)
  66. {
  67. return (*(volatile int *)&(v)->counter);
  68. }
  69. /* exported interface */
  70. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  71. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  72. /**
  73. * __atomic_add_unless - add unless the number is a given value
  74. * @v: pointer of type atomic_t
  75. * @a: the amount to add to v...
  76. * @u: ...unless v is equal to u.
  77. *
  78. * Atomically adds @a to @v, so long as it was not @u.
  79. * Returns the old value of @v.
  80. */
  81. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  82. {
  83. int c, old;
  84. c = atomic_read(v);
  85. for (;;) {
  86. if (unlikely(c == (u)))
  87. break;
  88. old = atomic_cmpxchg((v), c, c + (a));
  89. if (likely(old == c))
  90. break;
  91. c = old;
  92. }
  93. return c;
  94. }
  95. #define atomic_add(i,v) ((void)(__atomic_add_return( (i),(v))))
  96. #define atomic_sub(i,v) ((void)(__atomic_add_return(-(i),(v))))
  97. #define atomic_inc(v) ((void)(__atomic_add_return( 1,(v))))
  98. #define atomic_dec(v) ((void)(__atomic_add_return( -1,(v))))
  99. #define atomic_add_return(i,v) (__atomic_add_return( (i),(v)))
  100. #define atomic_sub_return(i,v) (__atomic_add_return(-(i),(v)))
  101. #define atomic_inc_return(v) (__atomic_add_return( 1,(v)))
  102. #define atomic_dec_return(v) (__atomic_add_return( -1,(v)))
  103. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  104. /*
  105. * atomic_inc_and_test - increment and test
  106. * @v: pointer of type atomic_t
  107. *
  108. * Atomically increments @v by 1
  109. * and returns true if the result is zero, or false for all
  110. * other cases.
  111. */
  112. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  113. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  114. #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
  115. #define ATOMIC_INIT(i) ((atomic_t) { (i) })
  116. #define smp_mb__before_atomic_dec() smp_mb()
  117. #define smp_mb__after_atomic_dec() smp_mb()
  118. #define smp_mb__before_atomic_inc() smp_mb()
  119. #define smp_mb__after_atomic_inc() smp_mb()
  120. #ifdef CONFIG_64BIT
  121. #define ATOMIC64_INIT(i) ((atomic64_t) { (i) })
  122. static __inline__ s64
  123. __atomic64_add_return(s64 i, atomic64_t *v)
  124. {
  125. s64 ret;
  126. unsigned long flags;
  127. _atomic_spin_lock_irqsave(v, flags);
  128. ret = (v->counter += i);
  129. _atomic_spin_unlock_irqrestore(v, flags);
  130. return ret;
  131. }
  132. static __inline__ void
  133. atomic64_set(atomic64_t *v, s64 i)
  134. {
  135. unsigned long flags;
  136. _atomic_spin_lock_irqsave(v, flags);
  137. v->counter = i;
  138. _atomic_spin_unlock_irqrestore(v, flags);
  139. }
  140. static __inline__ s64
  141. atomic64_read(const atomic64_t *v)
  142. {
  143. return (*(volatile long *)&(v)->counter);
  144. }
  145. #define atomic64_add(i,v) ((void)(__atomic64_add_return( ((s64)(i)),(v))))
  146. #define atomic64_sub(i,v) ((void)(__atomic64_add_return(-((s64)(i)),(v))))
  147. #define atomic64_inc(v) ((void)(__atomic64_add_return( 1,(v))))
  148. #define atomic64_dec(v) ((void)(__atomic64_add_return( -1,(v))))
  149. #define atomic64_add_return(i,v) (__atomic64_add_return( ((s64)(i)),(v)))
  150. #define atomic64_sub_return(i,v) (__atomic64_add_return(-((s64)(i)),(v)))
  151. #define atomic64_inc_return(v) (__atomic64_add_return( 1,(v)))
  152. #define atomic64_dec_return(v) (__atomic64_add_return( -1,(v)))
  153. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  154. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  155. #define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
  156. #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i),(v)) == 0)
  157. /* exported interface */
  158. #define atomic64_cmpxchg(v, o, n) \
  159. ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
  160. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  161. /**
  162. * atomic64_add_unless - add unless the number is a given value
  163. * @v: pointer of type atomic64_t
  164. * @a: the amount to add to v...
  165. * @u: ...unless v is equal to u.
  166. *
  167. * Atomically adds @a to @v, so long as it was not @u.
  168. * Returns the old value of @v.
  169. */
  170. static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
  171. {
  172. long c, old;
  173. c = atomic64_read(v);
  174. for (;;) {
  175. if (unlikely(c == (u)))
  176. break;
  177. old = atomic64_cmpxchg((v), c, c + (a));
  178. if (likely(old == c))
  179. break;
  180. c = old;
  181. }
  182. return c != (u);
  183. }
  184. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  185. #endif /* !CONFIG_64BIT */
  186. #endif /* _ASM_PARISC_ATOMIC_H_ */