rwsem.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <linux/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. raw_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, \
  48. __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  49. LIST_HEAD_INIT((name).wait_list) \
  50. __RWSEM_DEP_MAP_INIT(name) }
  51. #define DECLARE_RWSEM(name) \
  52. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  53. extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  54. struct lock_class_key *key);
  55. #define init_rwsem(sem) \
  56. do { \
  57. static struct lock_class_key __key; \
  58. \
  59. __init_rwsem((sem), #sem, &__key); \
  60. } while (0)
  61. /*
  62. * lock for reading
  63. */
  64. extern void down_read(struct rw_semaphore *sem);
  65. /*
  66. * trylock for reading -- returns 1 if successful, 0 if contention
  67. */
  68. extern int down_read_trylock(struct rw_semaphore *sem);
  69. /*
  70. * lock for writing
  71. */
  72. extern void down_write(struct rw_semaphore *sem);
  73. /*
  74. * trylock for writing -- returns 1 if successful, 0 if contention
  75. */
  76. extern int down_write_trylock(struct rw_semaphore *sem);
  77. /*
  78. * release a read lock
  79. */
  80. extern void up_read(struct rw_semaphore *sem);
  81. /*
  82. * release a write lock
  83. */
  84. extern void up_write(struct rw_semaphore *sem);
  85. /*
  86. * downgrade write lock to read lock
  87. */
  88. extern void downgrade_write(struct rw_semaphore *sem);
  89. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  90. /*
  91. * nested locking. NOTE: rwsems are not allowed to recurse
  92. * (which occurs if the same task tries to acquire the same
  93. * lock instance multiple times), but multiple locks of the
  94. * same lock class might be taken, if the order of the locks
  95. * is always the same. This ordering rule can be expressed
  96. * to lockdep via the _nested() APIs, but enumerating the
  97. * subclasses that are used. (If the nesting relationship is
  98. * static then another method for expressing nested locking is
  99. * the explicit definition of lock class keys and the use of
  100. * lockdep_set_class() at lock initialization time.
  101. * See Documentation/lockdep-design.txt for more details.)
  102. */
  103. extern void down_read_nested(struct rw_semaphore *sem, int subclass);
  104. extern void down_write_nested(struct rw_semaphore *sem, int subclass);
  105. #else
  106. # define down_read_nested(sem, subclass) down_read(sem)
  107. # define down_write_nested(sem, subclass) down_write(sem)
  108. #endif
  109. #endif /* _LINUX_RWSEM_H */