rwsem.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * R/W semaphores for ia64
  4. *
  5. * Copyright (C) 2003 Ken Chen <kenneth.w.chen@intel.com>
  6. * Copyright (C) 2003 Asit Mallick <asit.k.mallick@intel.com>
  7. * Copyright (C) 2005 Christoph Lameter <cl@linux.com>
  8. *
  9. * Based on asm-i386/rwsem.h and other architecture implementation.
  10. *
  11. * The MSW of the count is the negated number of active writers and
  12. * waiting lockers, and the LSW is the total number of active locks.
  13. *
  14. * The lock count is initialized to 0 (no active and no waiting lockers).
  15. *
  16. * When a writer subtracts WRITE_BIAS, it'll get 0xffffffff00000001 for
  17. * the case of an uncontended lock. Readers increment by 1 and see a positive
  18. * value when uncontended, negative if there are writers (and maybe) readers
  19. * waiting (in which case it goes to sleep).
  20. */
  21. #ifndef _ASM_IA64_RWSEM_H
  22. #define _ASM_IA64_RWSEM_H
  23. #ifndef _LINUX_RWSEM_H
  24. #error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead."
  25. #endif
  26. #include <asm/intrinsics.h>
  27. #define RWSEM_UNLOCKED_VALUE __IA64_UL_CONST(0x0000000000000000)
  28. #define RWSEM_ACTIVE_BIAS (1L)
  29. #define RWSEM_ACTIVE_MASK (0xffffffffL)
  30. #define RWSEM_WAITING_BIAS (-0x100000000L)
  31. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  32. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  33. /*
  34. * lock for reading
  35. */
  36. static inline int
  37. ___down_read (struct rw_semaphore *sem)
  38. {
  39. long result = ia64_fetchadd8_acq((unsigned long *)&sem->count.counter, 1);
  40. return (result < 0);
  41. }
  42. static inline void
  43. __down_read (struct rw_semaphore *sem)
  44. {
  45. if (___down_read(sem))
  46. rwsem_down_read_failed(sem);
  47. }
  48. static inline int
  49. __down_read_killable (struct rw_semaphore *sem)
  50. {
  51. if (___down_read(sem))
  52. if (IS_ERR(rwsem_down_read_failed_killable(sem)))
  53. return -EINTR;
  54. return 0;
  55. }
  56. /*
  57. * lock for writing
  58. */
  59. static inline long
  60. ___down_write (struct rw_semaphore *sem)
  61. {
  62. long old, new;
  63. do {
  64. old = atomic_long_read(&sem->count);
  65. new = old + RWSEM_ACTIVE_WRITE_BIAS;
  66. } while (atomic_long_cmpxchg_acquire(&sem->count, old, new) != old);
  67. return old;
  68. }
  69. static inline void
  70. __down_write (struct rw_semaphore *sem)
  71. {
  72. if (___down_write(sem))
  73. rwsem_down_write_failed(sem);
  74. }
  75. static inline int
  76. __down_write_killable (struct rw_semaphore *sem)
  77. {
  78. if (___down_write(sem)) {
  79. if (IS_ERR(rwsem_down_write_failed_killable(sem)))
  80. return -EINTR;
  81. }
  82. return 0;
  83. }
  84. /*
  85. * unlock after reading
  86. */
  87. static inline void
  88. __up_read (struct rw_semaphore *sem)
  89. {
  90. long result = ia64_fetchadd8_rel((unsigned long *)&sem->count.counter, -1);
  91. if (result < 0 && (--result & RWSEM_ACTIVE_MASK) == 0)
  92. rwsem_wake(sem);
  93. }
  94. /*
  95. * unlock after writing
  96. */
  97. static inline void
  98. __up_write (struct rw_semaphore *sem)
  99. {
  100. long old, new;
  101. do {
  102. old = atomic_long_read(&sem->count);
  103. new = old - RWSEM_ACTIVE_WRITE_BIAS;
  104. } while (atomic_long_cmpxchg_release(&sem->count, old, new) != old);
  105. if (new < 0 && (new & RWSEM_ACTIVE_MASK) == 0)
  106. rwsem_wake(sem);
  107. }
  108. /*
  109. * trylock for reading -- returns 1 if successful, 0 if contention
  110. */
  111. static inline int
  112. __down_read_trylock (struct rw_semaphore *sem)
  113. {
  114. long tmp;
  115. while ((tmp = atomic_long_read(&sem->count)) >= 0) {
  116. if (tmp == atomic_long_cmpxchg_acquire(&sem->count, tmp, tmp+1)) {
  117. return 1;
  118. }
  119. }
  120. return 0;
  121. }
  122. /*
  123. * trylock for writing -- returns 1 if successful, 0 if contention
  124. */
  125. static inline int
  126. __down_write_trylock (struct rw_semaphore *sem)
  127. {
  128. long tmp = atomic_long_cmpxchg_acquire(&sem->count,
  129. RWSEM_UNLOCKED_VALUE, RWSEM_ACTIVE_WRITE_BIAS);
  130. return tmp == RWSEM_UNLOCKED_VALUE;
  131. }
  132. /*
  133. * downgrade write lock to read lock
  134. */
  135. static inline void
  136. __downgrade_write (struct rw_semaphore *sem)
  137. {
  138. long old, new;
  139. do {
  140. old = atomic_long_read(&sem->count);
  141. new = old - RWSEM_WAITING_BIAS;
  142. } while (atomic_long_cmpxchg_release(&sem->count, old, new) != old);
  143. if (old < 0)
  144. rwsem_downgrade_wake(sem);
  145. }
  146. #endif /* _ASM_IA64_RWSEM_H */