atomic64.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Generic implementation of 64-bit atomics using spinlocks,
  3. * useful on processors that don't have 64-bit atomic instructions.
  4. *
  5. * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/cache.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/init.h>
  16. #include <linux/export.h>
  17. #include <linux/atomic.h>
  18. /*
  19. * We use a hashed array of spinlocks to provide exclusive access
  20. * to each atomic64_t variable. Since this is expected to used on
  21. * systems with small numbers of CPUs (<= 4 or so), we use a
  22. * relatively small array of 16 spinlocks to avoid wasting too much
  23. * memory on the spinlock array.
  24. */
  25. #define NR_LOCKS 16
  26. /*
  27. * Ensure each lock is in a separate cacheline.
  28. */
  29. static union {
  30. raw_spinlock_t lock;
  31. char pad[L1_CACHE_BYTES];
  32. } atomic64_lock[NR_LOCKS] __cacheline_aligned_in_smp = {
  33. [0 ... (NR_LOCKS - 1)] = {
  34. .lock = __RAW_SPIN_LOCK_UNLOCKED(atomic64_lock.lock),
  35. },
  36. };
  37. static inline raw_spinlock_t *lock_addr(const atomic64_t *v)
  38. {
  39. unsigned long addr = (unsigned long) v;
  40. addr >>= L1_CACHE_SHIFT;
  41. addr ^= (addr >> 8) ^ (addr >> 16);
  42. return &atomic64_lock[addr & (NR_LOCKS - 1)].lock;
  43. }
  44. long long atomic64_read(const atomic64_t *v)
  45. {
  46. unsigned long flags;
  47. raw_spinlock_t *lock = lock_addr(v);
  48. long long val;
  49. raw_spin_lock_irqsave(lock, flags);
  50. val = v->counter;
  51. raw_spin_unlock_irqrestore(lock, flags);
  52. return val;
  53. }
  54. EXPORT_SYMBOL(atomic64_read);
  55. void atomic64_set(atomic64_t *v, long long i)
  56. {
  57. unsigned long flags;
  58. raw_spinlock_t *lock = lock_addr(v);
  59. raw_spin_lock_irqsave(lock, flags);
  60. v->counter = i;
  61. raw_spin_unlock_irqrestore(lock, flags);
  62. }
  63. EXPORT_SYMBOL(atomic64_set);
  64. void atomic64_add(long long a, atomic64_t *v)
  65. {
  66. unsigned long flags;
  67. raw_spinlock_t *lock = lock_addr(v);
  68. raw_spin_lock_irqsave(lock, flags);
  69. v->counter += a;
  70. raw_spin_unlock_irqrestore(lock, flags);
  71. }
  72. EXPORT_SYMBOL(atomic64_add);
  73. long long atomic64_add_return(long long a, atomic64_t *v)
  74. {
  75. unsigned long flags;
  76. raw_spinlock_t *lock = lock_addr(v);
  77. long long val;
  78. raw_spin_lock_irqsave(lock, flags);
  79. val = v->counter += a;
  80. raw_spin_unlock_irqrestore(lock, flags);
  81. return val;
  82. }
  83. EXPORT_SYMBOL(atomic64_add_return);
  84. void atomic64_sub(long long a, atomic64_t *v)
  85. {
  86. unsigned long flags;
  87. raw_spinlock_t *lock = lock_addr(v);
  88. raw_spin_lock_irqsave(lock, flags);
  89. v->counter -= a;
  90. raw_spin_unlock_irqrestore(lock, flags);
  91. }
  92. EXPORT_SYMBOL(atomic64_sub);
  93. long long atomic64_sub_return(long long a, atomic64_t *v)
  94. {
  95. unsigned long flags;
  96. raw_spinlock_t *lock = lock_addr(v);
  97. long long val;
  98. raw_spin_lock_irqsave(lock, flags);
  99. val = v->counter -= a;
  100. raw_spin_unlock_irqrestore(lock, flags);
  101. return val;
  102. }
  103. EXPORT_SYMBOL(atomic64_sub_return);
  104. long long atomic64_dec_if_positive(atomic64_t *v)
  105. {
  106. unsigned long flags;
  107. raw_spinlock_t *lock = lock_addr(v);
  108. long long val;
  109. raw_spin_lock_irqsave(lock, flags);
  110. val = v->counter - 1;
  111. if (val >= 0)
  112. v->counter = val;
  113. raw_spin_unlock_irqrestore(lock, flags);
  114. return val;
  115. }
  116. EXPORT_SYMBOL(atomic64_dec_if_positive);
  117. long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
  118. {
  119. unsigned long flags;
  120. raw_spinlock_t *lock = lock_addr(v);
  121. long long val;
  122. raw_spin_lock_irqsave(lock, flags);
  123. val = v->counter;
  124. if (val == o)
  125. v->counter = n;
  126. raw_spin_unlock_irqrestore(lock, flags);
  127. return val;
  128. }
  129. EXPORT_SYMBOL(atomic64_cmpxchg);
  130. long long atomic64_xchg(atomic64_t *v, long long new)
  131. {
  132. unsigned long flags;
  133. raw_spinlock_t *lock = lock_addr(v);
  134. long long val;
  135. raw_spin_lock_irqsave(lock, flags);
  136. val = v->counter;
  137. v->counter = new;
  138. raw_spin_unlock_irqrestore(lock, flags);
  139. return val;
  140. }
  141. EXPORT_SYMBOL(atomic64_xchg);
  142. int atomic64_add_unless(atomic64_t *v, long long a, long long u)
  143. {
  144. unsigned long flags;
  145. raw_spinlock_t *lock = lock_addr(v);
  146. int ret = 0;
  147. raw_spin_lock_irqsave(lock, flags);
  148. if (v->counter != u) {
  149. v->counter += a;
  150. ret = 1;
  151. }
  152. raw_spin_unlock_irqrestore(lock, flags);
  153. return ret;
  154. }
  155. EXPORT_SYMBOL(atomic64_add_unless);