kern_lock.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* $OpenBSD: kern_lock.c,v 1.46 2014/09/14 14:17:25 jsg Exp $ */
  2. /*
  3. * Copyright (c) 1995
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code contains ideas from software contributed to Berkeley by
  7. * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
  8. * System project at Carnegie-Mellon University.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
  35. */
  36. #include <sys/param.h>
  37. #include <sys/lock.h>
  38. #include <sys/systm.h>
  39. #include <sys/sched.h>
  40. #ifdef MP_LOCKDEBUG
  41. /* CPU-dependent timing, this needs to be settable from ddb. */
  42. int __mp_lock_spinout = 200000000;
  43. #endif
  44. /*
  45. * Initialize a lock; required before use.
  46. */
  47. void
  48. lockinit(struct lock *lkp, int prio, char *wmesg, int timo, int flags)
  49. {
  50. KASSERT(flags == 0);
  51. memset(lkp, 0, sizeof(struct lock));
  52. rrw_init(&lkp->lk_lck, wmesg);
  53. }
  54. int
  55. lockstatus(struct lock *lkp)
  56. {
  57. switch (rrw_status(&lkp->lk_lck)) {
  58. case RW_WRITE:
  59. return (LK_EXCLUSIVE);
  60. case RW_WRITE_OTHER:
  61. return (LK_EXCLOTHER);
  62. case RW_READ:
  63. return (LK_SHARED);
  64. case 0:
  65. default:
  66. return (0);
  67. }
  68. }
  69. int
  70. lockmgr(struct lock *lkp, u_int flags, void *notused)
  71. {
  72. int rwflags = 0;
  73. KASSERT(!((flags & (LK_SHARED|LK_EXCLUSIVE)) ==
  74. (LK_SHARED|LK_EXCLUSIVE)));
  75. KASSERT(!((flags & (LK_CANRECURSE|LK_RECURSEFAIL)) ==
  76. (LK_CANRECURSE|LK_RECURSEFAIL)));
  77. KASSERT((flags & LK_RELEASE) ||
  78. (flags & (LK_SHARED|LK_EXCLUSIVE|LK_DRAIN)));
  79. if (flags & LK_RELEASE) {
  80. rrw_exit(&lkp->lk_lck);
  81. return (0);
  82. }
  83. if (flags & LK_SHARED)
  84. rwflags |= RW_READ;
  85. if (flags & (LK_EXCLUSIVE|LK_DRAIN))
  86. rwflags |= RW_WRITE;
  87. if (flags & LK_RECURSEFAIL)
  88. rwflags |= RW_RECURSEFAIL;
  89. if (flags & LK_NOWAIT)
  90. rwflags |= RW_NOSLEEP;
  91. return (rrw_enter(&lkp->lk_lck, rwflags));
  92. }
  93. #if defined(MULTIPROCESSOR)
  94. /*
  95. * Functions for manipulating the kernel_lock. We put them here
  96. * so that they show up in profiles.
  97. */
  98. struct __mp_lock kernel_lock;
  99. void
  100. _kernel_lock_init(void)
  101. {
  102. __mp_lock_init(&kernel_lock);
  103. }
  104. /*
  105. * Acquire/release the kernel lock. Intended for use in the scheduler
  106. * and the lower half of the kernel.
  107. */
  108. void
  109. _kernel_lock(void)
  110. {
  111. SCHED_ASSERT_UNLOCKED();
  112. __mp_lock(&kernel_lock);
  113. }
  114. void
  115. _kernel_unlock(void)
  116. {
  117. __mp_unlock(&kernel_lock);
  118. }
  119. int
  120. _kernel_lock_held(void)
  121. {
  122. return (__mp_lock_held(&kernel_lock));
  123. }
  124. #endif /* MULTIPROCESSOR */