rwsem.h 5.1 KB

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