rwsem.h 5.2 KB

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