atomic.h 3.9 KB

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