rwsem.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * 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. #ifndef _LINUX_RWSEM_H
  23. #error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead."
  24. #endif
  25. #include <linux/list.h>
  26. #include <linux/spinlock.h>
  27. #include <asm/intrinsics.h>
  28. /*
  29. * the semaphore definition
  30. */
  31. struct rw_semaphore {
  32. signed long count;
  33. spinlock_t wait_lock;
  34. struct list_head wait_list;
  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. #define __RWSEM_INITIALIZER(name) \
  43. { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
  44. LIST_HEAD_INIT((name).wait_list) }
  45. #define DECLARE_RWSEM(name) \
  46. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  47. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  48. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  49. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
  50. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  51. static inline void
  52. init_rwsem (struct rw_semaphore *sem)
  53. {
  54. sem->count = RWSEM_UNLOCKED_VALUE;
  55. spin_lock_init(&sem->wait_lock);
  56. INIT_LIST_HEAD(&sem->wait_list);
  57. }
  58. /*
  59. * lock for reading
  60. */
  61. static inline void
  62. __down_read (struct rw_semaphore *sem)
  63. {
  64. long result = ia64_fetchadd8_acq((unsigned long *)&sem->count, 1);
  65. if (result < 0)
  66. rwsem_down_read_failed(sem);
  67. }
  68. /*
  69. * lock for writing
  70. */
  71. static inline void
  72. __down_write (struct rw_semaphore *sem)
  73. {
  74. long old, new;
  75. do {
  76. old = sem->count;
  77. new = old + RWSEM_ACTIVE_WRITE_BIAS;
  78. } while (cmpxchg_acq(&sem->count, old, new) != old);
  79. if (old != 0)
  80. rwsem_down_write_failed(sem);
  81. }
  82. /*
  83. * unlock after reading
  84. */
  85. static inline void
  86. __up_read (struct rw_semaphore *sem)
  87. {
  88. long result = ia64_fetchadd8_rel((unsigned long *)&sem->count, -1);
  89. if (result < 0 && (--result & RWSEM_ACTIVE_MASK) == 0)
  90. rwsem_wake(sem);
  91. }
  92. /*
  93. * unlock after writing
  94. */
  95. static inline void
  96. __up_write (struct rw_semaphore *sem)
  97. {
  98. long old, new;
  99. do {
  100. old = sem->count;
  101. new = old - RWSEM_ACTIVE_WRITE_BIAS;
  102. } while (cmpxchg_rel(&sem->count, old, new) != old);
  103. if (new < 0 && (new & RWSEM_ACTIVE_MASK) == 0)
  104. rwsem_wake(sem);
  105. }
  106. /*
  107. * trylock for reading -- returns 1 if successful, 0 if contention
  108. */
  109. static inline int
  110. __down_read_trylock (struct rw_semaphore *sem)
  111. {
  112. long tmp;
  113. while ((tmp = sem->count) >= 0) {
  114. if (tmp == cmpxchg_acq(&sem->count, tmp, tmp+1)) {
  115. return 1;
  116. }
  117. }
  118. return 0;
  119. }
  120. /*
  121. * trylock for writing -- returns 1 if successful, 0 if contention
  122. */
  123. static inline int
  124. __down_write_trylock (struct rw_semaphore *sem)
  125. {
  126. long tmp = cmpxchg_acq(&sem->count, RWSEM_UNLOCKED_VALUE,
  127. RWSEM_ACTIVE_WRITE_BIAS);
  128. return tmp == RWSEM_UNLOCKED_VALUE;
  129. }
  130. /*
  131. * downgrade write lock to read lock
  132. */
  133. static inline void
  134. __downgrade_write (struct rw_semaphore *sem)
  135. {
  136. long old, new;
  137. do {
  138. old = sem->count;
  139. new = old - RWSEM_WAITING_BIAS;
  140. } while (cmpxchg_rel(&sem->count, old, new) != old);
  141. if (old < 0)
  142. rwsem_downgrade_wake(sem);
  143. }
  144. /*
  145. * Implement atomic add functionality. These used to be "inline" functions, but GCC v3.1
  146. * doesn't quite optimize this stuff right and ends up with bad calls to fetchandadd.
  147. */
  148. #define rwsem_atomic_add(delta, sem) atomic64_add(delta, (atomic64_t *)(&(sem)->count))
  149. #define rwsem_atomic_update(delta, sem) atomic64_add_return(delta, (atomic64_t *)(&(sem)->count))
  150. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  151. {
  152. return (sem->count != 0);
  153. }
  154. #endif /* _ASM_IA64_RWSEM_H */