atomic.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #ifdef CONFIG_SMP
  19. /* Force people to define core atomics */
  20. # if !defined(atomic_add_return) || !defined(atomic_sub_return) || \
  21. !defined(atomic_clear_mask) || !defined(atomic_set_mask)
  22. # error "SMP requires a little arch-specific magic"
  23. # endif
  24. #endif
  25. /*
  26. * Atomic operations that C can't guarantee us. Useful for
  27. * resource counting etc..
  28. */
  29. #define ATOMIC_INIT(i) { (i) }
  30. #ifdef __KERNEL__
  31. /**
  32. * atomic_read - read atomic variable
  33. * @v: pointer of type atomic_t
  34. *
  35. * Atomically reads the value of @v.
  36. */
  37. #ifndef atomic_read
  38. #define atomic_read(v) (*(volatile int *)&(v)->counter)
  39. #endif
  40. /**
  41. * atomic_set - set atomic variable
  42. * @v: pointer of type atomic_t
  43. * @i: required value
  44. *
  45. * Atomically sets the value of @v to @i.
  46. */
  47. #define atomic_set(v, i) (((v)->counter) = (i))
  48. #include <linux/irqflags.h>
  49. /**
  50. * atomic_add_return - add integer to atomic variable
  51. * @i: integer value to add
  52. * @v: pointer of type atomic_t
  53. *
  54. * Atomically adds @i to @v and returns the result
  55. */
  56. #ifndef atomic_add_return
  57. static inline int atomic_add_return(int i, atomic_t *v)
  58. {
  59. unsigned long flags;
  60. int temp;
  61. raw_local_irq_save(flags); /* Don't trace it in an irqsoff handler */
  62. temp = v->counter;
  63. temp += i;
  64. v->counter = temp;
  65. raw_local_irq_restore(flags);
  66. return temp;
  67. }
  68. #endif
  69. /**
  70. * atomic_sub_return - subtract integer from atomic variable
  71. * @i: integer value to subtract
  72. * @v: pointer of type atomic_t
  73. *
  74. * Atomically subtracts @i from @v and returns the result
  75. */
  76. #ifndef atomic_sub_return
  77. static inline int atomic_sub_return(int i, atomic_t *v)
  78. {
  79. unsigned long flags;
  80. int temp;
  81. raw_local_irq_save(flags); /* Don't trace it in an irqsoff handler */
  82. temp = v->counter;
  83. temp -= i;
  84. v->counter = temp;
  85. raw_local_irq_restore(flags);
  86. return temp;
  87. }
  88. #endif
  89. static inline int atomic_add_negative(int i, atomic_t *v)
  90. {
  91. return atomic_add_return(i, v) < 0;
  92. }
  93. static inline void atomic_add(int i, atomic_t *v)
  94. {
  95. atomic_add_return(i, v);
  96. }
  97. static inline void atomic_sub(int i, atomic_t *v)
  98. {
  99. atomic_sub_return(i, v);
  100. }
  101. static inline void atomic_inc(atomic_t *v)
  102. {
  103. atomic_add_return(1, v);
  104. }
  105. static inline void atomic_dec(atomic_t *v)
  106. {
  107. atomic_sub_return(1, v);
  108. }
  109. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  110. #define atomic_inc_return(v) atomic_add_return(1, (v))
  111. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  112. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  113. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  114. #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
  115. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
  116. #define cmpxchg_local(ptr, o, n) \
  117. ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
  118. (unsigned long)(n), sizeof(*(ptr))))
  119. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  120. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  121. {
  122. int c, old;
  123. c = atomic_read(v);
  124. while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
  125. c = old;
  126. return c;
  127. }
  128. /**
  129. * atomic_clear_mask - Atomically clear bits in atomic variable
  130. * @mask: Mask of the bits to be cleared
  131. * @v: pointer of type atomic_t
  132. *
  133. * Atomically clears the bits set in @mask from @v
  134. */
  135. #ifndef atomic_clear_mask
  136. static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
  137. {
  138. unsigned long flags;
  139. mask = ~mask;
  140. raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
  141. v->counter &= mask;
  142. raw_local_irq_restore(flags);
  143. }
  144. #endif
  145. /**
  146. * atomic_set_mask - Atomically set bits in atomic variable
  147. * @mask: Mask of the bits to be set
  148. * @v: pointer of type atomic_t
  149. *
  150. * Atomically sets the bits set in @mask in @v
  151. */
  152. #ifndef atomic_set_mask
  153. static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
  154. {
  155. unsigned long flags;
  156. raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
  157. v->counter |= mask;
  158. raw_local_irq_restore(flags);
  159. }
  160. #endif
  161. /* Assume that atomic operations are already serializing */
  162. #define smp_mb__before_atomic_dec() barrier()
  163. #define smp_mb__after_atomic_dec() barrier()
  164. #define smp_mb__before_atomic_inc() barrier()
  165. #define smp_mb__after_atomic_inc() barrier()
  166. #endif /* __KERNEL__ */
  167. #endif /* __ASM_GENERIC_ATOMIC_H */