rwsem.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #if RWSEM_DEBUG
  26. int debug;
  27. #endif
  28. };
  29. /*
  30. * initialisation
  31. */
  32. #if RWSEM_DEBUG
  33. #define __RWSEM_DEBUG_INIT , 0
  34. #else
  35. #define __RWSEM_DEBUG_INIT /* */
  36. #endif
  37. #define __RWSEM_INITIALIZER(name) \
  38. { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
  39. LIST_HEAD_INIT((name).wait_list) \
  40. __RWSEM_DEBUG_INIT }
  41. #define DECLARE_RWSEM(name) \
  42. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  43. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  44. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  45. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
  46. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  47. static inline void init_rwsem(struct rw_semaphore *sem)
  48. {
  49. sem->count = RWSEM_UNLOCKED_VALUE;
  50. spin_lock_init(&sem->wait_lock);
  51. INIT_LIST_HEAD(&sem->wait_list);
  52. #if RWSEM_DEBUG
  53. sem->debug = 0;
  54. #endif
  55. }
  56. /*
  57. * lock for reading
  58. */
  59. static inline void __down_read(struct rw_semaphore *sem)
  60. {
  61. if (atomic_inc_return((atomic_t *)(&sem->count)) > 0)
  62. smp_wmb();
  63. else
  64. rwsem_down_read_failed(sem);
  65. }
  66. static inline int __down_read_trylock(struct rw_semaphore *sem)
  67. {
  68. int tmp;
  69. while ((tmp = sem->count) >= 0) {
  70. if (tmp == cmpxchg(&sem->count, tmp,
  71. tmp + RWSEM_ACTIVE_READ_BIAS)) {
  72. smp_wmb();
  73. return 1;
  74. }
  75. }
  76. return 0;
  77. }
  78. /*
  79. * lock for writing
  80. */
  81. static inline void __down_write(struct rw_semaphore *sem)
  82. {
  83. int tmp;
  84. tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS,
  85. (atomic_t *)(&sem->count));
  86. if (tmp == RWSEM_ACTIVE_WRITE_BIAS)
  87. smp_wmb();
  88. else
  89. rwsem_down_write_failed(sem);
  90. }
  91. static inline int __down_write_trylock(struct rw_semaphore *sem)
  92. {
  93. int tmp;
  94. tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
  95. RWSEM_ACTIVE_WRITE_BIAS);
  96. smp_wmb();
  97. return tmp == RWSEM_UNLOCKED_VALUE;
  98. }
  99. /*
  100. * unlock after reading
  101. */
  102. static inline void __up_read(struct rw_semaphore *sem)
  103. {
  104. int tmp;
  105. smp_wmb();
  106. tmp = atomic_dec_return((atomic_t *)(&sem->count));
  107. if (tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)
  108. rwsem_wake(sem);
  109. }
  110. /*
  111. * unlock after writing
  112. */
  113. static inline void __up_write(struct rw_semaphore *sem)
  114. {
  115. smp_wmb();
  116. if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
  117. (atomic_t *)(&sem->count)) < 0)
  118. rwsem_wake(sem);
  119. }
  120. /*
  121. * implement atomic add functionality
  122. */
  123. static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
  124. {
  125. atomic_add(delta, (atomic_t *)(&sem->count));
  126. }
  127. /*
  128. * downgrade write lock to read lock
  129. */
  130. static inline void __downgrade_write(struct rw_semaphore *sem)
  131. {
  132. int tmp;
  133. smp_wmb();
  134. tmp = atomic_add_return(-RWSEM_WAITING_BIAS, (atomic_t *)(&sem->count));
  135. if (tmp < 0)
  136. rwsem_downgrade_wake(sem);
  137. }
  138. /*
  139. * implement exchange and add functionality
  140. */
  141. static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
  142. {
  143. smp_mb();
  144. return atomic_add_return(delta, (atomic_t *)(&sem->count));
  145. }
  146. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  147. {
  148. return (sem->count != 0);
  149. }
  150. #endif /* __KERNEL__ */
  151. #endif /* _ASM_SH_RWSEM_H */