atomic.h 3.9 KB

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