Lock.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/TemporaryChange.h>
  27. #include <Kernel/KSyms.h>
  28. #include <Kernel/Lock.h>
  29. #include <Kernel/Thread.h>
  30. namespace Kernel {
  31. static bool modes_conflict(Lock::Mode mode1, Lock::Mode mode2)
  32. {
  33. if (mode1 == Lock::Mode::Unlocked || mode2 == Lock::Mode::Unlocked)
  34. return false;
  35. if (mode1 == Lock::Mode::Shared && mode2 == Lock::Mode::Shared)
  36. return false;
  37. return true;
  38. }
  39. void Lock::lock(Mode mode)
  40. {
  41. ASSERT(mode != Mode::Unlocked);
  42. if (!are_interrupts_enabled()) {
  43. klog() << "Interrupts disabled when trying to take Lock{" << m_name << "}";
  44. dump_backtrace();
  45. Processor::halt();
  46. }
  47. auto current_thread = Thread::current();
  48. for (;;) {
  49. if (m_lock.exchange(true, AK::memory_order_acq_rel) == false) {
  50. do {
  51. // FIXME: Do not add new readers if writers are queued.
  52. bool modes_dont_conflict = !modes_conflict(m_mode, mode);
  53. bool already_hold_exclusive_lock = m_mode == Mode::Exclusive && m_holder == current_thread;
  54. if (modes_dont_conflict || already_hold_exclusive_lock) {
  55. // We got the lock!
  56. if (!already_hold_exclusive_lock)
  57. m_mode = mode;
  58. m_holder = current_thread;
  59. m_times_locked++;
  60. m_lock.store(false, AK::memory_order_release);
  61. return;
  62. }
  63. } while (current_thread->wait_on(m_queue, m_name, nullptr, &m_lock, m_holder) == Thread::BlockResult::NotBlocked);
  64. } else if (Processor::current().in_critical()) {
  65. // If we're in a critical section and trying to lock, no context
  66. // switch will happen, so yield.
  67. // The assumption is that if we call this from a critical section
  68. // that we DO want to temporarily leave it
  69. u32 prev_flags;
  70. u32 prev_crit = Processor::current().clear_critical(prev_flags, !Processor::current().in_irq());
  71. Scheduler::yield();
  72. // Note, we may now be on a different CPU!
  73. Processor::current().restore_critical(prev_crit, prev_flags);
  74. } else {
  75. // We need to process e.g. smp messages
  76. Processor::wait_check();
  77. }
  78. }
  79. }
  80. void Lock::unlock()
  81. {
  82. auto current_thread = Thread::current();
  83. for (;;) {
  84. if (m_lock.exchange(true, AK::memory_order_acq_rel) == false) {
  85. ASSERT(m_times_locked);
  86. --m_times_locked;
  87. ASSERT(m_mode != Mode::Unlocked);
  88. if (m_mode == Mode::Exclusive)
  89. ASSERT(m_holder == current_thread);
  90. if (m_holder == current_thread && (m_mode == Mode::Shared || m_times_locked == 0))
  91. m_holder = nullptr;
  92. if (m_times_locked > 0) {
  93. m_lock.store(false, AK::memory_order_release);
  94. return;
  95. }
  96. m_mode = Mode::Unlocked;
  97. m_queue.wake_one(&m_lock);
  98. return;
  99. }
  100. // I don't know *who* is using "m_lock", so just yield.
  101. // The assumption is that if we call this from a critical section
  102. // that we DO want to temporarily leave it
  103. u32 prev_flags;
  104. u32 prev_crit = Processor::current().clear_critical(prev_flags, false);
  105. Scheduler::yield();
  106. // Note, we may now be on a different CPU!
  107. Processor::current().restore_critical(prev_crit, prev_flags);
  108. }
  109. }
  110. bool Lock::force_unlock_if_locked()
  111. {
  112. ASSERT(m_mode != Mode::Shared);
  113. ScopedCritical critical;
  114. if (m_holder != Thread::current())
  115. return false;
  116. ASSERT(m_times_locked == 1);
  117. m_holder = nullptr;
  118. m_mode = Mode::Unlocked;
  119. m_times_locked = 0;
  120. m_queue.wake_one();
  121. return true;
  122. }
  123. void Lock::clear_waiters()
  124. {
  125. ASSERT(m_mode != Mode::Shared);
  126. ScopedCritical critical;
  127. m_queue.clear();
  128. }
  129. }