qspinlock.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * (C) Copyright 2015 Hewlett-Packard Enterprise Development LP
  16. *
  17. * Authors: Waiman Long <waiman.long@hpe.com>
  18. */
  19. #ifndef __ASM_GENERIC_QSPINLOCK_H
  20. #define __ASM_GENERIC_QSPINLOCK_H
  21. #include <asm-generic/qspinlock_types.h>
  22. /**
  23. * queued_spin_is_locked - is the spinlock locked?
  24. * @lock: Pointer to queued spinlock structure
  25. * Return: 1 if it is locked, 0 otherwise
  26. */
  27. static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
  28. {
  29. /*
  30. * Any !0 state indicates it is locked, even if _Q_LOCKED_VAL
  31. * isn't immediately observable.
  32. */
  33. return atomic_read(&lock->val);
  34. }
  35. /**
  36. * queued_spin_value_unlocked - is the spinlock structure unlocked?
  37. * @lock: queued spinlock structure
  38. * Return: 1 if it is unlocked, 0 otherwise
  39. *
  40. * N.B. Whenever there are tasks waiting for the lock, it is considered
  41. * locked wrt the lockref code to avoid lock stealing by the lockref
  42. * code and change things underneath the lock. This also allows some
  43. * optimizations to be applied without conflict with lockref.
  44. */
  45. static __always_inline int queued_spin_value_unlocked(struct qspinlock lock)
  46. {
  47. return !atomic_read(&lock.val);
  48. }
  49. /**
  50. * queued_spin_is_contended - check if the lock is contended
  51. * @lock : Pointer to queued spinlock structure
  52. * Return: 1 if lock contended, 0 otherwise
  53. */
  54. static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
  55. {
  56. return atomic_read(&lock->val) & ~_Q_LOCKED_MASK;
  57. }
  58. /**
  59. * queued_spin_trylock - try to acquire the queued spinlock
  60. * @lock : Pointer to queued spinlock structure
  61. * Return: 1 if lock acquired, 0 if failed
  62. */
  63. static __always_inline int queued_spin_trylock(struct qspinlock *lock)
  64. {
  65. if (!atomic_read(&lock->val) &&
  66. (atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL) == 0))
  67. return 1;
  68. return 0;
  69. }
  70. extern void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
  71. /**
  72. * queued_spin_lock - acquire a queued spinlock
  73. * @lock: Pointer to queued spinlock structure
  74. */
  75. static __always_inline void queued_spin_lock(struct qspinlock *lock)
  76. {
  77. u32 val;
  78. val = atomic_cmpxchg_acquire(&lock->val, 0, _Q_LOCKED_VAL);
  79. if (likely(val == 0))
  80. return;
  81. queued_spin_lock_slowpath(lock, val);
  82. }
  83. #ifndef queued_spin_unlock
  84. /**
  85. * queued_spin_unlock - release a queued spinlock
  86. * @lock : Pointer to queued spinlock structure
  87. */
  88. static __always_inline void queued_spin_unlock(struct qspinlock *lock)
  89. {
  90. /*
  91. * unlock() needs release semantics:
  92. */
  93. smp_store_release(&lock->locked, 0);
  94. }
  95. #endif
  96. #ifndef virt_spin_lock
  97. static __always_inline bool virt_spin_lock(struct qspinlock *lock)
  98. {
  99. return false;
  100. }
  101. #endif
  102. /*
  103. * Remapping spinlock architecture specific functions to the corresponding
  104. * queued spinlock functions.
  105. */
  106. #define arch_spin_is_locked(l) queued_spin_is_locked(l)
  107. #define arch_spin_is_contended(l) queued_spin_is_contended(l)
  108. #define arch_spin_value_unlocked(l) queued_spin_value_unlocked(l)
  109. #define arch_spin_lock(l) queued_spin_lock(l)
  110. #define arch_spin_trylock(l) queued_spin_trylock(l)
  111. #define arch_spin_unlock(l) queued_spin_unlock(l)
  112. #endif /* __ASM_GENERIC_QSPINLOCK_H */