rwsem.h 3.1 KB

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