spinlock.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef __ASM_SPINLOCK_H
  2. #define __ASM_SPINLOCK_H
  3. #include <asm/atomic.h>
  4. #include <asm/rwlock.h>
  5. #include <asm/page.h>
  6. #include <linux/config.h>
  7. #include <linux/compiler.h>
  8. /*
  9. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  10. *
  11. * Simple spin lock operations. There are two variants, one clears IRQ's
  12. * on the local processor, one does not.
  13. *
  14. * We make no fairness assumptions. They have a cost.
  15. *
  16. * (the type definitions are in asm/spinlock_types.h)
  17. */
  18. #define __raw_spin_is_locked(x) \
  19. (*(volatile signed char *)(&(x)->slock) <= 0)
  20. #define __raw_spin_lock_string \
  21. "\n1:\t" \
  22. "lock ; decb %0\n\t" \
  23. "jns 3f\n" \
  24. "2:\t" \
  25. "rep;nop\n\t" \
  26. "cmpb $0,%0\n\t" \
  27. "jle 2b\n\t" \
  28. "jmp 1b\n" \
  29. "3:\n\t"
  30. #define __raw_spin_lock_string_flags \
  31. "\n1:\t" \
  32. "lock ; decb %0\n\t" \
  33. "jns 4f\n\t" \
  34. "2:\t" \
  35. "testl $0x200, %1\n\t" \
  36. "jz 3f\n\t" \
  37. "sti\n\t" \
  38. "3:\t" \
  39. "rep;nop\n\t" \
  40. "cmpb $0, %0\n\t" \
  41. "jle 3b\n\t" \
  42. "cli\n\t" \
  43. "jmp 1b\n" \
  44. "4:\n\t"
  45. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  46. {
  47. __asm__ __volatile__(
  48. __raw_spin_lock_string
  49. :"=m" (lock->slock) : : "memory");
  50. }
  51. static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
  52. {
  53. __asm__ __volatile__(
  54. __raw_spin_lock_string_flags
  55. :"=m" (lock->slock) : "r" (flags) : "memory");
  56. }
  57. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  58. {
  59. char oldval;
  60. __asm__ __volatile__(
  61. "xchgb %b0,%1"
  62. :"=q" (oldval), "=m" (lock->slock)
  63. :"0" (0) : "memory");
  64. return oldval > 0;
  65. }
  66. /*
  67. * __raw_spin_unlock based on writing $1 to the low byte.
  68. * This method works. Despite all the confusion.
  69. * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
  70. * (PPro errata 66, 92)
  71. */
  72. #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
  73. #define __raw_spin_unlock_string \
  74. "movb $1,%0" \
  75. :"=m" (lock->slock) : : "memory"
  76. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  77. {
  78. __asm__ __volatile__(
  79. __raw_spin_unlock_string
  80. );
  81. }
  82. #else
  83. #define __raw_spin_unlock_string \
  84. "xchgb %b0, %1" \
  85. :"=q" (oldval), "=m" (lock->slock) \
  86. :"0" (oldval) : "memory"
  87. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  88. {
  89. char oldval = 1;
  90. __asm__ __volatile__(
  91. __raw_spin_unlock_string
  92. );
  93. }
  94. #endif
  95. #define __raw_spin_unlock_wait(lock) \
  96. do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
  97. /*
  98. * Read-write spinlocks, allowing multiple readers
  99. * but only one writer.
  100. *
  101. * NOTE! it is quite common to have readers in interrupts
  102. * but no interrupt writers. For those circumstances we
  103. * can "mix" irq-safe locks - any writer needs to get a
  104. * irq-safe write-lock, but readers can get non-irqsafe
  105. * read-locks.
  106. *
  107. * On x86, we implement read-write locks as a 32-bit counter
  108. * with the high bit (sign) being the "contended" bit.
  109. *
  110. * The inline assembly is non-obvious. Think about it.
  111. *
  112. * Changed to use the same technique as rw semaphores. See
  113. * semaphore.h for details. -ben
  114. *
  115. * the helpers are in arch/i386/kernel/semaphore.c
  116. */
  117. /**
  118. * read_can_lock - would read_trylock() succeed?
  119. * @lock: the rwlock in question.
  120. */
  121. #define __raw_read_can_lock(x) ((int)(x)->lock > 0)
  122. /**
  123. * write_can_lock - would write_trylock() succeed?
  124. * @lock: the rwlock in question.
  125. */
  126. #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
  127. static inline void __raw_read_lock(raw_rwlock_t *rw)
  128. {
  129. __build_read_lock(rw, "__read_lock_failed");
  130. }
  131. static inline void __raw_write_lock(raw_rwlock_t *rw)
  132. {
  133. __build_write_lock(rw, "__write_lock_failed");
  134. }
  135. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  136. {
  137. atomic_t *count = (atomic_t *)lock;
  138. atomic_dec(count);
  139. if (atomic_read(count) >= 0)
  140. return 1;
  141. atomic_inc(count);
  142. return 0;
  143. }
  144. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  145. {
  146. atomic_t *count = (atomic_t *)lock;
  147. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  148. return 1;
  149. atomic_add(RW_LOCK_BIAS, count);
  150. return 0;
  151. }
  152. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  153. {
  154. asm volatile("lock ; incl %0" :"=m" (rw->lock) : : "memory");
  155. }
  156. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  157. {
  158. asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ", %0"
  159. : "=m" (rw->lock) : : "memory");
  160. }
  161. #endif /* __ASM_SPINLOCK_H */