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