atomic.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Generic C implementation of atomic counter operations. Usable on
  3. * UP systems only. Do not include in machine independent code.
  4. *
  5. * Originally implemented for MN10300.
  6. *
  7. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  8. * Written by David Howells (dhowells@redhat.com)
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public Licence
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the Licence, or (at your option) any later version.
  14. */
  15. #ifndef __ASM_GENERIC_ATOMIC_H
  16. #define __ASM_GENERIC_ATOMIC_H
  17. #include <asm/cmpxchg.h>
  18. #include <asm/barrier.h>
  19. /*
  20. * atomic_$op() - $op integer to atomic variable
  21. * @i: integer value to $op
  22. * @v: pointer to the atomic variable
  23. *
  24. * Atomically $ops @i to @v. Does not strictly guarantee a memory-barrier, use
  25. * smp_mb__{before,after}_atomic().
  26. */
  27. /*
  28. * atomic_$op_return() - $op interer to atomic variable and returns the result
  29. * @i: integer value to $op
  30. * @v: pointer to the atomic variable
  31. *
  32. * Atomically $ops @i to @v. Does imply a full memory barrier.
  33. */
  34. #ifdef CONFIG_SMP
  35. /* we can build all atomic primitives from cmpxchg */
  36. #define ATOMIC_OP(op, c_op) \
  37. static inline void atomic_##op(int i, atomic_t *v) \
  38. { \
  39. int c, old; \
  40. \
  41. c = v->counter; \
  42. while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
  43. c = old; \
  44. }
  45. #define ATOMIC_OP_RETURN(op, c_op) \
  46. static inline int atomic_##op##_return(int i, atomic_t *v) \
  47. { \
  48. int c, old; \
  49. \
  50. c = v->counter; \
  51. while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
  52. c = old; \
  53. \
  54. return c c_op i; \
  55. }
  56. #else
  57. #include <linux/irqflags.h>
  58. #define ATOMIC_OP(op, c_op) \
  59. static inline void atomic_##op(int i, atomic_t *v) \
  60. { \
  61. unsigned long flags; \
  62. \
  63. raw_local_irq_save(flags); \
  64. v->counter = v->counter c_op i; \
  65. raw_local_irq_restore(flags); \
  66. }
  67. #define ATOMIC_OP_RETURN(op, c_op) \
  68. static inline int atomic_##op##_return(int i, atomic_t *v) \
  69. { \
  70. unsigned long flags; \
  71. int ret; \
  72. \
  73. raw_local_irq_save(flags); \
  74. ret = (v->counter = v->counter c_op i); \
  75. raw_local_irq_restore(flags); \
  76. \
  77. return ret; \
  78. }
  79. #endif /* CONFIG_SMP */
  80. #ifndef atomic_add_return
  81. ATOMIC_OP_RETURN(add, +)
  82. #endif
  83. #ifndef atomic_sub_return
  84. ATOMIC_OP_RETURN(sub, -)
  85. #endif
  86. #ifndef atomic_and
  87. ATOMIC_OP(and, &)
  88. #endif
  89. #ifndef atomic_or
  90. ATOMIC_OP(or, |)
  91. #endif
  92. #ifndef atomic_xor
  93. ATOMIC_OP(xor, ^)
  94. #endif
  95. #undef ATOMIC_OP_RETURN
  96. #undef ATOMIC_OP
  97. /*
  98. * Atomic operations that C can't guarantee us. Useful for
  99. * resource counting etc..
  100. */
  101. #define ATOMIC_INIT(i) { (i) }
  102. /**
  103. * atomic_read - read atomic variable
  104. * @v: pointer of type atomic_t
  105. *
  106. * Atomically reads the value of @v.
  107. */
  108. #ifndef atomic_read
  109. #define atomic_read(v) READ_ONCE((v)->counter)
  110. #endif
  111. /**
  112. * atomic_set - set atomic variable
  113. * @v: pointer of type atomic_t
  114. * @i: required value
  115. *
  116. * Atomically sets the value of @v to @i.
  117. */
  118. #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
  119. #include <linux/irqflags.h>
  120. static inline int atomic_add_negative(int i, atomic_t *v)
  121. {
  122. return atomic_add_return(i, v) < 0;
  123. }
  124. static inline void atomic_add(int i, atomic_t *v)
  125. {
  126. atomic_add_return(i, v);
  127. }
  128. static inline void atomic_sub(int i, atomic_t *v)
  129. {
  130. atomic_sub_return(i, v);
  131. }
  132. static inline void atomic_inc(atomic_t *v)
  133. {
  134. atomic_add_return(1, v);
  135. }
  136. static inline void atomic_dec(atomic_t *v)
  137. {
  138. atomic_sub_return(1, v);
  139. }
  140. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  141. #define atomic_inc_return(v) atomic_add_return(1, (v))
  142. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  143. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  144. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  145. #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
  146. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
  147. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  148. {
  149. int c, old;
  150. c = atomic_read(v);
  151. while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
  152. c = old;
  153. return c;
  154. }
  155. #endif /* __ASM_GENERIC_ATOMIC_H */