rwsem.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_RWSEM_H
  3. #define _ASM_GENERIC_RWSEM_H
  4. #ifndef _LINUX_RWSEM_H
  5. #error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead."
  6. #endif
  7. #ifdef __KERNEL__
  8. /*
  9. * R/W semaphores originally for PPC using the stuff in lib/rwsem.c.
  10. * Adapted largely from include/asm-i386/rwsem.h
  11. * by Paul Mackerras <paulus@samba.org>.
  12. */
  13. /*
  14. * the semaphore definition
  15. */
  16. #ifdef CONFIG_64BIT
  17. # define RWSEM_ACTIVE_MASK 0xffffffffL
  18. #else
  19. # define RWSEM_ACTIVE_MASK 0x0000ffffL
  20. #endif
  21. #define RWSEM_UNLOCKED_VALUE 0x00000000L
  22. #define RWSEM_ACTIVE_BIAS 0x00000001L
  23. #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
  24. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  25. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  26. /*
  27. * lock for reading
  28. */
  29. static inline void __down_read(struct rw_semaphore *sem)
  30. {
  31. if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0))
  32. rwsem_down_read_failed(sem);
  33. }
  34. static inline int __down_read_killable(struct rw_semaphore *sem)
  35. {
  36. if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) {
  37. if (IS_ERR(rwsem_down_read_failed_killable(sem)))
  38. return -EINTR;
  39. }
  40. return 0;
  41. }
  42. static inline int __down_read_trylock(struct rw_semaphore *sem)
  43. {
  44. long tmp;
  45. while ((tmp = atomic_long_read(&sem->count)) >= 0) {
  46. if (tmp == atomic_long_cmpxchg_acquire(&sem->count, tmp,
  47. tmp + RWSEM_ACTIVE_READ_BIAS)) {
  48. return 1;
  49. }
  50. }
  51. return 0;
  52. }
  53. /*
  54. * lock for writing
  55. */
  56. static inline void __down_write(struct rw_semaphore *sem)
  57. {
  58. long tmp;
  59. tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
  60. &sem->count);
  61. if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
  62. rwsem_down_write_failed(sem);
  63. }
  64. static inline int __down_write_killable(struct rw_semaphore *sem)
  65. {
  66. long tmp;
  67. tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
  68. &sem->count);
  69. if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
  70. if (IS_ERR(rwsem_down_write_failed_killable(sem)))
  71. return -EINTR;
  72. return 0;
  73. }
  74. static inline int __down_write_trylock(struct rw_semaphore *sem)
  75. {
  76. long tmp;
  77. tmp = atomic_long_cmpxchg_acquire(&sem->count, RWSEM_UNLOCKED_VALUE,
  78. RWSEM_ACTIVE_WRITE_BIAS);
  79. return tmp == RWSEM_UNLOCKED_VALUE;
  80. }
  81. /*
  82. * unlock after reading
  83. */
  84. static inline void __up_read(struct rw_semaphore *sem)
  85. {
  86. long tmp;
  87. tmp = atomic_long_dec_return_release(&sem->count);
  88. if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0))
  89. rwsem_wake(sem);
  90. }
  91. /*
  92. * unlock after writing
  93. */
  94. static inline void __up_write(struct rw_semaphore *sem)
  95. {
  96. if (unlikely(atomic_long_sub_return_release(RWSEM_ACTIVE_WRITE_BIAS,
  97. &sem->count) < 0))
  98. rwsem_wake(sem);
  99. }
  100. /*
  101. * downgrade write lock to read lock
  102. */
  103. static inline void __downgrade_write(struct rw_semaphore *sem)
  104. {
  105. long tmp;
  106. /*
  107. * When downgrading from exclusive to shared ownership,
  108. * anything inside the write-locked region cannot leak
  109. * into the read side. In contrast, anything in the
  110. * read-locked region is ok to be re-ordered into the
  111. * write side. As such, rely on RELEASE semantics.
  112. */
  113. tmp = atomic_long_add_return_release(-RWSEM_WAITING_BIAS, &sem->count);
  114. if (tmp < 0)
  115. rwsem_downgrade_wake(sem);
  116. }
  117. #endif /* __KERNEL__ */
  118. #endif /* _ASM_GENERIC_RWSEM_H */