spinlock.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /*
  8. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  9. *
  10. * Simple spin lock operations. There are two variants, one clears IRQ's
  11. * on the local processor, one does not.
  12. *
  13. * We make no fairness assumptions. They have a cost.
  14. *
  15. * (the type definitions are in asm/spinlock_types.h)
  16. */
  17. #define __raw_spin_is_locked(x) \
  18. (*(volatile signed char *)(&(x)->slock) <= 0)
  19. #define __raw_spin_lock_string \
  20. "\n1:\t" \
  21. "lock ; decb %0\n\t" \
  22. "js 2f\n" \
  23. LOCK_SECTION_START("") \
  24. "2:\t" \
  25. "rep;nop\n\t" \
  26. "cmpb $0,%0\n\t" \
  27. "jle 2b\n\t" \
  28. "jmp 1b\n" \
  29. LOCK_SECTION_END
  30. #define __raw_spin_unlock_string \
  31. "movb $1,%0" \
  32. :"=m" (lock->slock) : : "memory"
  33. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  34. {
  35. __asm__ __volatile__(
  36. __raw_spin_lock_string
  37. :"=m" (lock->slock) : : "memory");
  38. }
  39. #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
  40. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  41. {
  42. char oldval;
  43. __asm__ __volatile__(
  44. "xchgb %b0,%1"
  45. :"=q" (oldval), "=m" (lock->slock)
  46. :"0" (0) : "memory");
  47. return oldval > 0;
  48. }
  49. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  50. {
  51. __asm__ __volatile__(
  52. __raw_spin_unlock_string
  53. );
  54. }
  55. #define __raw_spin_unlock_wait(lock) \
  56. do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
  57. /*
  58. * Read-write spinlocks, allowing multiple readers
  59. * but only one writer.
  60. *
  61. * NOTE! it is quite common to have readers in interrupts
  62. * but no interrupt writers. For those circumstances we
  63. * can "mix" irq-safe locks - any writer needs to get a
  64. * irq-safe write-lock, but readers can get non-irqsafe
  65. * read-locks.
  66. *
  67. * On x86, we implement read-write locks as a 32-bit counter
  68. * with the high bit (sign) being the "contended" bit.
  69. *
  70. * The inline assembly is non-obvious. Think about it.
  71. *
  72. * Changed to use the same technique as rw semaphores. See
  73. * semaphore.h for details. -ben
  74. *
  75. * the helpers are in arch/i386/kernel/semaphore.c
  76. */
  77. #define __raw_read_can_lock(x) ((int)(x)->lock > 0)
  78. #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
  79. static inline void __raw_read_lock(raw_rwlock_t *rw)
  80. {
  81. __build_read_lock(rw, "__read_lock_failed");
  82. }
  83. static inline void __raw_write_lock(raw_rwlock_t *rw)
  84. {
  85. __build_write_lock(rw, "__write_lock_failed");
  86. }
  87. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  88. {
  89. atomic_t *count = (atomic_t *)lock;
  90. atomic_dec(count);
  91. if (atomic_read(count) >= 0)
  92. return 1;
  93. atomic_inc(count);
  94. return 0;
  95. }
  96. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  97. {
  98. atomic_t *count = (atomic_t *)lock;
  99. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  100. return 1;
  101. atomic_add(RW_LOCK_BIAS, count);
  102. return 0;
  103. }
  104. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  105. {
  106. asm volatile("lock ; incl %0" :"=m" (rw->lock) : : "memory");
  107. }
  108. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  109. {
  110. asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0"
  111. : "=m" (rw->lock) : : "memory");
  112. }
  113. #endif /* __ASM_SPINLOCK_H */