rwsem.h 4.6 KB

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