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. #include <asm/system.h>
  50. /**
  51. * atomic_add_return - add integer to atomic variable
  52. * @i: integer value to add
  53. * @v: pointer of type atomic_t
  54. *
  55. * Atomically adds @i to @v and returns the result
  56. */
  57. #ifndef atomic_add_return
  58. static inline int atomic_add_return(int i, atomic_t *v)
  59. {
  60. unsigned long flags;
  61. int temp;
  62. raw_local_irq_save(flags); /* Don't trace it in an irqsoff handler */
  63. temp = v->counter;
  64. temp += i;
  65. v->counter = temp;
  66. raw_local_irq_restore(flags);
  67. return temp;
  68. }
  69. #endif
  70. /**
  71. * atomic_sub_return - subtract integer from atomic variable
  72. * @i: integer value to subtract
  73. * @v: pointer of type atomic_t
  74. *
  75. * Atomically subtracts @i from @v and returns the result
  76. */
  77. #ifndef atomic_sub_return
  78. static inline int atomic_sub_return(int i, atomic_t *v)
  79. {
  80. unsigned long flags;
  81. int temp;
  82. raw_local_irq_save(flags); /* Don't trace it in an irqsoff handler */
  83. temp = v->counter;
  84. temp -= i;
  85. v->counter = temp;
  86. raw_local_irq_restore(flags);
  87. return temp;
  88. }
  89. #endif
  90. static inline int atomic_add_negative(int i, atomic_t *v)
  91. {
  92. return atomic_add_return(i, v) < 0;
  93. }
  94. static inline void atomic_add(int i, atomic_t *v)
  95. {
  96. atomic_add_return(i, v);
  97. }
  98. static inline void atomic_sub(int i, atomic_t *v)
  99. {
  100. atomic_sub_return(i, v);
  101. }
  102. static inline void atomic_inc(atomic_t *v)
  103. {
  104. atomic_add_return(1, v);
  105. }
  106. static inline void atomic_dec(atomic_t *v)
  107. {
  108. atomic_sub_return(1, v);
  109. }
  110. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  111. #define atomic_inc_return(v) atomic_add_return(1, (v))
  112. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  113. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  114. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  115. #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
  116. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
  117. #define cmpxchg_local(ptr, o, n) \
  118. ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
  119. (unsigned long)(n), sizeof(*(ptr))))
  120. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  121. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  122. {
  123. int c, old;
  124. c = atomic_read(v);
  125. while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
  126. c = old;
  127. return c;
  128. }
  129. /**
  130. * atomic_clear_mask - Atomically clear bits in atomic variable
  131. * @mask: Mask of the bits to be cleared
  132. * @v: pointer of type atomic_t
  133. *
  134. * Atomically clears the bits set in @mask from @v
  135. */
  136. #ifndef atomic_clear_mask
  137. static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
  138. {
  139. unsigned long flags;
  140. mask = ~mask;
  141. raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
  142. v->counter &= mask;
  143. raw_local_irq_restore(flags);
  144. }
  145. #endif
  146. /**
  147. * atomic_set_mask - Atomically set bits in atomic variable
  148. * @mask: Mask of the bits to be set
  149. * @v: pointer of type atomic_t
  150. *
  151. * Atomically sets the bits set in @mask in @v
  152. */
  153. #ifndef atomic_set_mask
  154. static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
  155. {
  156. unsigned long flags;
  157. raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
  158. v->counter |= mask;
  159. raw_local_irq_restore(flags);
  160. }
  161. #endif
  162. /* Assume that atomic operations are already serializing */
  163. #define smp_mb__before_atomic_dec() barrier()
  164. #define smp_mb__after_atomic_dec() barrier()
  165. #define smp_mb__before_atomic_inc() barrier()
  166. #define smp_mb__after_atomic_inc() barrier()
  167. #endif /* __KERNEL__ */
  168. #endif /* __ASM_GENERIC_ATOMIC_H */