atomic.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #ifdef CONFIG_SMP
  20. /* Force people to define core atomics */
  21. # if !defined(atomic_add_return) || !defined(atomic_sub_return) || \
  22. !defined(atomic_clear_mask) || !defined(atomic_set_mask)
  23. # error "SMP requires a little arch-specific magic"
  24. # endif
  25. #endif
  26. /*
  27. * Atomic operations that C can't guarantee us. Useful for
  28. * resource counting etc..
  29. */
  30. #define ATOMIC_INIT(i) { (i) }
  31. #ifdef __KERNEL__
  32. /**
  33. * atomic_read - read atomic variable
  34. * @v: pointer of type atomic_t
  35. *
  36. * Atomically reads the value of @v.
  37. */
  38. #ifndef atomic_read
  39. #define atomic_read(v) (*(volatile int *)&(v)->counter)
  40. #endif
  41. /**
  42. * atomic_set - set atomic variable
  43. * @v: pointer of type atomic_t
  44. * @i: required value
  45. *
  46. * Atomically sets the value of @v to @i.
  47. */
  48. #define atomic_set(v, i) (((v)->counter) = (i))
  49. #include <linux/irqflags.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. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  118. {
  119. int c, old;
  120. c = atomic_read(v);
  121. while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
  122. c = old;
  123. return c;
  124. }
  125. /**
  126. * atomic_clear_mask - Atomically clear bits in atomic variable
  127. * @mask: Mask of the bits to be cleared
  128. * @v: pointer of type atomic_t
  129. *
  130. * Atomically clears the bits set in @mask from @v
  131. */
  132. #ifndef atomic_clear_mask
  133. static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
  134. {
  135. unsigned long flags;
  136. mask = ~mask;
  137. raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
  138. v->counter &= mask;
  139. raw_local_irq_restore(flags);
  140. }
  141. #endif
  142. /**
  143. * atomic_set_mask - Atomically set bits in atomic variable
  144. * @mask: Mask of the bits to be set
  145. * @v: pointer of type atomic_t
  146. *
  147. * Atomically sets the bits set in @mask in @v
  148. */
  149. #ifndef atomic_set_mask
  150. static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
  151. {
  152. unsigned long flags;
  153. raw_local_irq_save(flags); /* Don't trace it in a irqsoff handler */
  154. v->counter |= mask;
  155. raw_local_irq_restore(flags);
  156. }
  157. #endif
  158. #endif /* __KERNEL__ */
  159. #endif /* __ASM_GENERIC_ATOMIC_H */