rwsem.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * asm-ia64/rwsem.h: R/W semaphores for ia64
  3. *
  4. * Copyright (C) 2003 Ken Chen <kenneth.w.chen@intel.com>
  5. * Copyright (C) 2003 Asit Mallick <asit.k.mallick@intel.com>
  6. *
  7. * Based on asm-i386/rwsem.h and other architecture implementation.
  8. *
  9. * The MSW of the count is the negated number of active writers and
  10. * waiting lockers, and the LSW is the total number of active locks.
  11. *
  12. * The lock count is initialized to 0 (no active and no waiting lockers).
  13. *
  14. * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case
  15. * of an uncontended lock. Readers increment by 1 and see a positive value
  16. * when uncontended, negative if there are writers (and maybe) readers
  17. * waiting (in which case it goes to sleep).
  18. */
  19. #ifndef _ASM_IA64_RWSEM_H
  20. #define _ASM_IA64_RWSEM_H
  21. #include <linux/list.h>
  22. #include <linux/spinlock.h>
  23. #include <asm/intrinsics.h>
  24. /*
  25. * the semaphore definition
  26. */
  27. struct rw_semaphore {
  28. signed int count;
  29. spinlock_t wait_lock;
  30. struct list_head wait_list;
  31. #if RWSEM_DEBUG
  32. int debug;
  33. #endif
  34. };
  35. #define RWSEM_UNLOCKED_VALUE 0x00000000
  36. #define RWSEM_ACTIVE_BIAS 0x00000001
  37. #define RWSEM_ACTIVE_MASK 0x0000ffff
  38. #define RWSEM_WAITING_BIAS (-0x00010000)
  39. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  40. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  41. /*
  42. * initialization
  43. */
  44. #if RWSEM_DEBUG
  45. #define __RWSEM_DEBUG_INIT , 0
  46. #else
  47. #define __RWSEM_DEBUG_INIT /* */
  48. #endif
  49. #define __RWSEM_INITIALIZER(name) \
  50. { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
  51. LIST_HEAD_INIT((name).wait_list) \
  52. __RWSEM_DEBUG_INIT }
  53. #define DECLARE_RWSEM(name) \
  54. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  55. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  56. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  57. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
  58. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  59. static inline void
  60. init_rwsem (struct rw_semaphore *sem)
  61. {
  62. sem->count = RWSEM_UNLOCKED_VALUE;
  63. spin_lock_init(&sem->wait_lock);
  64. INIT_LIST_HEAD(&sem->wait_list);
  65. #if RWSEM_DEBUG
  66. sem->debug = 0;
  67. #endif
  68. }
  69. /*
  70. * lock for reading
  71. */
  72. static inline void
  73. __down_read (struct rw_semaphore *sem)
  74. {
  75. int result = ia64_fetchadd4_acq((unsigned int *)&sem->count, 1);
  76. if (result < 0)
  77. rwsem_down_read_failed(sem);
  78. }
  79. /*
  80. * lock for writing
  81. */
  82. static inline void
  83. __down_write (struct rw_semaphore *sem)
  84. {
  85. int old, new;
  86. do {
  87. old = sem->count;
  88. new = old + RWSEM_ACTIVE_WRITE_BIAS;
  89. } while (cmpxchg_acq(&sem->count, old, new) != old);
  90. if (old != 0)
  91. rwsem_down_write_failed(sem);
  92. }
  93. /*
  94. * unlock after reading
  95. */
  96. static inline void
  97. __up_read (struct rw_semaphore *sem)
  98. {
  99. int result = ia64_fetchadd4_rel((unsigned int *)&sem->count, -1);
  100. if (result < 0 && (--result & RWSEM_ACTIVE_MASK) == 0)
  101. rwsem_wake(sem);
  102. }
  103. /*
  104. * unlock after writing
  105. */
  106. static inline void
  107. __up_write (struct rw_semaphore *sem)
  108. {
  109. int old, new;
  110. do {
  111. old = sem->count;
  112. new = old - RWSEM_ACTIVE_WRITE_BIAS;
  113. } while (cmpxchg_rel(&sem->count, old, new) != old);
  114. if (new < 0 && (new & RWSEM_ACTIVE_MASK) == 0)
  115. rwsem_wake(sem);
  116. }
  117. /*
  118. * trylock for reading -- returns 1 if successful, 0 if contention
  119. */
  120. static inline int
  121. __down_read_trylock (struct rw_semaphore *sem)
  122. {
  123. int tmp;
  124. while ((tmp = sem->count) >= 0) {
  125. if (tmp == cmpxchg_acq(&sem->count, tmp, tmp+1)) {
  126. return 1;
  127. }
  128. }
  129. return 0;
  130. }
  131. /*
  132. * trylock for writing -- returns 1 if successful, 0 if contention
  133. */
  134. static inline int
  135. __down_write_trylock (struct rw_semaphore *sem)
  136. {
  137. int tmp = cmpxchg_acq(&sem->count, RWSEM_UNLOCKED_VALUE,
  138. RWSEM_ACTIVE_WRITE_BIAS);
  139. return tmp == RWSEM_UNLOCKED_VALUE;
  140. }
  141. /*
  142. * downgrade write lock to read lock
  143. */
  144. static inline void
  145. __downgrade_write (struct rw_semaphore *sem)
  146. {
  147. int old, new;
  148. do {
  149. old = sem->count;
  150. new = old - RWSEM_WAITING_BIAS;
  151. } while (cmpxchg_rel(&sem->count, old, new) != old);
  152. if (old < 0)
  153. rwsem_downgrade_wake(sem);
  154. }
  155. /*
  156. * Implement atomic add functionality. These used to be "inline" functions, but GCC v3.1
  157. * doesn't quite optimize this stuff right and ends up with bad calls to fetchandadd.
  158. */
  159. #define rwsem_atomic_add(delta, sem) atomic_add(delta, (atomic_t *)(&(sem)->count))
  160. #define rwsem_atomic_update(delta, sem) atomic_add_return(delta, (atomic_t *)(&(sem)->count))
  161. #endif /* _ASM_IA64_RWSEM_H */