rwsem.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * include/asm-xtensa/rwsem.h
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Largely copied from include/asm-ppc/rwsem.h
  9. *
  10. * Copyright (C) 2001 - 2005 Tensilica Inc.
  11. */
  12. #ifndef _XTENSA_RWSEM_H
  13. #define _XTENSA_RWSEM_H
  14. #ifndef _LINUX_RWSEM_H
  15. #error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead."
  16. #endif
  17. #include <linux/list.h>
  18. #include <linux/spinlock.h>
  19. #include <asm/atomic.h>
  20. #include <asm/system.h>
  21. /*
  22. * the semaphore definition
  23. */
  24. struct rw_semaphore {
  25. signed long count;
  26. #define RWSEM_UNLOCKED_VALUE 0x00000000
  27. #define RWSEM_ACTIVE_BIAS 0x00000001
  28. #define RWSEM_ACTIVE_MASK 0x0000ffff
  29. #define RWSEM_WAITING_BIAS (-0x00010000)
  30. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  31. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  32. spinlock_t wait_lock;
  33. struct list_head wait_list;
  34. };
  35. #define __RWSEM_INITIALIZER(name) \
  36. { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
  37. LIST_HEAD_INIT((name).wait_list) }
  38. #define DECLARE_RWSEM(name) \
  39. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  40. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  41. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  42. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
  43. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  44. static inline void init_rwsem(struct rw_semaphore *sem)
  45. {
  46. sem->count = RWSEM_UNLOCKED_VALUE;
  47. spin_lock_init(&sem->wait_lock);
  48. INIT_LIST_HEAD(&sem->wait_list);
  49. }
  50. /*
  51. * lock for reading
  52. */
  53. static inline void __down_read(struct rw_semaphore *sem)
  54. {
  55. if (atomic_add_return(1,(atomic_t *)(&sem->count)) > 0)
  56. smp_wmb();
  57. else
  58. rwsem_down_read_failed(sem);
  59. }
  60. static inline int __down_read_trylock(struct rw_semaphore *sem)
  61. {
  62. int tmp;
  63. while ((tmp = sem->count) >= 0) {
  64. if (tmp == cmpxchg(&sem->count, tmp,
  65. tmp + RWSEM_ACTIVE_READ_BIAS)) {
  66. smp_wmb();
  67. return 1;
  68. }
  69. }
  70. return 0;
  71. }
  72. /*
  73. * lock for writing
  74. */
  75. static inline void __down_write(struct rw_semaphore *sem)
  76. {
  77. int tmp;
  78. tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS,
  79. (atomic_t *)(&sem->count));
  80. if (tmp == RWSEM_ACTIVE_WRITE_BIAS)
  81. smp_wmb();
  82. else
  83. rwsem_down_write_failed(sem);
  84. }
  85. static inline int __down_write_trylock(struct rw_semaphore *sem)
  86. {
  87. int tmp;
  88. tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
  89. RWSEM_ACTIVE_WRITE_BIAS);
  90. smp_wmb();
  91. return tmp == RWSEM_UNLOCKED_VALUE;
  92. }
  93. /*
  94. * unlock after reading
  95. */
  96. static inline void __up_read(struct rw_semaphore *sem)
  97. {
  98. int tmp;
  99. smp_wmb();
  100. tmp = atomic_sub_return(1,(atomic_t *)(&sem->count));
  101. if (tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)
  102. rwsem_wake(sem);
  103. }
  104. /*
  105. * unlock after writing
  106. */
  107. static inline void __up_write(struct rw_semaphore *sem)
  108. {
  109. smp_wmb();
  110. if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
  111. (atomic_t *)(&sem->count)) < 0)
  112. rwsem_wake(sem);
  113. }
  114. /*
  115. * implement atomic add functionality
  116. */
  117. static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
  118. {
  119. atomic_add(delta, (atomic_t *)(&sem->count));
  120. }
  121. /*
  122. * downgrade write lock to read lock
  123. */
  124. static inline void __downgrade_write(struct rw_semaphore *sem)
  125. {
  126. int tmp;
  127. smp_wmb();
  128. tmp = atomic_add_return(-RWSEM_WAITING_BIAS, (atomic_t *)(&sem->count));
  129. if (tmp < 0)
  130. rwsem_downgrade_wake(sem);
  131. }
  132. /*
  133. * implement exchange and add functionality
  134. */
  135. static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
  136. {
  137. smp_mb();
  138. return atomic_add_return(delta, (atomic_t *)(&sem->count));
  139. }
  140. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  141. {
  142. return (sem->count != 0);
  143. }
  144. #endif /* _XTENSA_RWSEM_H */