rwlock.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef __LINUX_RWLOCK_H
  2. #define __LINUX_RWLOCK_H
  3. #ifndef __LINUX_SPINLOCK_H
  4. # error "please don't include this file directly"
  5. #endif
  6. /*
  7. * rwlock related methods
  8. *
  9. * split out from spinlock.h
  10. *
  11. * portions Copyright 2005, Red Hat, Inc., Ingo Molnar
  12. * Released under the General Public License (GPL).
  13. */
  14. #ifdef CONFIG_DEBUG_SPINLOCK
  15. extern void __rwlock_init(rwlock_t *lock, const char *name,
  16. struct lock_class_key *key);
  17. # define rwlock_init(lock) \
  18. do { \
  19. static struct lock_class_key __key; \
  20. \
  21. __rwlock_init((lock), #lock, &__key); \
  22. } while (0)
  23. #else
  24. # define rwlock_init(lock) \
  25. do { *(lock) = __RW_LOCK_UNLOCKED(lock); } while (0)
  26. #endif
  27. #ifdef CONFIG_DEBUG_SPINLOCK
  28. extern void do_raw_read_lock(rwlock_t *lock) __acquires(lock);
  29. #define do_raw_read_lock_flags(lock, flags) do_raw_read_lock(lock)
  30. extern int do_raw_read_trylock(rwlock_t *lock);
  31. extern void do_raw_read_unlock(rwlock_t *lock) __releases(lock);
  32. extern void do_raw_write_lock(rwlock_t *lock) __acquires(lock);
  33. #define do_raw_write_lock_flags(lock, flags) do_raw_write_lock(lock)
  34. extern int do_raw_write_trylock(rwlock_t *lock);
  35. extern void do_raw_write_unlock(rwlock_t *lock) __releases(lock);
  36. #else
  37. # define do_raw_read_lock(rwlock) do {__acquire(lock); arch_read_lock(&(rwlock)->raw_lock); } while (0)
  38. # define do_raw_read_lock_flags(lock, flags) \
  39. do {__acquire(lock); arch_read_lock_flags(&(lock)->raw_lock, *(flags)); } while (0)
  40. # define do_raw_read_trylock(rwlock) arch_read_trylock(&(rwlock)->raw_lock)
  41. # define do_raw_read_unlock(rwlock) do {arch_read_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
  42. # define do_raw_write_lock(rwlock) do {__acquire(lock); arch_write_lock(&(rwlock)->raw_lock); } while (0)
  43. # define do_raw_write_lock_flags(lock, flags) \
  44. do {__acquire(lock); arch_write_lock_flags(&(lock)->raw_lock, *(flags)); } while (0)
  45. # define do_raw_write_trylock(rwlock) arch_write_trylock(&(rwlock)->raw_lock)
  46. # define do_raw_write_unlock(rwlock) do {arch_write_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
  47. #endif
  48. #define read_can_lock(rwlock) arch_read_can_lock(&(rwlock)->raw_lock)
  49. #define write_can_lock(rwlock) arch_write_can_lock(&(rwlock)->raw_lock)
  50. /*
  51. * Define the various rw_lock methods. Note we define these
  52. * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
  53. * methods are defined as nops in the case they are not required.
  54. */
  55. #define read_trylock(lock) __cond_lock(lock, _raw_read_trylock(lock))
  56. #define write_trylock(lock) __cond_lock(lock, _raw_write_trylock(lock))
  57. #define write_lock(lock) _raw_write_lock(lock)
  58. #define read_lock(lock) _raw_read_lock(lock)
  59. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  60. #define read_lock_irqsave(lock, flags) \
  61. do { \
  62. typecheck(unsigned long, flags); \
  63. flags = _raw_read_lock_irqsave(lock); \
  64. } while (0)
  65. #define write_lock_irqsave(lock, flags) \
  66. do { \
  67. typecheck(unsigned long, flags); \
  68. flags = _raw_write_lock_irqsave(lock); \
  69. } while (0)
  70. #else
  71. #define read_lock_irqsave(lock, flags) \
  72. do { \
  73. typecheck(unsigned long, flags); \
  74. _raw_read_lock_irqsave(lock, flags); \
  75. } while (0)
  76. #define write_lock_irqsave(lock, flags) \
  77. do { \
  78. typecheck(unsigned long, flags); \
  79. _raw_write_lock_irqsave(lock, flags); \
  80. } while (0)
  81. #endif
  82. #define read_lock_irq(lock) _raw_read_lock_irq(lock)
  83. #define read_lock_bh(lock) _raw_read_lock_bh(lock)
  84. #define write_lock_irq(lock) _raw_write_lock_irq(lock)
  85. #define write_lock_bh(lock) _raw_write_lock_bh(lock)
  86. #define read_unlock(lock) _raw_read_unlock(lock)
  87. #define write_unlock(lock) _raw_write_unlock(lock)
  88. #define read_unlock_irq(lock) _raw_read_unlock_irq(lock)
  89. #define write_unlock_irq(lock) _raw_write_unlock_irq(lock)
  90. #define read_unlock_irqrestore(lock, flags) \
  91. do { \
  92. typecheck(unsigned long, flags); \
  93. _raw_read_unlock_irqrestore(lock, flags); \
  94. } while (0)
  95. #define read_unlock_bh(lock) _raw_read_unlock_bh(lock)
  96. #define write_unlock_irqrestore(lock, flags) \
  97. do { \
  98. typecheck(unsigned long, flags); \
  99. _raw_write_unlock_irqrestore(lock, flags); \
  100. } while (0)
  101. #define write_unlock_bh(lock) _raw_write_unlock_bh(lock)
  102. #define write_trylock_irqsave(lock, flags) \
  103. ({ \
  104. local_irq_save(flags); \
  105. write_trylock(lock) ? \
  106. 1 : ({ local_irq_restore(flags); 0; }); \
  107. })
  108. #endif /* __LINUX_RWLOCK_H */