spinlock.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <asm/processor.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. static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
  18. {
  19. return *(volatile signed int *)(&(lock)->slock) <= 0;
  20. }
  21. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  22. {
  23. asm volatile(
  24. "\n1:\t"
  25. LOCK_PREFIX " ; decl %0\n\t"
  26. "jns 2f\n"
  27. "3:\n"
  28. "rep;nop\n\t"
  29. "cmpl $0,%0\n\t"
  30. "jle 3b\n\t"
  31. "jmp 1b\n"
  32. "2:\t" : "=m" (lock->slock) : : "memory");
  33. }
  34. #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
  35. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  36. {
  37. int oldval;
  38. asm volatile(
  39. "xchgl %0,%1"
  40. :"=q" (oldval), "=m" (lock->slock)
  41. :"0" (0) : "memory");
  42. return oldval > 0;
  43. }
  44. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  45. {
  46. asm volatile("movl $1,%0" :"=m" (lock->slock) :: "memory");
  47. }
  48. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  49. {
  50. while (__raw_spin_is_locked(lock))
  51. cpu_relax();
  52. }
  53. /*
  54. * Read-write spinlocks, allowing multiple readers
  55. * but only one writer.
  56. *
  57. * NOTE! it is quite common to have readers in interrupts
  58. * but no interrupt writers. For those circumstances we
  59. * can "mix" irq-safe locks - any writer needs to get a
  60. * irq-safe write-lock, but readers can get non-irqsafe
  61. * read-locks.
  62. *
  63. * On x86, we implement read-write locks as a 32-bit counter
  64. * with the high bit (sign) being the "contended" bit.
  65. */
  66. static inline int __raw_read_can_lock(raw_rwlock_t *lock)
  67. {
  68. return (int)(lock)->lock > 0;
  69. }
  70. static inline int __raw_write_can_lock(raw_rwlock_t *lock)
  71. {
  72. return (lock)->lock == RW_LOCK_BIAS;
  73. }
  74. static inline void __raw_read_lock(raw_rwlock_t *rw)
  75. {
  76. asm volatile(LOCK_PREFIX "subl $1,(%0)\n\t"
  77. "jns 1f\n"
  78. "call __read_lock_failed\n"
  79. "1:\n"
  80. ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory");
  81. }
  82. static inline void __raw_write_lock(raw_rwlock_t *rw)
  83. {
  84. asm volatile(LOCK_PREFIX "subl %1,(%0)\n\t"
  85. "jz 1f\n"
  86. "\tcall __write_lock_failed\n\t"
  87. "1:\n"
  88. ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory");
  89. }
  90. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  91. {
  92. atomic_t *count = (atomic_t *)lock;
  93. atomic_dec(count);
  94. if (atomic_read(count) >= 0)
  95. return 1;
  96. atomic_inc(count);
  97. return 0;
  98. }
  99. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  100. {
  101. atomic_t *count = (atomic_t *)lock;
  102. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  103. return 1;
  104. atomic_add(RW_LOCK_BIAS, count);
  105. return 0;
  106. }
  107. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  108. {
  109. asm volatile(LOCK_PREFIX " ; incl %0" :"=m" (rw->lock) : : "memory");
  110. }
  111. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  112. {
  113. asm volatile(LOCK_PREFIX " ; addl $" RW_LOCK_BIAS_STR ",%0"
  114. : "=m" (rw->lock) : : "memory");
  115. }
  116. #define _raw_spin_relax(lock) cpu_relax()
  117. #define _raw_read_relax(lock) cpu_relax()
  118. #define _raw_write_relax(lock) cpu_relax()
  119. #endif /* __ASM_SPINLOCK_H */