spinlock.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /* Make sure to build out-of-line versions of spinlock inline functions */
  30. #define SPINLOCK_INLINE /* empty */
  31. #include <types.h>
  32. #include <lib.h>
  33. #include <cpu.h>
  34. #include <spl.h>
  35. #include <spinlock.h>
  36. #include <current.h> /* for curcpu */
  37. /*
  38. * Spinlocks.
  39. */
  40. /*
  41. * Initialize spinlock.
  42. */
  43. void
  44. spinlock_init(struct spinlock *lk)
  45. {
  46. spinlock_data_set(&lk->lk_lock, 0);
  47. lk->lk_holder = NULL;
  48. }
  49. /*
  50. * Clean up spinlock.
  51. */
  52. void
  53. spinlock_cleanup(struct spinlock *lk)
  54. {
  55. KASSERT(lk->lk_holder == NULL);
  56. KASSERT(spinlock_data_get(&lk->lk_lock) == 0);
  57. }
  58. /*
  59. * Get the lock.
  60. *
  61. * First disable interrupts (otherwise, if we get a timer interrupt we
  62. * might come back to this lock and deadlock), then use a machine-level
  63. * atomic operation to wait for the lock to be free.
  64. */
  65. void
  66. spinlock_acquire(struct spinlock *lk)
  67. {
  68. struct cpu *mycpu;
  69. splraise(IPL_NONE, IPL_HIGH);
  70. /* this must work before curcpu initialization */
  71. if (CURCPU_EXISTS()) {
  72. mycpu = curcpu->c_self;
  73. if (lk->lk_holder == mycpu) {
  74. panic("Deadlock on spinlock %p\n", lk);
  75. }
  76. }
  77. else {
  78. mycpu = NULL;
  79. }
  80. while (1) {
  81. /*
  82. * Do test-test-and-set, that is, read first before
  83. * doing test-and-set, to reduce bus contention.
  84. *
  85. * Test-and-set is a machine-level atomic operation
  86. * that writes 1 into the lock word and returns the
  87. * previous value. If that value was 0, the lock was
  88. * previously unheld and we now own it. If it was 1,
  89. * we don't.
  90. */
  91. if (spinlock_data_get(&lk->lk_lock) != 0) {
  92. continue;
  93. }
  94. if (spinlock_data_testandset(&lk->lk_lock) != 0) {
  95. continue;
  96. }
  97. break;
  98. }
  99. lk->lk_holder = mycpu;
  100. }
  101. /*
  102. * Release the lock.
  103. */
  104. void
  105. spinlock_release(struct spinlock *lk)
  106. {
  107. /* this must work before curcpu initialization */
  108. if (CURCPU_EXISTS()) {
  109. KASSERT(lk->lk_holder == curcpu->c_self);
  110. }
  111. lk->lk_holder = NULL;
  112. spinlock_data_set(&lk->lk_lock, 0);
  113. spllower(IPL_HIGH, IPL_NONE);
  114. }
  115. /*
  116. * Check if the current cpu holds the lock.
  117. */
  118. bool
  119. spinlock_do_i_hold(struct spinlock *lk)
  120. {
  121. if (!CURCPU_EXISTS()) {
  122. return true;
  123. }
  124. /* Assume we can read lk_holder atomically enough for this to work */
  125. return (lk->lk_holder == curcpu->c_self);
  126. }