rwsem.h 4.4 KB

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