qspinlock.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Queued spinlock
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
  15. *
  16. * Authors: Waiman Long <waiman.long@hp.com>
  17. */
  18. #ifndef __ASM_GENERIC_QSPINLOCK_H
  19. #define __ASM_GENERIC_QSPINLOCK_H
  20. #include <asm-generic/qspinlock_types.h>
  21. /**
  22. * queued_spin_is_locked - is the spinlock locked?
  23. * @lock: Pointer to queued spinlock structure
  24. * Return: 1 if it is locked, 0 otherwise
  25. */
  26. static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
  27. {
  28. return atomic_read(&lock->val);
  29. }
  30. /**
  31. * queued_spin_value_unlocked - is the spinlock structure unlocked?
  32. * @lock: queued spinlock structure
  33. * Return: 1 if it is unlocked, 0 otherwise
  34. *
  35. * N.B. Whenever there are tasks waiting for the lock, it is considered
  36. * locked wrt the lockref code to avoid lock stealing by the lockref
  37. * code and change things underneath the lock. This also allows some
  38. * optimizations to be applied without conflict with lockref.
  39. */
  40. static __always_inline int queued_spin_value_unlocked(struct qspinlock lock)
  41. {
  42. return !atomic_read(&lock.val);
  43. }
  44. /**
  45. * queued_spin_is_contended - check if the lock is contended
  46. * @lock : Pointer to queued spinlock structure
  47. * Return: 1 if lock contended, 0 otherwise
  48. */
  49. static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
  50. {
  51. return atomic_read(&lock->val) & ~_Q_LOCKED_MASK;
  52. }
  53. /**
  54. * queued_spin_trylock - try to acquire the queued spinlock
  55. * @lock : Pointer to queued spinlock structure
  56. * Return: 1 if lock acquired, 0 if failed
  57. */
  58. static __always_inline int queued_spin_trylock(struct qspinlock *lock)
  59. {
  60. if (!atomic_read(&lock->val) &&
  61. (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) == 0))
  62. return 1;
  63. return 0;
  64. }
  65. extern void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
  66. /**
  67. * queued_spin_lock - acquire a queued spinlock
  68. * @lock: Pointer to queued spinlock structure
  69. */
  70. static __always_inline void queued_spin_lock(struct qspinlock *lock)
  71. {
  72. u32 val;
  73. val = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL);
  74. if (likely(val == 0))
  75. return;
  76. queued_spin_lock_slowpath(lock, val);
  77. }
  78. #ifndef queued_spin_unlock
  79. /**
  80. * queued_spin_unlock - release a queued spinlock
  81. * @lock : Pointer to queued spinlock structure
  82. */
  83. static __always_inline void queued_spin_unlock(struct qspinlock *lock)
  84. {
  85. /*
  86. * smp_mb__before_atomic() in order to guarantee release semantics
  87. */
  88. smp_mb__before_atomic_dec();
  89. atomic_sub(_Q_LOCKED_VAL, &lock->val);
  90. }
  91. #endif
  92. /**
  93. * queued_spin_unlock_wait - wait until current lock holder releases the lock
  94. * @lock : Pointer to queued spinlock structure
  95. *
  96. * There is a very slight possibility of live-lock if the lockers keep coming
  97. * and the waiter is just unfortunate enough to not see any unlock state.
  98. */
  99. static inline void queued_spin_unlock_wait(struct qspinlock *lock)
  100. {
  101. while (atomic_read(&lock->val) & _Q_LOCKED_MASK)
  102. cpu_relax();
  103. }
  104. #ifndef virt_spin_lock
  105. static __always_inline bool virt_spin_lock(struct qspinlock *lock)
  106. {
  107. return false;
  108. }
  109. #endif
  110. /*
  111. * Initializier
  112. */
  113. #define __ARCH_SPIN_LOCK_UNLOCKED { ATOMIC_INIT(0) }
  114. /*
  115. * Remapping spinlock architecture specific functions to the corresponding
  116. * queued spinlock functions.
  117. */
  118. #define arch_spin_is_locked(l) queued_spin_is_locked(l)
  119. #define arch_spin_is_contended(l) queued_spin_is_contended(l)
  120. #define arch_spin_value_unlocked(l) queued_spin_value_unlocked(l)
  121. #define arch_spin_lock(l) queued_spin_lock(l)
  122. #define arch_spin_trylock(l) queued_spin_trylock(l)
  123. #define arch_spin_unlock(l) queued_spin_unlock(l)
  124. #define arch_spin_lock_flags(l, f) queued_spin_lock(l)
  125. #define arch_spin_unlock_wait(l) queued_spin_unlock_wait(l)
  126. #endif /* __ASM_GENERIC_QSPINLOCK_H */