rwsem.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * include/asm-sh/rwsem.h: R/W semaphores for SH using the stuff
  3. * in lib/rwsem.c.
  4. */
  5. #ifndef _ASM_SH_RWSEM_H
  6. #define _ASM_SH_RWSEM_H
  7. #ifndef _LINUX_RWSEM_H
  8. #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
  9. #endif
  10. #ifdef __KERNEL__
  11. #define RWSEM_UNLOCKED_VALUE 0x00000000
  12. #define RWSEM_ACTIVE_BIAS 0x00000001
  13. #define RWSEM_ACTIVE_MASK 0x0000ffff
  14. #define RWSEM_WAITING_BIAS (-0x00010000)
  15. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  16. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  17. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  18. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  19. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
  20. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  21. /*
  22. * lock for reading
  23. */
  24. static inline void __down_read(struct rw_semaphore *sem)
  25. {
  26. if (atomic_inc_return((atomic_t *)(&sem->count)) > 0)
  27. smp_wmb();
  28. else
  29. rwsem_down_read_failed(sem);
  30. }
  31. static inline int __down_read_trylock(struct rw_semaphore *sem)
  32. {
  33. int tmp;
  34. while ((tmp = sem->count) >= 0) {
  35. if (tmp == cmpxchg(&sem->count, tmp,
  36. tmp + RWSEM_ACTIVE_READ_BIAS)) {
  37. smp_wmb();
  38. return 1;
  39. }
  40. }
  41. return 0;
  42. }
  43. /*
  44. * lock for writing
  45. */
  46. static inline void __down_write(struct rw_semaphore *sem)
  47. {
  48. int tmp;
  49. tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS,
  50. (atomic_t *)(&sem->count));
  51. if (tmp == RWSEM_ACTIVE_WRITE_BIAS)
  52. smp_wmb();
  53. else
  54. rwsem_down_write_failed(sem);
  55. }
  56. static inline int __down_write_trylock(struct rw_semaphore *sem)
  57. {
  58. int tmp;
  59. tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
  60. RWSEM_ACTIVE_WRITE_BIAS);
  61. smp_wmb();
  62. return tmp == RWSEM_UNLOCKED_VALUE;
  63. }
  64. /*
  65. * unlock after reading
  66. */
  67. static inline void __up_read(struct rw_semaphore *sem)
  68. {
  69. int tmp;
  70. smp_wmb();
  71. tmp = atomic_dec_return((atomic_t *)(&sem->count));
  72. if (tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)
  73. rwsem_wake(sem);
  74. }
  75. /*
  76. * unlock after writing
  77. */
  78. static inline void __up_write(struct rw_semaphore *sem)
  79. {
  80. smp_wmb();
  81. if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
  82. (atomic_t *)(&sem->count)) < 0)
  83. rwsem_wake(sem);
  84. }
  85. /*
  86. * implement atomic add functionality
  87. */
  88. static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
  89. {
  90. atomic_add(delta, (atomic_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. int tmp;
  98. smp_wmb();
  99. tmp = atomic_add_return(-RWSEM_WAITING_BIAS, (atomic_t *)(&sem->count));
  100. if (tmp < 0)
  101. rwsem_downgrade_wake(sem);
  102. }
  103. static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
  104. {
  105. __down_write(sem);
  106. }
  107. /*
  108. * implement exchange and add functionality
  109. */
  110. static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
  111. {
  112. smp_mb();
  113. return atomic_add_return(delta, (atomic_t *)(&sem->count));
  114. }
  115. #endif /* __KERNEL__ */
  116. #endif /* _ASM_SH_RWSEM_H */