atomic.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #define ATOMIC_INIT(i) { (i) }
  21. #ifdef __KERNEL__
  22. /**
  23. * atomic_read - read atomic variable
  24. * @v: pointer of type atomic_t
  25. *
  26. * Atomically reads the value of @v. Note that the guaranteed
  27. * useful range of an atomic_t is only 24 bits.
  28. */
  29. #define atomic_read(v) ((v)->counter)
  30. /**
  31. * atomic_set - set atomic variable
  32. * @v: pointer of type atomic_t
  33. * @i: required value
  34. *
  35. * Atomically sets the value of @v to @i. Note that the guaranteed
  36. * useful range of an atomic_t is only 24 bits.
  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. * Note that the guaranteed useful range of an atomic_t is only 24 bits.
  47. */
  48. static inline int atomic_add_return(int i, atomic_t *v)
  49. {
  50. unsigned long flags;
  51. int temp;
  52. local_irq_save(flags);
  53. temp = v->counter;
  54. temp += i;
  55. v->counter = temp;
  56. 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. * Note that the guaranteed useful range of an atomic_t is only 24 bits.
  66. */
  67. static inline int atomic_sub_return(int i, atomic_t *v)
  68. {
  69. unsigned long flags;
  70. int temp;
  71. local_irq_save(flags);
  72. temp = v->counter;
  73. temp -= i;
  74. v->counter = temp;
  75. local_irq_restore(flags);
  76. return temp;
  77. }
  78. static inline int atomic_add_negative(int i, atomic_t *v)
  79. {
  80. return atomic_add_return(i, v) < 0;
  81. }
  82. static inline void atomic_add(int i, atomic_t *v)
  83. {
  84. atomic_add_return(i, v);
  85. }
  86. static inline void atomic_sub(int i, atomic_t *v)
  87. {
  88. atomic_sub_return(i, v);
  89. }
  90. static inline void atomic_inc(atomic_t *v)
  91. {
  92. atomic_add_return(1, v);
  93. }
  94. static inline void atomic_dec(atomic_t *v)
  95. {
  96. atomic_sub_return(1, v);
  97. }
  98. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  99. #define atomic_inc_return(v) atomic_add_return(1, (v))
  100. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  101. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  102. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  103. #define atomic_add_unless(v, a, u) \
  104. ({ \
  105. int c, old; \
  106. c = atomic_read(v); \
  107. while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
  108. c = old; \
  109. c != (u); \
  110. })
  111. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  112. static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
  113. {
  114. unsigned long flags;
  115. mask = ~mask;
  116. local_irq_save(flags);
  117. *addr &= mask;
  118. local_irq_restore(flags);
  119. }
  120. #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
  121. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
  122. /* Atomic operations are already serializing on MN10300??? */
  123. #define smp_mb__before_atomic_dec() barrier()
  124. #define smp_mb__after_atomic_dec() barrier()
  125. #define smp_mb__before_atomic_inc() barrier()
  126. #define smp_mb__after_atomic_inc() barrier()
  127. #include <asm-generic/atomic-long.h>
  128. #endif /* __KERNEL__ */
  129. #endif /* _ASM_ATOMIC_H */