rwsem.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* kernel/rwsem.c: R/W semaphores, public implementation
  2. *
  3. * Written by David Howells (dhowells@redhat.com).
  4. * Derived from asm-i386/semaphore.h
  5. */
  6. #include <linux/types.h>
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/export.h>
  10. #include <linux/rwsem.h>
  11. #include <linux/atomic.h>
  12. #include "rwsem.h"
  13. /*
  14. * lock for reading
  15. */
  16. void __sched down_read(struct rw_semaphore *sem)
  17. {
  18. might_sleep();
  19. rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
  20. LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
  21. }
  22. EXPORT_SYMBOL(down_read);
  23. /*
  24. * trylock for reading -- returns 1 if successful, 0 if contention
  25. */
  26. int down_read_trylock(struct rw_semaphore *sem)
  27. {
  28. int ret = __down_read_trylock(sem);
  29. if (ret == 1)
  30. rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
  31. return ret;
  32. }
  33. EXPORT_SYMBOL(down_read_trylock);
  34. /*
  35. * lock for writing
  36. */
  37. void __sched down_write(struct rw_semaphore *sem)
  38. {
  39. might_sleep();
  40. rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
  41. LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
  42. rwsem_set_owner(sem);
  43. }
  44. EXPORT_SYMBOL(down_write);
  45. /*
  46. * trylock for writing -- returns 1 if successful, 0 if contention
  47. */
  48. int down_write_trylock(struct rw_semaphore *sem)
  49. {
  50. int ret = __down_write_trylock(sem);
  51. if (ret == 1) {
  52. rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
  53. rwsem_set_owner(sem);
  54. }
  55. return ret;
  56. }
  57. EXPORT_SYMBOL(down_write_trylock);
  58. /*
  59. * release a read lock
  60. */
  61. void up_read(struct rw_semaphore *sem)
  62. {
  63. rwsem_release(&sem->dep_map, 1, _RET_IP_);
  64. __up_read(sem);
  65. }
  66. EXPORT_SYMBOL(up_read);
  67. /*
  68. * release a write lock
  69. */
  70. void up_write(struct rw_semaphore *sem)
  71. {
  72. rwsem_release(&sem->dep_map, 1, _RET_IP_);
  73. rwsem_clear_owner(sem);
  74. __up_write(sem);
  75. }
  76. EXPORT_SYMBOL(up_write);
  77. /*
  78. * downgrade write lock to read lock
  79. */
  80. void downgrade_write(struct rw_semaphore *sem)
  81. {
  82. /*
  83. * lockdep: a downgraded write will live on as a write
  84. * dependency.
  85. */
  86. rwsem_clear_owner(sem);
  87. __downgrade_write(sem);
  88. }
  89. EXPORT_SYMBOL(downgrade_write);
  90. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  91. void down_read_nested(struct rw_semaphore *sem, int subclass)
  92. {
  93. might_sleep();
  94. rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
  95. LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
  96. }
  97. EXPORT_SYMBOL(down_read_nested);
  98. void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
  99. {
  100. might_sleep();
  101. rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);
  102. LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
  103. rwsem_set_owner(sem);
  104. }
  105. EXPORT_SYMBOL(_down_write_nest_lock);
  106. void down_read_non_owner(struct rw_semaphore *sem)
  107. {
  108. might_sleep();
  109. __down_read(sem);
  110. }
  111. EXPORT_SYMBOL(down_read_non_owner);
  112. void down_write_nested(struct rw_semaphore *sem, int subclass)
  113. {
  114. might_sleep();
  115. rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
  116. LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
  117. rwsem_set_owner(sem);
  118. }
  119. EXPORT_SYMBOL(down_write_nested);
  120. void up_read_non_owner(struct rw_semaphore *sem)
  121. {
  122. __up_read(sem);
  123. }
  124. EXPORT_SYMBOL(up_read_non_owner);
  125. #endif