rwsem.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * include/asm-ppc/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. #ifdef __KERNEL__
  8. #include <linux/list.h>
  9. #include <linux/spinlock.h>
  10. #include <asm/atomic.h>
  11. #include <asm/system.h>
  12. /*
  13. * the semaphore definition
  14. */
  15. struct rw_semaphore {
  16. long count;
  17. #define RWSEM_UNLOCKED_VALUE 0x00000000
  18. #define RWSEM_ACTIVE_BIAS 0x00000001
  19. #define RWSEM_ACTIVE_MASK 0x0000ffff
  20. #define RWSEM_WAITING_BIAS (-0x00010000)
  21. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  22. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  23. spinlock_t wait_lock;
  24. struct list_head wait_list;
  25. };
  26. #define __RWSEM_INITIALIZER(name) \
  27. { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
  28. LIST_HEAD_INIT((name).wait_list) }
  29. #define DECLARE_RWSEM(name) \
  30. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  31. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  32. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  33. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
  34. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  35. static inline void init_rwsem(struct rw_semaphore *sem)
  36. {
  37. sem->count = RWSEM_UNLOCKED_VALUE;
  38. spin_lock_init(&sem->wait_lock);
  39. INIT_LIST_HEAD(&sem->wait_list);
  40. }
  41. /*
  42. * lock for reading
  43. */
  44. static inline void __down_read(struct rw_semaphore *sem)
  45. {
  46. if (atomic_inc_return((atomic_t *)(&sem->count)) > 0)
  47. smp_wmb();
  48. else
  49. rwsem_down_read_failed(sem);
  50. }
  51. static inline int __down_read_trylock(struct rw_semaphore *sem)
  52. {
  53. int tmp;
  54. while ((tmp = sem->count) >= 0) {
  55. if (tmp == cmpxchg(&sem->count, tmp,
  56. tmp + RWSEM_ACTIVE_READ_BIAS)) {
  57. smp_wmb();
  58. return 1;
  59. }
  60. }
  61. return 0;
  62. }
  63. /*
  64. * lock for writing
  65. */
  66. static inline void __down_write(struct rw_semaphore *sem)
  67. {
  68. int tmp;
  69. tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS,
  70. (atomic_t *)(&sem->count));
  71. if (tmp == RWSEM_ACTIVE_WRITE_BIAS)
  72. smp_wmb();
  73. else
  74. rwsem_down_write_failed(sem);
  75. }
  76. static inline int __down_write_trylock(struct rw_semaphore *sem)
  77. {
  78. int tmp;
  79. tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
  80. RWSEM_ACTIVE_WRITE_BIAS);
  81. smp_wmb();
  82. return tmp == RWSEM_UNLOCKED_VALUE;
  83. }
  84. /*
  85. * unlock after reading
  86. */
  87. static inline void __up_read(struct rw_semaphore *sem)
  88. {
  89. int tmp;
  90. smp_wmb();
  91. tmp = atomic_dec_return((atomic_t *)(&sem->count));
  92. if (tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)
  93. rwsem_wake(sem);
  94. }
  95. /*
  96. * unlock after writing
  97. */
  98. static inline void __up_write(struct rw_semaphore *sem)
  99. {
  100. smp_wmb();
  101. if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
  102. (atomic_t *)(&sem->count)) < 0)
  103. rwsem_wake(sem);
  104. }
  105. /*
  106. * implement atomic add functionality
  107. */
  108. static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
  109. {
  110. atomic_add(delta, (atomic_t *)(&sem->count));
  111. }
  112. /*
  113. * downgrade write lock to read lock
  114. */
  115. static inline void __downgrade_write(struct rw_semaphore *sem)
  116. {
  117. int tmp;
  118. smp_wmb();
  119. tmp = atomic_add_return(-RWSEM_WAITING_BIAS, (atomic_t *)(&sem->count));
  120. if (tmp < 0)
  121. rwsem_downgrade_wake(sem);
  122. }
  123. /*
  124. * implement exchange and add functionality
  125. */
  126. static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
  127. {
  128. smp_mb();
  129. return atomic_add_return(delta, (atomic_t *)(&sem->count));
  130. }
  131. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  132. {
  133. return (sem->count != 0);
  134. }
  135. #endif /* __KERNEL__ */
  136. #endif /* _ASM_SH_RWSEM_H */