rwsem.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef _ASM_POWERPC_RWSEM_H
  2. #define _ASM_POWERPC_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. * include/asm-powerpc/rwsem.h: R/W semaphores for PPC using the stuff
  9. * in lib/rwsem.c. Adapted largely from include/asm-i386/rwsem.h
  10. * by Paul Mackerras <paulus@samba.org>.
  11. */
  12. #include <linux/list.h>
  13. #include <linux/spinlock.h>
  14. #include <asm/atomic.h>
  15. #include <asm/system.h>
  16. /*
  17. * the semaphore definition
  18. */
  19. struct rw_semaphore {
  20. /* XXX this should be able to be an atomic_t -- paulus */
  21. signed int count;
  22. #define RWSEM_UNLOCKED_VALUE 0x00000000
  23. #define RWSEM_ACTIVE_BIAS 0x00000001
  24. #define RWSEM_ACTIVE_MASK 0x0000ffff
  25. #define RWSEM_WAITING_BIAS (-0x00010000)
  26. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  27. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  28. spinlock_t wait_lock;
  29. struct list_head wait_list;
  30. };
  31. #define __RWSEM_INITIALIZER(name) \
  32. { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
  33. LIST_HEAD_INIT((name).wait_list) }
  34. #define DECLARE_RWSEM(name) \
  35. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  36. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  37. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  38. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
  39. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  40. static inline void init_rwsem(struct rw_semaphore *sem)
  41. {
  42. sem->count = RWSEM_UNLOCKED_VALUE;
  43. spin_lock_init(&sem->wait_lock);
  44. INIT_LIST_HEAD(&sem->wait_list);
  45. }
  46. /*
  47. * lock for reading
  48. */
  49. static inline void __down_read(struct rw_semaphore *sem)
  50. {
  51. if (unlikely(atomic_inc_return((atomic_t *)(&sem->count)) <= 0))
  52. rwsem_down_read_failed(sem);
  53. }
  54. static inline int __down_read_trylock(struct rw_semaphore *sem)
  55. {
  56. int tmp;
  57. while ((tmp = sem->count) >= 0) {
  58. if (tmp == cmpxchg(&sem->count, tmp,
  59. tmp + RWSEM_ACTIVE_READ_BIAS)) {
  60. return 1;
  61. }
  62. }
  63. return 0;
  64. }
  65. /*
  66. * lock for writing
  67. */
  68. static inline void __down_write(struct rw_semaphore *sem)
  69. {
  70. int tmp;
  71. tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS,
  72. (atomic_t *)(&sem->count));
  73. if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
  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. return tmp == RWSEM_UNLOCKED_VALUE;
  82. }
  83. /*
  84. * unlock after reading
  85. */
  86. static inline void __up_read(struct rw_semaphore *sem)
  87. {
  88. int tmp;
  89. tmp = atomic_dec_return((atomic_t *)(&sem->count));
  90. if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0))
  91. rwsem_wake(sem);
  92. }
  93. /*
  94. * unlock after writing
  95. */
  96. static inline void __up_write(struct rw_semaphore *sem)
  97. {
  98. if (unlikely(atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
  99. (atomic_t *)(&sem->count)) < 0))
  100. rwsem_wake(sem);
  101. }
  102. /*
  103. * implement atomic add functionality
  104. */
  105. static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
  106. {
  107. atomic_add(delta, (atomic_t *)(&sem->count));
  108. }
  109. /*
  110. * downgrade write lock to read lock
  111. */
  112. static inline void __downgrade_write(struct rw_semaphore *sem)
  113. {
  114. int tmp;
  115. tmp = atomic_add_return(-RWSEM_WAITING_BIAS, (atomic_t *)(&sem->count));
  116. if (tmp < 0)
  117. rwsem_downgrade_wake(sem);
  118. }
  119. /*
  120. * implement exchange and add functionality
  121. */
  122. static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
  123. {
  124. return atomic_add_return(delta, (atomic_t *)(&sem->count));
  125. }
  126. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  127. {
  128. return (sem->count != 0);
  129. }
  130. #endif /* __KERNEL__ */
  131. #endif /* _ASM_POWERPC_RWSEM_H */