rwsem.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = sem->count) >= 0) {
  37. if (tmp == 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_nested(struct rw_semaphore *sem, int subclass)
  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 void __down_write(struct rw_semaphore *sem)
  56. {
  57. __down_write_nested(sem, 0);
  58. }
  59. static inline int __down_write_trylock(struct rw_semaphore *sem)
  60. {
  61. long tmp;
  62. tmp = cmpxchg_acquire(&sem->count, RWSEM_UNLOCKED_VALUE,
  63. RWSEM_ACTIVE_WRITE_BIAS);
  64. return tmp == RWSEM_UNLOCKED_VALUE;
  65. }
  66. /*
  67. * unlock after reading
  68. */
  69. static inline void __up_read(struct rw_semaphore *sem)
  70. {
  71. long tmp;
  72. tmp = atomic_long_dec_return_release((atomic_long_t *)&sem->count);
  73. if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0))
  74. rwsem_wake(sem);
  75. }
  76. /*
  77. * unlock after writing
  78. */
  79. static inline void __up_write(struct rw_semaphore *sem)
  80. {
  81. if (unlikely(atomic_long_sub_return_release(RWSEM_ACTIVE_WRITE_BIAS,
  82. (atomic_long_t *)&sem->count) < 0))
  83. rwsem_wake(sem);
  84. }
  85. /*
  86. * implement atomic add functionality
  87. */
  88. static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
  89. {
  90. atomic_long_add(delta, (atomic_long_t *)&sem->count);
  91. }
  92. /*
  93. * downgrade write lock to read lock
  94. */
  95. static inline void __downgrade_write(struct rw_semaphore *sem)
  96. {
  97. long tmp;
  98. /*
  99. * When downgrading from exclusive to shared ownership,
  100. * anything inside the write-locked region cannot leak
  101. * into the read side. In contrast, anything in the
  102. * read-locked region is ok to be re-ordered into the
  103. * write side. As such, rely on RELEASE semantics.
  104. */
  105. tmp = atomic_long_add_return_release(-RWSEM_WAITING_BIAS,
  106. (atomic_long_t *)&sem->count);
  107. if (tmp < 0)
  108. rwsem_downgrade_wake(sem);
  109. }
  110. /*
  111. * implement exchange and add functionality
  112. */
  113. static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
  114. {
  115. return atomic_long_add_return(delta, (atomic_long_t *)&sem->count);
  116. }
  117. #endif /* __KERNEL__ */
  118. #endif /* _ASM_GENERIC_RWSEM_H */