atomic.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * linux/include/asm-arm/atomic.h
  3. *
  4. * Copyright (c) 1996 Russell King.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Changelog:
  11. * 27-06-1996 RMK Created
  12. * 13-04-1997 RMK Made functions atomic!
  13. * 07-12-1997 RMK Upgraded for v2.1.
  14. * 26-08-1998 PJB Added #ifdef __KERNEL__
  15. */
  16. #ifndef __ASM_ARM_ATOMIC_H
  17. #define __ASM_ARM_ATOMIC_H
  18. #ifdef CONFIG_SMP
  19. #error SMP not supported
  20. #endif
  21. typedef struct { volatile int counter; } atomic_t;
  22. #if BITS_PER_LONG == 32
  23. typedef struct { volatile long long counter; } atomic64_t;
  24. #else /* BIT_PER_LONG == 32 */
  25. typedef struct { volatile long counter; } atomic64_t;
  26. #endif
  27. #define ATOMIC_INIT(i) { (i) }
  28. #ifdef __KERNEL__
  29. #include <asm/proc-armv/system.h>
  30. #define atomic_read(v) ((v)->counter)
  31. #define atomic_set(v, i) (((v)->counter) = (i))
  32. #define atomic64_read(v) atomic_read(v)
  33. #define atomic64_set(v, i) atomic_set(v, i)
  34. static inline void atomic_add(int i, volatile atomic_t *v)
  35. {
  36. unsigned long flags = 0;
  37. local_irq_save(flags);
  38. v->counter += i;
  39. local_irq_restore(flags);
  40. }
  41. static inline void atomic_sub(int i, volatile atomic_t *v)
  42. {
  43. unsigned long flags = 0;
  44. local_irq_save(flags);
  45. v->counter -= i;
  46. local_irq_restore(flags);
  47. }
  48. static inline void atomic_inc(volatile atomic_t *v)
  49. {
  50. unsigned long flags = 0;
  51. local_irq_save(flags);
  52. v->counter += 1;
  53. local_irq_restore(flags);
  54. }
  55. static inline void atomic_dec(volatile atomic_t *v)
  56. {
  57. unsigned long flags = 0;
  58. local_irq_save(flags);
  59. v->counter -= 1;
  60. local_irq_restore(flags);
  61. }
  62. static inline int atomic_dec_and_test(volatile atomic_t *v)
  63. {
  64. unsigned long flags = 0;
  65. int val;
  66. local_irq_save(flags);
  67. val = v->counter;
  68. v->counter = val -= 1;
  69. local_irq_restore(flags);
  70. return val == 0;
  71. }
  72. static inline int atomic_add_negative(int i, volatile atomic_t *v)
  73. {
  74. unsigned long flags = 0;
  75. int val;
  76. local_irq_save(flags);
  77. val = v->counter;
  78. v->counter = val += i;
  79. local_irq_restore(flags);
  80. return val < 0;
  81. }
  82. static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
  83. {
  84. unsigned long flags = 0;
  85. local_irq_save(flags);
  86. *addr &= ~mask;
  87. local_irq_restore(flags);
  88. }
  89. #if BITS_PER_LONG == 32
  90. static inline void atomic64_add(long long i, volatile atomic64_t *v)
  91. {
  92. unsigned long flags = 0;
  93. local_irq_save(flags);
  94. v->counter += i;
  95. local_irq_restore(flags);
  96. }
  97. static inline void atomic64_sub(long long i, volatile atomic64_t *v)
  98. {
  99. unsigned long flags = 0;
  100. local_irq_save(flags);
  101. v->counter -= i;
  102. local_irq_restore(flags);
  103. }
  104. #else /* BIT_PER_LONG == 32 */
  105. static inline void atomic64_add(long i, volatile atomic64_t *v)
  106. {
  107. unsigned long flags = 0;
  108. local_irq_save(flags);
  109. v->counter += i;
  110. local_irq_restore(flags);
  111. }
  112. static inline void atomic64_sub(long i, volatile atomic64_t *v)
  113. {
  114. unsigned long flags = 0;
  115. local_irq_save(flags);
  116. v->counter -= i;
  117. local_irq_restore(flags);
  118. }
  119. #endif
  120. static inline void atomic64_inc(volatile atomic64_t *v)
  121. {
  122. unsigned long flags = 0;
  123. local_irq_save(flags);
  124. v->counter += 1;
  125. local_irq_restore(flags);
  126. }
  127. static inline void atomic64_dec(volatile atomic64_t *v)
  128. {
  129. unsigned long flags = 0;
  130. local_irq_save(flags);
  131. v->counter -= 1;
  132. local_irq_restore(flags);
  133. }
  134. /* Atomic operations are already serializing on ARM */
  135. #define smp_mb__before_atomic_dec() barrier()
  136. #define smp_mb__after_atomic_dec() barrier()
  137. #define smp_mb__before_atomic_inc() barrier()
  138. #define smp_mb__after_atomic_inc() barrier()
  139. #endif
  140. #endif