rwsem.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * rwsem.h: R/W semaphores implemented using CAS
  3. *
  4. * Written by David S. Miller (davem@redhat.com), 2001.
  5. * Derived from asm-i386/rwsem.h
  6. */
  7. #ifndef _SPARC64_RWSEM_H
  8. #define _SPARC64_RWSEM_H
  9. #ifndef _LINUX_RWSEM_H
  10. #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
  11. #endif
  12. #ifdef __KERNEL__
  13. #define RWSEM_UNLOCKED_VALUE 0x00000000L
  14. #define RWSEM_ACTIVE_BIAS 0x00000001L
  15. #define RWSEM_ACTIVE_MASK 0xffffffffL
  16. #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
  17. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  18. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  19. /*
  20. * lock for reading
  21. */
  22. static inline void __down_read(struct rw_semaphore *sem)
  23. {
  24. if (unlikely(atomic64_inc_return((atomic64_t *)(&sem->count)) <= 0L))
  25. rwsem_down_read_failed(sem);
  26. }
  27. static inline int __down_read_trylock(struct rw_semaphore *sem)
  28. {
  29. long tmp;
  30. while ((tmp = sem->count) >= 0L) {
  31. if (tmp == cmpxchg(&sem->count, tmp,
  32. tmp + RWSEM_ACTIVE_READ_BIAS)) {
  33. return 1;
  34. }
  35. }
  36. return 0;
  37. }
  38. /*
  39. * lock for writing
  40. */
  41. static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
  42. {
  43. long tmp;
  44. tmp = atomic64_add_return(RWSEM_ACTIVE_WRITE_BIAS,
  45. (atomic64_t *)(&sem->count));
  46. if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
  47. rwsem_down_write_failed(sem);
  48. }
  49. static inline void __down_write(struct rw_semaphore *sem)
  50. {
  51. __down_write_nested(sem, 0);
  52. }
  53. static inline int __down_write_trylock(struct rw_semaphore *sem)
  54. {
  55. long tmp;
  56. tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
  57. RWSEM_ACTIVE_WRITE_BIAS);
  58. return tmp == RWSEM_UNLOCKED_VALUE;
  59. }
  60. /*
  61. * unlock after reading
  62. */
  63. static inline void __up_read(struct rw_semaphore *sem)
  64. {
  65. long tmp;
  66. tmp = atomic64_dec_return((atomic64_t *)(&sem->count));
  67. if (unlikely(tmp < -1L && (tmp & RWSEM_ACTIVE_MASK) == 0L))
  68. rwsem_wake(sem);
  69. }
  70. /*
  71. * unlock after writing
  72. */
  73. static inline void __up_write(struct rw_semaphore *sem)
  74. {
  75. if (unlikely(atomic64_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
  76. (atomic64_t *)(&sem->count)) < 0L))
  77. rwsem_wake(sem);
  78. }
  79. /*
  80. * implement atomic add functionality
  81. */
  82. static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
  83. {
  84. atomic64_add(delta, (atomic64_t *)(&sem->count));
  85. }
  86. /*
  87. * downgrade write lock to read lock
  88. */
  89. static inline void __downgrade_write(struct rw_semaphore *sem)
  90. {
  91. long tmp;
  92. tmp = atomic64_add_return(-RWSEM_WAITING_BIAS, (atomic64_t *)(&sem->count));
  93. if (tmp < 0L)
  94. rwsem_downgrade_wake(sem);
  95. }
  96. /*
  97. * implement exchange and add functionality
  98. */
  99. static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
  100. {
  101. return atomic64_add_return(delta, (atomic64_t *)(&sem->count));
  102. }
  103. #endif /* __KERNEL__ */
  104. #endif /* _SPARC64_RWSEM_H */