rwsem.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <cl@linux.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 <asm/intrinsics.h>
  26. #define RWSEM_UNLOCKED_VALUE __IA64_UL_CONST(0x0000000000000000)
  27. #define RWSEM_ACTIVE_BIAS (1L)
  28. #define RWSEM_ACTIVE_MASK (0xffffffffL)
  29. #define RWSEM_WAITING_BIAS (-0x100000000L)
  30. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  31. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  32. /*
  33. * lock for reading
  34. */
  35. static inline void
  36. __down_read (struct rw_semaphore *sem)
  37. {
  38. long result = ia64_fetchadd8_acq((unsigned long *)&sem->count.counter, 1);
  39. if (result < 0)
  40. rwsem_down_read_failed(sem);
  41. }
  42. /*
  43. * lock for writing
  44. */
  45. static inline long
  46. ___down_write (struct rw_semaphore *sem)
  47. {
  48. long old, new;
  49. do {
  50. old = atomic_long_read(&sem->count);
  51. new = old + RWSEM_ACTIVE_WRITE_BIAS;
  52. } while (atomic_long_cmpxchg_acquire(&sem->count, old, new) != old);
  53. return old;
  54. }
  55. static inline void
  56. __down_write (struct rw_semaphore *sem)
  57. {
  58. if (___down_write(sem))
  59. rwsem_down_write_failed(sem);
  60. }
  61. static inline int
  62. __down_write_killable (struct rw_semaphore *sem)
  63. {
  64. if (___down_write(sem))
  65. if (IS_ERR(rwsem_down_write_failed_killable(sem)))
  66. return -EINTR;
  67. return 0;
  68. }
  69. /*
  70. * unlock after reading
  71. */
  72. static inline void
  73. __up_read (struct rw_semaphore *sem)
  74. {
  75. long result = ia64_fetchadd8_rel((unsigned long *)&sem->count.counter, -1);
  76. if (result < 0 && (--result & RWSEM_ACTIVE_MASK) == 0)
  77. rwsem_wake(sem);
  78. }
  79. /*
  80. * unlock after writing
  81. */
  82. static inline void
  83. __up_write (struct rw_semaphore *sem)
  84. {
  85. long old, new;
  86. do {
  87. old = atomic_long_read(&sem->count);
  88. new = old - RWSEM_ACTIVE_WRITE_BIAS;
  89. } while (atomic_long_cmpxchg_release(&sem->count, old, new) != old);
  90. if (new < 0 && (new & RWSEM_ACTIVE_MASK) == 0)
  91. rwsem_wake(sem);
  92. }
  93. /*
  94. * trylock for reading -- returns 1 if successful, 0 if contention
  95. */
  96. static inline int
  97. __down_read_trylock (struct rw_semaphore *sem)
  98. {
  99. long tmp;
  100. while ((tmp = atomic_long_read(&sem->count)) >= 0) {
  101. if (tmp == atomic_long_cmpxchg_acquire(&sem->count, tmp, tmp+1)) {
  102. return 1;
  103. }
  104. }
  105. return 0;
  106. }
  107. /*
  108. * trylock for writing -- returns 1 if successful, 0 if contention
  109. */
  110. static inline int
  111. __down_write_trylock (struct rw_semaphore *sem)
  112. {
  113. long tmp = atomic_long_cmpxchg_acquire(&sem->count,
  114. RWSEM_UNLOCKED_VALUE, RWSEM_ACTIVE_WRITE_BIAS);
  115. return tmp == RWSEM_UNLOCKED_VALUE;
  116. }
  117. /*
  118. * downgrade write lock to read lock
  119. */
  120. static inline void
  121. __downgrade_write (struct rw_semaphore *sem)
  122. {
  123. long old, new;
  124. do {
  125. old = atomic_long_read(&sem->count);
  126. new = old - RWSEM_WAITING_BIAS;
  127. } while (atomic_long_cmpxchg_release(&sem->count, old, new) != old);
  128. if (old < 0)
  129. rwsem_downgrade_wake(sem);
  130. }
  131. #endif /* _ASM_IA64_RWSEM_H */