atomic.h 4.3 KB

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