rwsem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/module.h>
  10. #include <linux/rwsem.h>
  11. #include <asm/system.h>
  12. #include <asm/atomic.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. }
  43. EXPORT_SYMBOL(down_write);
  44. /*
  45. * trylock for writing -- returns 1 if successful, 0 if contention
  46. */
  47. int down_write_trylock(struct rw_semaphore *sem)
  48. {
  49. int ret = __down_write_trylock(sem);
  50. if (ret == 1)
  51. rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
  52. return ret;
  53. }
  54. EXPORT_SYMBOL(down_write_trylock);
  55. /*
  56. * release a read lock
  57. */
  58. void up_read(struct rw_semaphore *sem)
  59. {
  60. rwsem_release(&sem->dep_map, 1, _RET_IP_);
  61. __up_read(sem);
  62. }
  63. EXPORT_SYMBOL(up_read);
  64. /*
  65. * release a write lock
  66. */
  67. void up_write(struct rw_semaphore *sem)
  68. {
  69. rwsem_release(&sem->dep_map, 1, _RET_IP_);
  70. __up_write(sem);
  71. }
  72. EXPORT_SYMBOL(up_write);
  73. /*
  74. * downgrade write lock to read lock
  75. */
  76. void downgrade_write(struct rw_semaphore *sem)
  77. {
  78. /*
  79. * lockdep: a downgraded write will live on as a write
  80. * dependency.
  81. */
  82. __downgrade_write(sem);
  83. }
  84. EXPORT_SYMBOL(downgrade_write);
  85. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  86. void down_read_nested(struct rw_semaphore *sem, int subclass)
  87. {
  88. might_sleep();
  89. rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
  90. LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
  91. }
  92. EXPORT_SYMBOL(down_read_nested);
  93. void down_read_non_owner(struct rw_semaphore *sem)
  94. {
  95. might_sleep();
  96. __down_read(sem);
  97. }
  98. EXPORT_SYMBOL(down_read_non_owner);
  99. void down_write_nested(struct rw_semaphore *sem, int subclass)
  100. {
  101. might_sleep();
  102. rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
  103. LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
  104. }
  105. EXPORT_SYMBOL(down_write_nested);
  106. void up_read_non_owner(struct rw_semaphore *sem)
  107. {
  108. __up_read(sem);
  109. }
  110. EXPORT_SYMBOL(up_read_non_owner);
  111. #endif