rwsem.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* rwsem.h: R/W semaphores, public interface
  2. *
  3. * Written by David Howells (dhowells@redhat.com).
  4. * Derived from asm-i386/semaphore.h
  5. */
  6. #ifndef _LINUX_RWSEM_H
  7. #define _LINUX_RWSEM_H
  8. #include <linux/linkage.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/list.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/atomic.h>
  14. #include <linux/err.h>
  15. #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
  16. #include <linux/osq_lock.h>
  17. #endif
  18. struct rw_semaphore;
  19. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  20. #include <linux/rwsem-spinlock.h> /* use a generic implementation */
  21. #define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE
  22. #else
  23. /* All arch specific implementations share the same struct */
  24. struct rw_semaphore {
  25. atomic_long_t count;
  26. struct list_head wait_list;
  27. raw_spinlock_t wait_lock;
  28. #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
  29. struct optimistic_spin_queue osq; /* spinner MCS lock */
  30. /*
  31. * Write owner. Used as a speculative check to see
  32. * if the owner is running on the cpu.
  33. */
  34. struct task_struct *owner;
  35. #endif
  36. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  37. struct lockdep_map dep_map;
  38. #endif
  39. };
  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_down_write_failed_killable(struct rw_semaphore *sem);
  43. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
  44. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  45. /* Include the arch specific part */
  46. #include <asm/rwsem.h>
  47. /* In all implementations count != 0 means locked */
  48. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  49. {
  50. return atomic_long_read(&sem->count) != 0;
  51. }
  52. #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
  53. #endif
  54. /* Common initializer macros and functions */
  55. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  56. # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
  57. #else
  58. # define __RWSEM_DEP_MAP_INIT(lockname)
  59. #endif
  60. #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
  61. #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
  62. #else
  63. #define __RWSEM_OPT_INIT(lockname)
  64. #endif
  65. #define __RWSEM_INITIALIZER(name) \
  66. { __RWSEM_INIT_COUNT(name), \
  67. .wait_list = LIST_HEAD_INIT((name).wait_list), \
  68. .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
  69. __RWSEM_OPT_INIT(name) \
  70. __RWSEM_DEP_MAP_INIT(name) }
  71. #define DECLARE_RWSEM(name) \
  72. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  73. extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  74. struct lock_class_key *key);
  75. #define init_rwsem(sem) \
  76. do { \
  77. static struct lock_class_key __key; \
  78. \
  79. __init_rwsem((sem), #sem, &__key); \
  80. } while (0)
  81. /*
  82. * This is the same regardless of which rwsem implementation that is being used.
  83. * It is just a heuristic meant to be called by somebody alreadying holding the
  84. * rwsem to see if somebody from an incompatible type is wanting access to the
  85. * lock.
  86. */
  87. static inline int rwsem_is_contended(struct rw_semaphore *sem)
  88. {
  89. return !list_empty(&sem->wait_list);
  90. }
  91. /*
  92. * lock for reading
  93. */
  94. extern void down_read(struct rw_semaphore *sem);
  95. /*
  96. * trylock for reading -- returns 1 if successful, 0 if contention
  97. */
  98. extern int down_read_trylock(struct rw_semaphore *sem);
  99. /*
  100. * lock for writing
  101. */
  102. extern void down_write(struct rw_semaphore *sem);
  103. extern int __must_check down_write_killable(struct rw_semaphore *sem);
  104. /*
  105. * trylock for writing -- returns 1 if successful, 0 if contention
  106. */
  107. extern int down_write_trylock(struct rw_semaphore *sem);
  108. /*
  109. * release a read lock
  110. */
  111. extern void up_read(struct rw_semaphore *sem);
  112. /*
  113. * release a write lock
  114. */
  115. extern void up_write(struct rw_semaphore *sem);
  116. /*
  117. * downgrade write lock to read lock
  118. */
  119. extern void downgrade_write(struct rw_semaphore *sem);
  120. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  121. /*
  122. * nested locking. NOTE: rwsems are not allowed to recurse
  123. * (which occurs if the same task tries to acquire the same
  124. * lock instance multiple times), but multiple locks of the
  125. * same lock class might be taken, if the order of the locks
  126. * is always the same. This ordering rule can be expressed
  127. * to lockdep via the _nested() APIs, but enumerating the
  128. * subclasses that are used. (If the nesting relationship is
  129. * static then another method for expressing nested locking is
  130. * the explicit definition of lock class keys and the use of
  131. * lockdep_set_class() at lock initialization time.
  132. * See Documentation/locking/lockdep-design.txt for more details.)
  133. */
  134. extern void down_read_nested(struct rw_semaphore *sem, int subclass);
  135. extern void down_write_nested(struct rw_semaphore *sem, int subclass);
  136. extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
  137. extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
  138. # define down_write_nest_lock(sem, nest_lock) \
  139. do { \
  140. typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  141. _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
  142. } while (0);
  143. /*
  144. * Take/release a lock when not the owner will release it.
  145. *
  146. * [ This API should be avoided as much as possible - the
  147. * proper abstraction for this case is completions. ]
  148. */
  149. extern void down_read_non_owner(struct rw_semaphore *sem);
  150. extern void up_read_non_owner(struct rw_semaphore *sem);
  151. #else
  152. # define down_read_nested(sem, subclass) down_read(sem)
  153. # define down_write_nest_lock(sem, nest_lock) down_write(sem)
  154. # define down_write_nested(sem, subclass) down_write(sem)
  155. # define down_write_killable_nested(sem, subclass) down_write_killable(sem)
  156. # define down_read_non_owner(sem) down_read(sem)
  157. # define up_read_non_owner(sem) up_read(sem)
  158. #endif
  159. #endif /* _LINUX_RWSEM_H */