qrwlock.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Queue read/write lock
  4. *
  5. * These use generic atomic and locking routines, but depend on a fair spinlock
  6. * implementation in order to be fair themselves. The implementation in
  7. * asm-generic/spinlock.h meets these requirements.
  8. *
  9. * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
  10. *
  11. * Authors: Waiman Long <[email protected]>
  12. */
  13. #ifndef __ASM_GENERIC_QRWLOCK_H
  14. #define __ASM_GENERIC_QRWLOCK_H
  15. #include <linux/atomic.h>
  16. #include <asm/barrier.h>
  17. #include <asm/processor.h>
  18. #include <asm-generic/qrwlock_types.h>
  19. /* Must be included from asm/spinlock.h after defining arch_spin_is_locked. */
  20. /*
  21. * Writer states & reader shift and bias.
  22. */
  23. #define _QW_WAITING 0x100 /* A writer is waiting */
  24. #define _QW_LOCKED 0x0ff /* A writer holds the lock */
  25. #define _QW_WMASK 0x1ff /* Writer mask */
  26. #define _QR_SHIFT 9 /* Reader count shift */
  27. #define _QR_BIAS (1U << _QR_SHIFT)
  28. /*
  29. * External function declarations
  30. */
  31. extern void queued_read_lock_slowpath(struct qrwlock *lock);
  32. extern void queued_write_lock_slowpath(struct qrwlock *lock);
  33. /**
  34. * queued_read_trylock - try to acquire read lock of a queued rwlock
  35. * @lock : Pointer to queued rwlock structure
  36. * Return: 1 if lock acquired, 0 if failed
  37. */
  38. static inline int queued_read_trylock(struct qrwlock *lock)
  39. {
  40. int cnts;
  41. cnts = atomic_read(&lock->cnts);
  42. if (likely(!(cnts & _QW_WMASK))) {
  43. cnts = (u32)atomic_add_return_acquire(_QR_BIAS, &lock->cnts);
  44. if (likely(!(cnts & _QW_WMASK)))
  45. return 1;
  46. atomic_sub(_QR_BIAS, &lock->cnts);
  47. }
  48. return 0;
  49. }
  50. /**
  51. * queued_write_trylock - try to acquire write lock of a queued rwlock
  52. * @lock : Pointer to queued rwlock structure
  53. * Return: 1 if lock acquired, 0 if failed
  54. */
  55. static inline int queued_write_trylock(struct qrwlock *lock)
  56. {
  57. int cnts;
  58. cnts = atomic_read(&lock->cnts);
  59. if (unlikely(cnts))
  60. return 0;
  61. return likely(atomic_try_cmpxchg_acquire(&lock->cnts, &cnts,
  62. _QW_LOCKED));
  63. }
  64. /**
  65. * queued_read_lock - acquire read lock of a queued rwlock
  66. * @lock: Pointer to queued rwlock structure
  67. */
  68. static inline void queued_read_lock(struct qrwlock *lock)
  69. {
  70. int cnts;
  71. cnts = atomic_add_return_acquire(_QR_BIAS, &lock->cnts);
  72. if (likely(!(cnts & _QW_WMASK)))
  73. return;
  74. /* The slowpath will decrement the reader count, if necessary. */
  75. queued_read_lock_slowpath(lock);
  76. }
  77. /**
  78. * queued_write_lock - acquire write lock of a queued rwlock
  79. * @lock : Pointer to queued rwlock structure
  80. */
  81. static inline void queued_write_lock(struct qrwlock *lock)
  82. {
  83. int cnts = 0;
  84. /* Optimize for the unfair lock case where the fair flag is 0. */
  85. if (likely(atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED)))
  86. return;
  87. queued_write_lock_slowpath(lock);
  88. }
  89. /**
  90. * queued_read_unlock - release read lock of a queued rwlock
  91. * @lock : Pointer to queued rwlock structure
  92. */
  93. static inline void queued_read_unlock(struct qrwlock *lock)
  94. {
  95. /*
  96. * Atomically decrement the reader count
  97. */
  98. (void)atomic_sub_return_release(_QR_BIAS, &lock->cnts);
  99. }
  100. /**
  101. * queued_write_unlock - release write lock of a queued rwlock
  102. * @lock : Pointer to queued rwlock structure
  103. */
  104. static inline void queued_write_unlock(struct qrwlock *lock)
  105. {
  106. smp_store_release(&lock->wlocked, 0);
  107. }
  108. /**
  109. * queued_rwlock_is_contended - check if the lock is contended
  110. * @lock : Pointer to queued rwlock structure
  111. * Return: 1 if lock contended, 0 otherwise
  112. */
  113. static inline int queued_rwlock_is_contended(struct qrwlock *lock)
  114. {
  115. return arch_spin_is_locked(&lock->wait_lock);
  116. }
  117. /*
  118. * Remapping rwlock architecture specific functions to the corresponding
  119. * queued rwlock functions.
  120. */
  121. #define arch_read_lock(l) queued_read_lock(l)
  122. #define arch_write_lock(l) queued_write_lock(l)
  123. #define arch_read_trylock(l) queued_read_trylock(l)
  124. #define arch_write_trylock(l) queued_write_trylock(l)
  125. #define arch_read_unlock(l) queued_read_unlock(l)
  126. #define arch_write_unlock(l) queued_write_unlock(l)
  127. #define arch_rwlock_is_contended(l) queued_rwlock_is_contended(l)
  128. #endif /* __ASM_GENERIC_QRWLOCK_H */