atomic.h 3.9 KB

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