rwsem.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <asm/system.h>
  14. #include <asm/atomic.h>
  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. spinlock_t wait_lock;
  23. struct list_head wait_list;
  24. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  25. struct lockdep_map dep_map;
  26. #endif
  27. };
  28. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  29. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  30. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
  31. extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
  32. /* Include the arch specific part */
  33. #include <asm/rwsem.h>
  34. /* In all implementations count != 0 means locked */
  35. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  36. {
  37. return sem->count != 0;
  38. }
  39. #endif
  40. /* Common initializer macros and functions */
  41. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  42. # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
  43. #else
  44. # define __RWSEM_DEP_MAP_INIT(lockname)
  45. #endif
  46. #define __RWSEM_INITIALIZER(name) \
  47. { RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED(name.wait_lock), \
  48. LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) }
  49. #define DECLARE_RWSEM(name) \
  50. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  51. extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  52. struct lock_class_key *key);
  53. #define init_rwsem(sem) \
  54. do { \
  55. static struct lock_class_key __key; \
  56. \
  57. __init_rwsem((sem), #sem, &__key); \
  58. } while (0)
  59. /*
  60. * lock for reading
  61. */
  62. extern void down_read(struct rw_semaphore *sem);
  63. /*
  64. * trylock for reading -- returns 1 if successful, 0 if contention
  65. */
  66. extern int down_read_trylock(struct rw_semaphore *sem);
  67. /*
  68. * lock for writing
  69. */
  70. extern void down_write(struct rw_semaphore *sem);
  71. /*
  72. * trylock for writing -- returns 1 if successful, 0 if contention
  73. */
  74. extern int down_write_trylock(struct rw_semaphore *sem);
  75. /*
  76. * release a read lock
  77. */
  78. extern void up_read(struct rw_semaphore *sem);
  79. /*
  80. * release a write lock
  81. */
  82. extern void up_write(struct rw_semaphore *sem);
  83. /*
  84. * downgrade write lock to read lock
  85. */
  86. extern void downgrade_write(struct rw_semaphore *sem);
  87. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  88. /*
  89. * nested locking. NOTE: rwsems are not allowed to recurse
  90. * (which occurs if the same task tries to acquire the same
  91. * lock instance multiple times), but multiple locks of the
  92. * same lock class might be taken, if the order of the locks
  93. * is always the same. This ordering rule can be expressed
  94. * to lockdep via the _nested() APIs, but enumerating the
  95. * subclasses that are used. (If the nesting relationship is
  96. * static then another method for expressing nested locking is
  97. * the explicit definition of lock class keys and the use of
  98. * lockdep_set_class() at lock initialization time.
  99. * See Documentation/lockdep-design.txt for more details.)
  100. */
  101. extern void down_read_nested(struct rw_semaphore *sem, int subclass);
  102. extern void down_write_nested(struct rw_semaphore *sem, int subclass);
  103. /*
  104. * Take/release a lock when not the owner will release it.
  105. *
  106. * [ This API should be avoided as much as possible - the
  107. * proper abstraction for this case is completions. ]
  108. */
  109. extern void down_read_non_owner(struct rw_semaphore *sem);
  110. extern void up_read_non_owner(struct rw_semaphore *sem);
  111. #else
  112. # define down_read_nested(sem, subclass) down_read(sem)
  113. # define down_write_nested(sem, subclass) down_write(sem)
  114. # define down_read_non_owner(sem) down_read(sem)
  115. # define up_read_non_owner(sem) up_read(sem)
  116. #endif
  117. #endif /* _LINUX_RWSEM_H */