rwsem.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ALPHA_RWSEM_H
  3. #define _ALPHA_RWSEM_H
  4. /*
  5. * Written by Ivan Kokshaysky <ink@jurassic.park.msu.ru>, 2001.
  6. * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
  7. */
  8. #ifndef _LINUX_RWSEM_H
  9. #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
  10. #endif
  11. #ifdef __KERNEL__
  12. #include <linux/compiler.h>
  13. #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
  14. #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
  15. #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
  16. #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
  17. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  18. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  19. static inline int ___down_read(struct rw_semaphore *sem)
  20. {
  21. long oldcount;
  22. #ifndef CONFIG_SMP
  23. oldcount = sem->count.counter;
  24. sem->count.counter += RWSEM_ACTIVE_READ_BIAS;
  25. #else
  26. long temp;
  27. __asm__ __volatile__(
  28. "1: ldq_l %0,%1\n"
  29. " addq %0,%3,%2\n"
  30. " stq_c %2,%1\n"
  31. " beq %2,2f\n"
  32. " mb\n"
  33. ".subsection 2\n"
  34. "2: br 1b\n"
  35. ".previous"
  36. :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
  37. :"Ir" (RWSEM_ACTIVE_READ_BIAS), "m" (sem->count) : "memory");
  38. #endif
  39. return (oldcount < 0);
  40. }
  41. static inline void __down_read(struct rw_semaphore *sem)
  42. {
  43. if (unlikely(___down_read(sem)))
  44. rwsem_down_read_failed(sem);
  45. }
  46. static inline int __down_read_killable(struct rw_semaphore *sem)
  47. {
  48. if (unlikely(___down_read(sem)))
  49. if (IS_ERR(rwsem_down_read_failed_killable(sem)))
  50. return -EINTR;
  51. return 0;
  52. }
  53. /*
  54. * trylock for reading -- returns 1 if successful, 0 if contention
  55. */
  56. static inline int __down_read_trylock(struct rw_semaphore *sem)
  57. {
  58. long old, new, res;
  59. res = atomic_long_read(&sem->count);
  60. do {
  61. new = res + RWSEM_ACTIVE_READ_BIAS;
  62. if (new <= 0)
  63. break;
  64. old = res;
  65. res = atomic_long_cmpxchg(&sem->count, old, new);
  66. } while (res != old);
  67. return res >= 0 ? 1 : 0;
  68. }
  69. static inline long ___down_write(struct rw_semaphore *sem)
  70. {
  71. long oldcount;
  72. #ifndef CONFIG_SMP
  73. oldcount = sem->count.counter;
  74. sem->count.counter += RWSEM_ACTIVE_WRITE_BIAS;
  75. #else
  76. long temp;
  77. __asm__ __volatile__(
  78. "1: ldq_l %0,%1\n"
  79. " addq %0,%3,%2\n"
  80. " stq_c %2,%1\n"
  81. " beq %2,2f\n"
  82. " mb\n"
  83. ".subsection 2\n"
  84. "2: br 1b\n"
  85. ".previous"
  86. :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
  87. :"Ir" (RWSEM_ACTIVE_WRITE_BIAS), "m" (sem->count) : "memory");
  88. #endif
  89. return oldcount;
  90. }
  91. static inline void __down_write(struct rw_semaphore *sem)
  92. {
  93. if (unlikely(___down_write(sem)))
  94. rwsem_down_write_failed(sem);
  95. }
  96. static inline int __down_write_killable(struct rw_semaphore *sem)
  97. {
  98. if (unlikely(___down_write(sem))) {
  99. if (IS_ERR(rwsem_down_write_failed_killable(sem)))
  100. return -EINTR;
  101. }
  102. return 0;
  103. }
  104. /*
  105. * trylock for writing -- returns 1 if successful, 0 if contention
  106. */
  107. static inline int __down_write_trylock(struct rw_semaphore *sem)
  108. {
  109. long ret = atomic_long_cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
  110. RWSEM_ACTIVE_WRITE_BIAS);
  111. if (ret == RWSEM_UNLOCKED_VALUE)
  112. return 1;
  113. return 0;
  114. }
  115. static inline void __up_read(struct rw_semaphore *sem)
  116. {
  117. long oldcount;
  118. #ifndef CONFIG_SMP
  119. oldcount = sem->count.counter;
  120. sem->count.counter -= RWSEM_ACTIVE_READ_BIAS;
  121. #else
  122. long temp;
  123. __asm__ __volatile__(
  124. " mb\n"
  125. "1: ldq_l %0,%1\n"
  126. " subq %0,%3,%2\n"
  127. " stq_c %2,%1\n"
  128. " beq %2,2f\n"
  129. ".subsection 2\n"
  130. "2: br 1b\n"
  131. ".previous"
  132. :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
  133. :"Ir" (RWSEM_ACTIVE_READ_BIAS), "m" (sem->count) : "memory");
  134. #endif
  135. if (unlikely(oldcount < 0))
  136. if ((int)oldcount - RWSEM_ACTIVE_READ_BIAS == 0)
  137. rwsem_wake(sem);
  138. }
  139. static inline void __up_write(struct rw_semaphore *sem)
  140. {
  141. long count;
  142. #ifndef CONFIG_SMP
  143. sem->count.counter -= RWSEM_ACTIVE_WRITE_BIAS;
  144. count = sem->count.counter;
  145. #else
  146. long temp;
  147. __asm__ __volatile__(
  148. " mb\n"
  149. "1: ldq_l %0,%1\n"
  150. " subq %0,%3,%2\n"
  151. " stq_c %2,%1\n"
  152. " beq %2,2f\n"
  153. " subq %0,%3,%0\n"
  154. ".subsection 2\n"
  155. "2: br 1b\n"
  156. ".previous"
  157. :"=&r" (count), "=m" (sem->count), "=&r" (temp)
  158. :"Ir" (RWSEM_ACTIVE_WRITE_BIAS), "m" (sem->count) : "memory");
  159. #endif
  160. if (unlikely(count))
  161. if ((int)count == 0)
  162. rwsem_wake(sem);
  163. }
  164. /*
  165. * downgrade write lock to read lock
  166. */
  167. static inline void __downgrade_write(struct rw_semaphore *sem)
  168. {
  169. long oldcount;
  170. #ifndef CONFIG_SMP
  171. oldcount = sem->count.counter;
  172. sem->count.counter -= RWSEM_WAITING_BIAS;
  173. #else
  174. long temp;
  175. __asm__ __volatile__(
  176. "1: ldq_l %0,%1\n"
  177. " addq %0,%3,%2\n"
  178. " stq_c %2,%1\n"
  179. " beq %2,2f\n"
  180. " mb\n"
  181. ".subsection 2\n"
  182. "2: br 1b\n"
  183. ".previous"
  184. :"=&r" (oldcount), "=m" (sem->count), "=&r" (temp)
  185. :"Ir" (-RWSEM_WAITING_BIAS), "m" (sem->count) : "memory");
  186. #endif
  187. if (unlikely(oldcount < 0))
  188. rwsem_downgrade_wake(sem);
  189. }
  190. #endif /* __KERNEL__ */
  191. #endif /* _ALPHA_RWSEM_H */