rwsem.h 3.5 KB

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