atomic.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  117. {
  118. int c, old;
  119. c = atomic_read(v);
  120. while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
  121. c = old;
  122. return c;
  123. }
  124. /**
  125. * atomic_clear_mask - Atomically clear bits in atomic variable
  126. * @mask: Mask of the bits to be cleared
  127. * @v: pointer of type atomic_t
  128. *
  129. * Atomically clears the bits set in @mask from @v
  130. */
  131. #ifndef atomic_clear_mask
  132. static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
  133. {
  134. unsigned long flags;
  135. mask = ~mask;
  136. raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
  137. v->counter &= mask;
  138. raw_local_irq_restore(flags);
  139. }
  140. #endif
  141. /**
  142. * atomic_set_mask - Atomically set bits in atomic variable
  143. * @mask: Mask of the bits to be set
  144. * @v: pointer of type atomic_t
  145. *
  146. * Atomically sets the bits set in @mask in @v
  147. */
  148. #ifndef atomic_set_mask
  149. static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
  150. {
  151. unsigned long flags;
  152. raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
  153. v->counter |= mask;
  154. raw_local_irq_restore(flags);
  155. }
  156. #endif
  157. /* Assume that atomic operations are already serializing */
  158. #define smp_mb__before_atomic_dec() barrier()
  159. #define smp_mb__after_atomic_dec() barrier()
  160. #define smp_mb__before_atomic_inc() barrier()
  161. #define smp_mb__after_atomic_inc() barrier()
  162. #endif /* __KERNEL__ */
  163. #endif /* __ASM_GENERIC_ATOMIC_H */