atomic.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Generic C implementation of atomic counter operations
  3. * Originally implemented for MN10300.
  4. *
  5. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  6. * Written by David Howells (dhowells@redhat.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public Licence
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the Licence, or (at your option) any later version.
  12. */
  13. #ifndef __ASM_GENERIC_ATOMIC_H
  14. #define __ASM_GENERIC_ATOMIC_H
  15. #ifdef CONFIG_SMP
  16. #error not SMP safe
  17. #endif
  18. /*
  19. * Atomic operations that C can't guarantee us. Useful for
  20. * resource counting etc..
  21. */
  22. #define ATOMIC_INIT(i) { (i) }
  23. #ifdef __KERNEL__
  24. /**
  25. * atomic_read - read atomic variable
  26. * @v: pointer of type atomic_t
  27. *
  28. * Atomically reads the value of @v. Note that the guaranteed
  29. * useful range of an atomic_t is only 24 bits.
  30. */
  31. #define atomic_read(v) ((v)->counter)
  32. /**
  33. * atomic_set - set atomic variable
  34. * @v: pointer of type atomic_t
  35. * @i: required value
  36. *
  37. * Atomically sets the value of @v to @i. Note that the guaranteed
  38. * useful range of an atomic_t is only 24 bits.
  39. */
  40. #define atomic_set(v, i) (((v)->counter) = (i))
  41. #include <asm/system.h>
  42. /**
  43. * atomic_add_return - add integer to atomic variable
  44. * @i: integer value to add
  45. * @v: pointer of type atomic_t
  46. *
  47. * Atomically adds @i to @v and returns the result
  48. * Note that the guaranteed useful range of an atomic_t is only 24 bits.
  49. */
  50. static inline int atomic_add_return(int i, atomic_t *v)
  51. {
  52. unsigned long flags;
  53. int temp;
  54. local_irq_save(flags);
  55. temp = v->counter;
  56. temp += i;
  57. v->counter = temp;
  58. 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. * Note that the guaranteed useful range of an atomic_t is only 24 bits.
  68. */
  69. static inline int atomic_sub_return(int i, atomic_t *v)
  70. {
  71. unsigned long flags;
  72. int temp;
  73. local_irq_save(flags);
  74. temp = v->counter;
  75. temp -= i;
  76. v->counter = temp;
  77. local_irq_restore(flags);
  78. return temp;
  79. }
  80. static inline int atomic_add_negative(int i, atomic_t *v)
  81. {
  82. return atomic_add_return(i, v) < 0;
  83. }
  84. static inline void atomic_add(int i, atomic_t *v)
  85. {
  86. atomic_add_return(i, v);
  87. }
  88. static inline void atomic_sub(int i, atomic_t *v)
  89. {
  90. atomic_sub_return(i, v);
  91. }
  92. static inline void atomic_inc(atomic_t *v)
  93. {
  94. atomic_add_return(1, v);
  95. }
  96. static inline void atomic_dec(atomic_t *v)
  97. {
  98. atomic_sub_return(1, v);
  99. }
  100. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  101. #define atomic_inc_return(v) atomic_add_return(1, (v))
  102. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  103. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  104. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  105. #define atomic_add_unless(v, a, u) \
  106. ({ \
  107. int c, old; \
  108. c = atomic_read(v); \
  109. while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
  110. c = old; \
  111. c != (u); \
  112. })
  113. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  114. static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
  115. {
  116. unsigned long flags;
  117. mask = ~mask;
  118. local_irq_save(flags);
  119. *addr &= mask;
  120. local_irq_restore(flags);
  121. }
  122. #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
  123. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
  124. #define cmpxchg_local(ptr, o, n) \
  125. ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
  126. (unsigned long)(n), sizeof(*(ptr))))
  127. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  128. /* Assume that atomic operations are already serializing */
  129. #define smp_mb__before_atomic_dec() barrier()
  130. #define smp_mb__after_atomic_dec() barrier()
  131. #define smp_mb__before_atomic_inc() barrier()
  132. #define smp_mb__after_atomic_inc() barrier()
  133. #include <asm-generic/atomic-long.h>
  134. #endif /* __KERNEL__ */
  135. #endif /* __ASM_GENERIC_ATOMIC_H */