atomic.h 4.0 KB

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