2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-01-11 11:52:18 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-12-30 20:46:17 +03:00
|
|
|
#include <AK/QuickSort.h>
|
2020-08-01 23:37:40 +03:00
|
|
|
#include <AK/ScopeGuard.h>
|
2019-02-06 17:05:47 +03:00
|
|
|
#include <AK/TemporaryChange.h>
|
2020-08-03 18:43:19 +03:00
|
|
|
#include <AK/Time.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2021-02-14 11:30:31 +03:00
|
|
|
#include <Kernel/Panic.h>
|
2021-01-11 11:52:18 +03:00
|
|
|
#include <Kernel/PerformanceEventBuffer.h>
|
2019-06-07 20:29:34 +03:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/RTC.h>
|
|
|
|
#include <Kernel/Scheduler.h>
|
Kernel: Introduce the new Time management subsystem
This new subsystem includes better abstractions of how time will be
handled in the OS. We take advantage of the existing RTC timer to aid
in keeping time synchronized. This is standing in contrast to how we
handled time-keeping in the kernel, where the PIT was responsible for
that function in addition to update the scheduler about ticks.
With that new advantage, we can easily change the ticking dynamically
and still keep the time synchronized.
In the process context, we no longer use a fixed declaration of
TICKS_PER_SECOND, but we call the TimeManagement singleton class to
provide us the right value. This allows us to use dynamic ticking in
the future, a feature known as tickless kernel.
The scheduler no longer does by himself the calculation of real time
(Unix time), and just calls the TimeManagment singleton class to provide
the value.
Also, we can use 2 new boot arguments:
- the "time" boot argument accpets either the value "modern", or
"legacy". If "modern" is specified, the time management subsystem will
try to setup HPET. Otherwise, for "legacy" value, the time subsystem
will revert to use the PIT & RTC, leaving HPET disabled.
If this boot argument is not specified, the default pattern is to try
to setup HPET.
- the "hpet" boot argumet accepts either the value "periodic" or
"nonperiodic". If "periodic" is specified, the HPET will scan for
periodic timers, and will assert if none are found. If only one is
found, that timer will be assigned for the time-keeping task. If more
than one is found, both time-keeping task & scheduler-ticking task
will be assigned to periodic timers.
If this boot argument is not specified, the default pattern is to try
to scan for HPET periodic timers. This boot argument has no effect if
HPET is disabled.
In hardware context, PIT & RealTimeClock classes are merely inheriting
from the HardwareTimer class, and they allow to use the old i8254 (PIT)
and RTC devices, managing them via IO ports. By default, the RTC will be
programmed to a frequency of 1024Hz. The PIT will be programmed to a
frequency close to 1000Hz.
About HPET, depending if we need to scan for periodic timers or not,
we try to set a frequency close to 1000Hz for the time-keeping timer
and scheduler-ticking timer. Also, if possible, we try to enable the
Legacy replacement feature of the HPET. This feature if exists,
instructs the chipset to disconnect both i8254 (PIT) and RTC.
This behavior is observable on QEMU, and was verified against the source
code:
https://github.com/qemu/qemu/commit/ce967e2f33861b0e17753f97fa4527b5943c94b6
The HPETComparator class is inheriting from HardwareTimer class, and is
responsible for an individual HPET comparator, which is essentially a
timer. Therefore, it needs to call the singleton HPET class to perform
HPET-related operations.
The new abstraction of Hardware timers brings an opportunity of more new
features in the foreseeable future. For example, we can change the
callback function of each hardware timer, thus it makes it possible to
swap missions between hardware timers, or to allow to use a hardware
timer for other temporary missions (e.g. calibrating the LAPIC timer,
measuring the CPU frequency, etc).
2020-03-09 18:03:27 +03:00
|
|
|
#include <Kernel/Time/TimeManagement.h>
|
2019-12-27 03:58:28 +03:00
|
|
|
#include <Kernel/TimerQueue.h>
|
2018-11-08 00:15:02 +03:00
|
|
|
|
2021-01-27 08:45:30 +03:00
|
|
|
// Remove this once SMP is stable and can be enabled by default
|
|
|
|
#define SCHEDULE_ON_ALL_PROCESSORS 0
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-02 19:19:35 +03:00
|
|
|
extern bool g_profiling_all_threads;
|
|
|
|
extern PerformanceEventBuffer* g_global_perf_events;
|
|
|
|
|
2020-08-01 23:37:40 +03:00
|
|
|
class SchedulerPerProcessorData {
|
|
|
|
AK_MAKE_NONCOPYABLE(SchedulerPerProcessorData);
|
|
|
|
AK_MAKE_NONMOVABLE(SchedulerPerProcessorData);
|
|
|
|
|
|
|
|
public:
|
|
|
|
SchedulerPerProcessorData() = default;
|
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
WeakPtr<Thread> m_pending_beneficiary;
|
2020-09-15 22:53:15 +03:00
|
|
|
const char* m_pending_donate_reason { nullptr };
|
2020-08-01 23:37:40 +03:00
|
|
|
bool m_in_scheduler { true };
|
|
|
|
};
|
|
|
|
|
2020-06-29 00:34:31 +03:00
|
|
|
RecursiveSpinLock g_scheduler_lock;
|
2019-07-19 18:21:13 +03:00
|
|
|
|
2019-12-30 20:46:17 +03:00
|
|
|
static u32 time_slice_for(const Thread& thread)
|
2019-02-07 14:21:17 +03:00
|
|
|
{
|
2020-12-04 08:12:50 +03:00
|
|
|
// One time slice unit == 4ms (assuming 250 ticks/second)
|
2020-06-29 00:34:31 +03:00
|
|
|
if (&thread == Processor::current().idle_thread())
|
2019-04-20 16:58:45 +03:00
|
|
|
return 1;
|
2020-12-04 08:12:50 +03:00
|
|
|
return 2;
|
2019-02-07 14:21:17 +03:00
|
|
|
}
|
2018-11-08 00:15:02 +03:00
|
|
|
|
2021-02-14 19:39:41 +03:00
|
|
|
READONLY_AFTER_INIT Thread* g_finalizer;
|
|
|
|
READONLY_AFTER_INIT WaitQueue* g_finalizer_wait_queue;
|
2020-07-30 22:46:06 +03:00
|
|
|
Atomic<bool> g_finalizer_has_work { false };
|
2021-02-14 19:39:41 +03:00
|
|
|
READONLY_AFTER_INIT static Process* s_colonel_process;
|
2018-11-08 00:15:02 +03:00
|
|
|
|
2021-01-23 02:56:08 +03:00
|
|
|
struct ThreadReadyQueue {
|
2021-04-16 15:03:24 +03:00
|
|
|
IntrusiveList<Thread, RawPtr<Thread>, &Thread::m_ready_queue_node> thread_list;
|
2021-01-23 02:56:08 +03:00
|
|
|
};
|
|
|
|
static SpinLock<u8> g_ready_queues_lock;
|
|
|
|
static u32 g_ready_queues_mask;
|
|
|
|
static constexpr u32 g_ready_queue_buckets = sizeof(g_ready_queues_mask) * 8;
|
2021-02-14 19:39:41 +03:00
|
|
|
READONLY_AFTER_INIT static ThreadReadyQueue* g_ready_queues; // g_ready_queue_buckets entries
|
2021-01-23 02:56:08 +03:00
|
|
|
|
|
|
|
static inline u32 thread_priority_to_priority_index(u32 thread_priority)
|
|
|
|
{
|
|
|
|
// Converts the priority in the range of THREAD_PRIORITY_MIN...THREAD_PRIORITY_MAX
|
|
|
|
// to a index into g_ready_queues where 0 is the highest priority bucket
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(thread_priority >= THREAD_PRIORITY_MIN && thread_priority <= THREAD_PRIORITY_MAX);
|
2021-01-23 02:56:08 +03:00
|
|
|
constexpr u32 thread_priority_count = THREAD_PRIORITY_MAX - THREAD_PRIORITY_MIN + 1;
|
|
|
|
static_assert(thread_priority_count > 0);
|
|
|
|
auto priority_bucket = ((thread_priority_count - (thread_priority - THREAD_PRIORITY_MIN)) / thread_priority_count) * (g_ready_queue_buckets - 1);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(priority_bucket < g_ready_queue_buckets);
|
2021-01-23 02:56:08 +03:00
|
|
|
return priority_bucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread& Scheduler::pull_next_runnable_thread()
|
|
|
|
{
|
|
|
|
auto affinity_mask = 1u << Processor::current().id();
|
|
|
|
|
|
|
|
ScopedSpinLock lock(g_ready_queues_lock);
|
|
|
|
auto priority_mask = g_ready_queues_mask;
|
|
|
|
while (priority_mask != 0) {
|
|
|
|
auto priority = __builtin_ffsl(priority_mask);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(priority > 0);
|
2021-01-23 02:56:08 +03:00
|
|
|
auto& ready_queue = g_ready_queues[--priority];
|
|
|
|
for (auto& thread : ready_queue.thread_list) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(thread.m_runnable_priority == (int)priority);
|
2021-01-23 02:56:08 +03:00
|
|
|
if (thread.is_active())
|
|
|
|
continue;
|
|
|
|
if (!(thread.affinity() & affinity_mask))
|
|
|
|
continue;
|
|
|
|
thread.m_runnable_priority = -1;
|
|
|
|
ready_queue.thread_list.remove(thread);
|
|
|
|
if (ready_queue.thread_list.is_empty())
|
|
|
|
g_ready_queues_mask &= ~(1u << priority);
|
|
|
|
// Mark it as active because we are using this thread. This is similar
|
|
|
|
// to comparing it with Processor::current_thread, but when there are
|
|
|
|
// multiple processors there's no easy way to check whether the thread
|
|
|
|
// is actually still needed. This prevents accidental finalization when
|
|
|
|
// a thread is no longer in Running state, but running on another core.
|
|
|
|
|
|
|
|
// We need to mark it active here so that this thread won't be
|
|
|
|
// scheduled on another core if it were to be queued before actually
|
|
|
|
// switching to it.
|
|
|
|
// FIXME: Figure out a better way maybe?
|
|
|
|
thread.set_active(true);
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
priority_mask &= ~(1u << priority);
|
|
|
|
}
|
|
|
|
return *Processor::current().idle_thread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Scheduler::dequeue_runnable_thread(Thread& thread, bool check_affinity)
|
|
|
|
{
|
|
|
|
if (&thread == Processor::current().idle_thread())
|
|
|
|
return true;
|
|
|
|
ScopedSpinLock lock(g_ready_queues_lock);
|
|
|
|
auto priority = thread.m_runnable_priority;
|
|
|
|
if (priority < 0) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!thread.m_ready_queue_node.is_in_list());
|
2021-01-23 02:56:08 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_affinity && !(thread.affinity() & (1 << Processor::current().id())))
|
|
|
|
return false;
|
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(g_ready_queues_mask & (1u << priority));
|
2021-01-23 02:56:08 +03:00
|
|
|
auto& ready_queue = g_ready_queues[priority];
|
|
|
|
thread.m_runnable_priority = -1;
|
|
|
|
ready_queue.thread_list.remove(thread);
|
|
|
|
if (ready_queue.thread_list.is_empty())
|
|
|
|
g_ready_queues_mask &= ~(1u << priority);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::queue_runnable_thread(Thread& thread)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(g_scheduler_lock.own_lock());
|
2021-01-23 02:56:08 +03:00
|
|
|
if (&thread == Processor::current().idle_thread())
|
|
|
|
return;
|
2021-01-28 10:25:05 +03:00
|
|
|
auto priority = thread_priority_to_priority_index(thread.priority());
|
2021-01-23 02:56:08 +03:00
|
|
|
|
|
|
|
ScopedSpinLock lock(g_ready_queues_lock);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(thread.m_runnable_priority < 0);
|
2021-01-23 02:56:08 +03:00
|
|
|
thread.m_runnable_priority = (int)priority;
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!thread.m_ready_queue_node.is_in_list());
|
2021-01-23 02:56:08 +03:00
|
|
|
auto& ready_queue = g_ready_queues[priority];
|
|
|
|
bool was_empty = ready_queue.thread_list.is_empty();
|
|
|
|
ready_queue.thread_list.append(thread);
|
|
|
|
if (was_empty)
|
|
|
|
g_ready_queues_mask |= (1u << priority);
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:41:50 +03:00
|
|
|
UNMAP_AFTER_INIT void Scheduler::start()
|
2018-11-08 00:15:02 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
2020-07-30 22:46:06 +03:00
|
|
|
|
2020-07-05 23:32:07 +03:00
|
|
|
// We need to acquire our scheduler lock, which will be released
|
|
|
|
// by the idle thread once control transferred there
|
|
|
|
g_scheduler_lock.lock();
|
|
|
|
|
2020-06-29 00:34:31 +03:00
|
|
|
auto& processor = Processor::current();
|
2020-08-01 23:37:40 +03:00
|
|
|
processor.set_scheduler_data(*new SchedulerPerProcessorData());
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(processor.is_initialized());
|
2020-06-29 00:34:31 +03:00
|
|
|
auto& idle_thread = *processor.idle_thread();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(processor.current_thread() == &idle_thread);
|
|
|
|
VERIFY(processor.idle_thread() == &idle_thread);
|
2020-06-29 00:34:31 +03:00
|
|
|
idle_thread.set_ticks_left(time_slice_for(idle_thread));
|
|
|
|
idle_thread.did_schedule();
|
|
|
|
idle_thread.set_initialized(true);
|
2020-07-03 14:19:50 +03:00
|
|
|
processor.init_context(idle_thread, false);
|
2020-06-29 00:34:31 +03:00
|
|
|
idle_thread.set_state(Thread::Running);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(idle_thread.affinity() == (1u << processor.get_id()));
|
2020-06-29 00:34:31 +03:00
|
|
|
processor.initialize_context_switching(idle_thread);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-27 22:42:28 +03:00
|
|
|
}
|
2018-11-08 00:15:02 +03:00
|
|
|
|
2020-06-27 22:42:28 +03:00
|
|
|
bool Scheduler::pick_next()
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
2018-11-08 00:15:02 +03:00
|
|
|
|
2020-06-29 00:34:31 +03:00
|
|
|
auto current_thread = Thread::current();
|
2019-03-13 15:13:23 +03:00
|
|
|
|
2020-08-01 23:37:40 +03:00
|
|
|
// Set the m_in_scheduler flag before acquiring the spinlock. This
|
|
|
|
// prevents a recursive call into Scheduler::invoke_async upon
|
|
|
|
// leaving the scheduler lock.
|
|
|
|
ScopedCritical critical;
|
2020-09-15 22:53:15 +03:00
|
|
|
auto& scheduler_data = Processor::current().get_scheduler_data();
|
|
|
|
scheduler_data.m_in_scheduler = true;
|
2020-08-01 23:37:40 +03:00
|
|
|
ScopeGuard guard(
|
|
|
|
[]() {
|
|
|
|
// We may be on a different processor after we got switched
|
|
|
|
// back to this thread!
|
|
|
|
auto& scheduler_data = Processor::current().get_scheduler_data();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(scheduler_data.m_in_scheduler);
|
2020-08-01 23:37:40 +03:00
|
|
|
scheduler_data.m_in_scheduler = false;
|
|
|
|
});
|
|
|
|
|
2020-06-29 00:34:31 +03:00
|
|
|
ScopedSpinLock lock(g_scheduler_lock);
|
|
|
|
|
2020-08-10 23:05:24 +03:00
|
|
|
if (current_thread->should_die() && current_thread->state() == Thread::Running) {
|
|
|
|
// Rather than immediately killing threads, yanking the kernel stack
|
|
|
|
// away from them (which can lead to e.g. reference leaks), we always
|
|
|
|
// allow Thread::wait_on to return. This allows the kernel stack to
|
|
|
|
// clean up and eventually we'll get here shortly before transitioning
|
|
|
|
// back to user mode (from Processor::exit_trap). At this point we
|
|
|
|
// no longer want to schedule this thread. We can't wait until
|
|
|
|
// Scheduler::enter_current because we don't want to allow it to
|
|
|
|
// transition back to user mode.
|
2021-01-13 00:30:52 +03:00
|
|
|
|
2021-01-24 01:59:27 +03:00
|
|
|
if constexpr (SCHEDULER_DEBUG)
|
2021-01-27 06:44:01 +03:00
|
|
|
dbgln("Scheduler[{}]: Thread {} is dying", Processor::id(), *current_thread);
|
2021-01-13 00:30:52 +03:00
|
|
|
|
2020-08-10 23:05:24 +03:00
|
|
|
current_thread->set_state(Thread::Dying);
|
|
|
|
}
|
|
|
|
|
2021-01-24 01:59:27 +03:00
|
|
|
if constexpr (SCHEDULER_RUNNABLE_DEBUG) {
|
2021-01-17 12:02:26 +03:00
|
|
|
dbgln("Scheduler thread list for processor {}:", Processor::id());
|
2021-01-28 08:58:24 +03:00
|
|
|
Thread::for_each([&](Thread& thread) -> IterationDecision {
|
|
|
|
switch (thread.state()) {
|
|
|
|
case Thread::Dying:
|
2021-01-13 00:30:52 +03:00
|
|
|
dbgln(" {:12} {} @ {:04x}:{:08x} Finalizable: {}",
|
|
|
|
thread.state_string(),
|
|
|
|
thread,
|
|
|
|
thread.tss().cs,
|
|
|
|
thread.tss().eip,
|
|
|
|
thread.is_finalizable());
|
2021-01-28 08:58:24 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dbgln(" {:12} Pr:{:2} {} @ {:04x}:{:08x}",
|
2021-01-13 00:30:52 +03:00
|
|
|
thread.state_string(),
|
2021-01-28 08:58:24 +03:00
|
|
|
thread.priority(),
|
2021-01-13 00:30:52 +03:00
|
|
|
thread,
|
|
|
|
thread.tss().cs,
|
|
|
|
thread.tss().eip);
|
2021-01-28 08:58:24 +03:00
|
|
|
break;
|
2021-01-13 00:30:52 +03:00
|
|
|
}
|
2019-05-20 04:44:45 +03:00
|
|
|
|
2021-01-13 00:30:52 +03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
2018-11-08 00:15:02 +03:00
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
auto pending_beneficiary = scheduler_data.m_pending_beneficiary.strong_ref();
|
2021-01-23 02:56:08 +03:00
|
|
|
if (pending_beneficiary && dequeue_runnable_thread(*pending_beneficiary, true)) {
|
|
|
|
// The thread we're supposed to donate to still exists and we can
|
2020-09-15 22:53:15 +03:00
|
|
|
const char* reason = scheduler_data.m_pending_donate_reason;
|
|
|
|
scheduler_data.m_pending_beneficiary = nullptr;
|
|
|
|
scheduler_data.m_pending_donate_reason = nullptr;
|
|
|
|
|
|
|
|
// We need to leave our first critical section before switching context,
|
|
|
|
// but since we're still holding the scheduler lock we're still in a critical section
|
|
|
|
critical.leave();
|
|
|
|
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(SCHEDULER_DEBUG, "Processing pending donate to {} reason={}", *pending_beneficiary, reason);
|
2021-01-23 02:56:08 +03:00
|
|
|
return donate_to_and_switch(pending_beneficiary.ptr(), reason);
|
2020-09-15 22:53:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Either we're not donating or the beneficiary disappeared.
|
|
|
|
// Either way clear any pending information
|
|
|
|
scheduler_data.m_pending_beneficiary = nullptr;
|
|
|
|
scheduler_data.m_pending_donate_reason = nullptr;
|
|
|
|
|
2021-01-23 02:56:08 +03:00
|
|
|
auto& thread_to_schedule = pull_next_runnable_thread();
|
2021-01-24 01:59:27 +03:00
|
|
|
if constexpr (SCHEDULER_DEBUG) {
|
2021-01-13 00:30:52 +03:00
|
|
|
dbgln("Scheduler[{}]: Switch to {} @ {:04x}:{:08x}",
|
2021-01-27 06:44:01 +03:00
|
|
|
Processor::id(),
|
2021-01-23 02:56:08 +03:00
|
|
|
thread_to_schedule,
|
|
|
|
thread_to_schedule.tss().cs, thread_to_schedule.tss().eip);
|
2021-01-13 00:30:52 +03:00
|
|
|
}
|
2019-12-30 20:46:17 +03:00
|
|
|
|
2020-08-01 23:37:40 +03:00
|
|
|
// We need to leave our first critical section before switching context,
|
|
|
|
// but since we're still holding the scheduler lock we're still in a critical section
|
|
|
|
critical.leave();
|
|
|
|
|
2021-01-23 02:56:08 +03:00
|
|
|
thread_to_schedule.set_ticks_left(time_slice_for(thread_to_schedule));
|
|
|
|
return context_switch(&thread_to_schedule);
|
2018-11-08 00:15:02 +03:00
|
|
|
}
|
|
|
|
|
2020-06-27 22:42:28 +03:00
|
|
|
bool Scheduler::yield()
|
|
|
|
{
|
2020-07-03 14:19:50 +03:00
|
|
|
InterruptDisabler disabler;
|
2020-06-29 00:34:31 +03:00
|
|
|
auto& proc = Processor::current();
|
2020-09-15 22:53:15 +03:00
|
|
|
auto& scheduler_data = proc.get_scheduler_data();
|
|
|
|
|
|
|
|
// Clear any pending beneficiary
|
|
|
|
scheduler_data.m_pending_beneficiary = nullptr;
|
|
|
|
scheduler_data.m_pending_donate_reason = nullptr;
|
2020-08-01 23:37:40 +03:00
|
|
|
|
2020-06-29 00:34:31 +03:00
|
|
|
auto current_thread = Thread::current();
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(SCHEDULER_DEBUG, "Scheduler[{}]: yielding thread {} in_irq={}", proc.get_id(), *current_thread, proc.in_irq());
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(current_thread != nullptr);
|
2020-07-03 14:19:50 +03:00
|
|
|
if (proc.in_irq() || proc.in_critical()) {
|
|
|
|
// If we're handling an IRQ we can't switch context, or we're in
|
|
|
|
// a critical section where we don't want to switch contexts, then
|
|
|
|
// delay until exiting the trap or critical section
|
2020-06-29 00:34:31 +03:00
|
|
|
proc.invoke_scheduler_async();
|
2020-07-03 14:19:50 +03:00
|
|
|
return false;
|
2020-08-01 23:37:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Scheduler::pick_next())
|
2020-06-27 22:42:28 +03:00
|
|
|
return false;
|
2021-01-13 00:30:52 +03:00
|
|
|
|
2021-01-24 01:59:27 +03:00
|
|
|
if constexpr (SCHEDULER_DEBUG)
|
2021-01-27 06:44:01 +03:00
|
|
|
dbgln("Scheduler[{}]: yield returns to thread {} in_irq={}", Processor::id(), *current_thread, Processor::current().in_irq());
|
2020-06-27 22:42:28 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-21 02:09:48 +03:00
|
|
|
bool Scheduler::donate_to_and_switch(Thread* beneficiary, [[maybe_unused]] const char* reason)
|
2020-09-15 22:53:15 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(g_scheduler_lock.own_lock());
|
2020-09-15 22:53:15 +03:00
|
|
|
|
|
|
|
auto& proc = Processor::current();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(proc.in_critical() == 1);
|
2020-09-15 22:53:15 +03:00
|
|
|
|
|
|
|
unsigned ticks_left = Thread::current()->ticks_left();
|
|
|
|
if (!beneficiary || beneficiary->state() != Thread::Runnable || ticks_left <= 1)
|
|
|
|
return Scheduler::yield();
|
|
|
|
|
|
|
|
unsigned ticks_to_donate = min(ticks_left - 1, time_slice_for(*beneficiary));
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(SCHEDULER_DEBUG, "Scheduler[{}]: Donating {} ticks to {}, reason={}", proc.get_id(), ticks_to_donate, *beneficiary, reason);
|
2020-09-15 22:53:15 +03:00
|
|
|
beneficiary->set_ticks_left(ticks_to_donate);
|
|
|
|
|
|
|
|
return Scheduler::context_switch(beneficiary);
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
bool Scheduler::donate_to(RefPtr<Thread>& beneficiary, const char* reason)
|
2019-02-07 13:12:23 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(beneficiary);
|
2020-08-08 18:32:34 +03:00
|
|
|
|
2020-09-15 22:53:15 +03:00
|
|
|
if (beneficiary == Thread::current())
|
|
|
|
return Scheduler::yield();
|
|
|
|
|
2020-08-01 23:37:40 +03:00
|
|
|
// Set the m_in_scheduler flag before acquiring the spinlock. This
|
|
|
|
// prevents a recursive call into Scheduler::invoke_async upon
|
|
|
|
// leaving the scheduler lock.
|
|
|
|
ScopedCritical critical;
|
2020-07-03 14:19:50 +03:00
|
|
|
auto& proc = Processor::current();
|
2020-09-15 22:53:15 +03:00
|
|
|
auto& scheduler_data = proc.get_scheduler_data();
|
|
|
|
scheduler_data.m_in_scheduler = true;
|
2020-08-01 23:37:40 +03:00
|
|
|
ScopeGuard guard(
|
|
|
|
[]() {
|
|
|
|
// We may be on a different processor after we got switched
|
|
|
|
// back to this thread!
|
|
|
|
auto& scheduler_data = Processor::current().get_scheduler_data();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(scheduler_data.m_in_scheduler);
|
2020-08-01 23:37:40 +03:00
|
|
|
scheduler_data.m_in_scheduler = false;
|
|
|
|
});
|
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!proc.in_irq());
|
2019-04-17 13:41:51 +03:00
|
|
|
|
2020-09-15 22:53:15 +03:00
|
|
|
if (proc.in_critical() > 1) {
|
2020-11-30 03:09:14 +03:00
|
|
|
scheduler_data.m_pending_beneficiary = beneficiary; // Save the beneficiary
|
2020-09-15 22:53:15 +03:00
|
|
|
scheduler_data.m_pending_donate_reason = reason;
|
2020-07-03 14:19:50 +03:00
|
|
|
proc.invoke_scheduler_async();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-15 22:53:15 +03:00
|
|
|
ScopedSpinLock lock(g_scheduler_lock);
|
2019-02-07 13:12:23 +03:00
|
|
|
|
2020-09-15 22:53:15 +03:00
|
|
|
// "Leave" the critical section before switching context. Since we
|
|
|
|
// still hold the scheduler lock, we're not actually leaving it.
|
|
|
|
// Processor::switch_context expects Processor::in_critical() to be 1
|
|
|
|
critical.leave();
|
|
|
|
donate_to_and_switch(beneficiary, reason);
|
2019-04-17 13:41:51 +03:00
|
|
|
return false;
|
2019-02-07 13:12:23 +03:00
|
|
|
}
|
|
|
|
|
2020-07-05 23:32:07 +03:00
|
|
|
bool Scheduler::context_switch(Thread* thread)
|
2018-11-08 00:15:02 +03:00
|
|
|
{
|
2020-07-05 23:32:07 +03:00
|
|
|
thread->did_schedule();
|
2018-11-08 00:15:02 +03:00
|
|
|
|
2020-07-05 23:32:07 +03:00
|
|
|
auto from_thread = Thread::current();
|
|
|
|
if (from_thread == thread)
|
2018-11-08 00:15:02 +03:00
|
|
|
return false;
|
|
|
|
|
2020-07-05 23:32:07 +03:00
|
|
|
if (from_thread) {
|
2018-11-08 00:15:02 +03:00
|
|
|
// If the last process hasn't blocked (still marked as running),
|
|
|
|
// mark it as runnable for the next round.
|
2020-07-05 23:32:07 +03:00
|
|
|
if (from_thread->state() == Thread::Running)
|
|
|
|
from_thread->set_state(Thread::Runnable);
|
2018-11-08 00:24:20 +03:00
|
|
|
|
|
|
|
#ifdef LOG_EVERY_CONTEXT_SWITCH
|
2021-01-27 06:44:01 +03:00
|
|
|
dbgln("Scheduler[{}]: {} -> {} [prio={}] {:04x}:{:08x}", Processor::id(), from_thread->tid().value(), thread->tid().value(), thread->priority(), thread->tss().cs, thread->tss().eip);
|
2018-11-08 00:24:20 +03:00
|
|
|
#endif
|
2018-11-08 00:15:02 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:19:50 +03:00
|
|
|
auto& proc = Processor::current();
|
2020-07-05 23:32:07 +03:00
|
|
|
if (!thread->is_initialized()) {
|
|
|
|
proc.init_context(*thread, false);
|
|
|
|
thread->set_initialized(true);
|
2019-09-07 16:50:44 +03:00
|
|
|
}
|
2020-07-05 23:32:07 +03:00
|
|
|
thread->set_state(Thread::Running);
|
|
|
|
|
|
|
|
proc.switch_context(from_thread, thread);
|
|
|
|
|
|
|
|
// NOTE: from_thread at this point reflects the thread we were
|
|
|
|
// switched from, and thread reflects Thread::current()
|
2020-12-08 07:29:41 +03:00
|
|
|
enter_current(*from_thread, false);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(thread == Thread::current());
|
2019-09-07 16:50:44 +03:00
|
|
|
|
2020-12-23 16:18:13 +03:00
|
|
|
#if ARCH(I386)
|
2021-01-25 23:19:34 +03:00
|
|
|
if (thread->process().is_user_process()) {
|
|
|
|
auto iopl = get_iopl_from_eflags(Thread::current()->get_register_dump_from_stack().eflags);
|
|
|
|
if (iopl != 0) {
|
2021-02-14 11:30:31 +03:00
|
|
|
PANIC("Switched to thread {} with non-zero IOPL={}", Thread::current()->tid().value(), iopl);
|
2021-01-25 23:19:34 +03:00
|
|
|
}
|
2020-12-23 16:18:13 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-08 00:15:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:29:41 +03:00
|
|
|
void Scheduler::enter_current(Thread& prev_thread, bool is_first)
|
2020-07-05 23:32:07 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(g_scheduler_lock.own_lock());
|
2020-07-05 23:32:07 +03:00
|
|
|
prev_thread.set_active(false);
|
|
|
|
if (prev_thread.state() == Thread::Dying) {
|
|
|
|
// If the thread we switched from is marked as dying, then notify
|
|
|
|
// the finalizer. Note that as soon as we leave the scheduler lock
|
|
|
|
// the finalizer may free from_thread!
|
|
|
|
notify_finalizer();
|
2020-12-08 07:29:41 +03:00
|
|
|
} else if (!is_first) {
|
|
|
|
// Check if we have any signals we should deliver (even if we don't
|
|
|
|
// end up switching to another thread).
|
|
|
|
auto current_thread = Thread::current();
|
2021-01-25 23:19:34 +03:00
|
|
|
if (!current_thread->is_in_block() && current_thread->previous_mode() != Thread::PreviousMode::KernelMode) {
|
2020-12-08 07:29:41 +03:00
|
|
|
ScopedSpinLock lock(current_thread->get_lock());
|
|
|
|
if (current_thread->state() == Thread::Running && current_thread->pending_signals_for_state()) {
|
|
|
|
current_thread->dispatch_one_pending_signal();
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 23:32:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:37:40 +03:00
|
|
|
void Scheduler::leave_on_first_switch(u32 flags)
|
|
|
|
{
|
2020-10-03 00:14:37 +03:00
|
|
|
// This is called when a thread is switched into for the first time.
|
2020-08-01 23:37:40 +03:00
|
|
|
// At this point, enter_current has already be called, but because
|
|
|
|
// Scheduler::context_switch is not in the call stack we need to
|
|
|
|
// clean up and release locks manually here
|
|
|
|
g_scheduler_lock.unlock(flags);
|
|
|
|
auto& scheduler_data = Processor::current().get_scheduler_data();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(scheduler_data.m_in_scheduler);
|
2020-08-01 23:37:40 +03:00
|
|
|
scheduler_data.m_in_scheduler = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::prepare_after_exec()
|
|
|
|
{
|
|
|
|
// This is called after exec() when doing a context "switch" into
|
|
|
|
// the new process. This is called from Processor::assume_context
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(g_scheduler_lock.own_lock());
|
2020-08-01 23:37:40 +03:00
|
|
|
auto& scheduler_data = Processor::current().get_scheduler_data();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!scheduler_data.m_in_scheduler);
|
2020-08-01 23:37:40 +03:00
|
|
|
scheduler_data.m_in_scheduler = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::prepare_for_idle_loop()
|
|
|
|
{
|
|
|
|
// This is called when the CPU finished setting up the idle loop
|
|
|
|
// and is about to run it. We need to acquire he scheduler lock
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!g_scheduler_lock.own_lock());
|
2020-08-01 23:37:40 +03:00
|
|
|
g_scheduler_lock.lock();
|
|
|
|
auto& scheduler_data = Processor::current().get_scheduler_data();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!scheduler_data.m_in_scheduler);
|
2020-08-01 23:37:40 +03:00
|
|
|
scheduler_data.m_in_scheduler = true;
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:28:12 +03:00
|
|
|
Process* Scheduler::colonel()
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(s_colonel_process);
|
2019-02-04 12:28:12 +03:00
|
|
|
return s_colonel_process;
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:41:50 +03:00
|
|
|
UNMAP_AFTER_INIT void Scheduler::initialize()
|
2018-11-08 00:15:02 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(&Processor::current() != nullptr); // sanity check
|
2020-06-27 22:42:28 +03:00
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
RefPtr<Thread> idle_thread;
|
2020-07-06 16:27:22 +03:00
|
|
|
g_finalizer_wait_queue = new WaitQueue;
|
2021-01-23 02:56:08 +03:00
|
|
|
g_ready_queues = new ThreadReadyQueue[g_ready_queue_buckets];
|
2020-06-29 01:05:52 +03:00
|
|
|
|
2020-07-06 16:27:22 +03:00
|
|
|
g_finalizer_has_work.store(false, AK::MemoryOrder::memory_order_release);
|
2020-09-06 00:52:14 +03:00
|
|
|
s_colonel_process = Process::create_kernel_process(idle_thread, "colonel", idle_loop, nullptr, 1).leak_ref();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(s_colonel_process);
|
|
|
|
VERIFY(idle_thread);
|
2020-07-06 16:27:22 +03:00
|
|
|
idle_thread->set_priority(THREAD_PRIORITY_MIN);
|
2020-09-12 06:11:07 +03:00
|
|
|
idle_thread->set_name(StringView("idle thread #0"));
|
2020-06-29 00:34:31 +03:00
|
|
|
|
2020-07-06 16:27:22 +03:00
|
|
|
set_idle_thread(idle_thread);
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:29:46 +03:00
|
|
|
UNMAP_AFTER_INIT void Scheduler::set_idle_thread(Thread* idle_thread)
|
2020-07-06 16:27:22 +03:00
|
|
|
{
|
2020-06-29 00:34:31 +03:00
|
|
|
Processor::current().set_idle_thread(*idle_thread);
|
2020-06-29 07:36:12 +03:00
|
|
|
Processor::current().set_current_thread(*idle_thread);
|
2020-07-06 16:27:22 +03:00
|
|
|
}
|
2020-06-29 01:05:52 +03:00
|
|
|
|
2021-02-19 23:29:46 +03:00
|
|
|
UNMAP_AFTER_INIT Thread* Scheduler::create_ap_idle_thread(u32 cpu)
|
2020-07-06 16:27:22 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(cpu != 0);
|
2020-07-06 16:27:22 +03:00
|
|
|
// This function is called on the bsp, but creates an idle thread for another AP
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(Processor::id() == 0);
|
2020-07-06 16:27:22 +03:00
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(s_colonel_process);
|
2020-11-17 06:51:34 +03:00
|
|
|
Thread* idle_thread = s_colonel_process->create_kernel_thread(idle_loop, nullptr, THREAD_PRIORITY_MIN, String::format("idle thread #%u", cpu), 1 << cpu, false);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(idle_thread);
|
2020-07-06 16:27:22 +03:00
|
|
|
return idle_thread;
|
2018-11-08 00:15:02 +03:00
|
|
|
}
|
2018-11-08 02:24:59 +03:00
|
|
|
|
2020-03-09 17:24:29 +03:00
|
|
|
void Scheduler::timer_tick(const RegisterState& regs)
|
2018-11-08 02:24:59 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
|
|
|
VERIFY(Processor::current().in_irq());
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-01-27 00:16:07 +03:00
|
|
|
auto current_thread = Processor::current_thread();
|
2020-06-29 00:34:31 +03:00
|
|
|
if (!current_thread)
|
2018-11-08 02:24:59 +03:00
|
|
|
return;
|
|
|
|
|
2021-01-25 23:19:34 +03:00
|
|
|
// Sanity checks
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(current_thread->current_trap());
|
|
|
|
VERIFY(current_thread->current_trap()->regs == ®s);
|
2021-01-25 23:19:34 +03:00
|
|
|
|
2021-01-27 08:45:30 +03:00
|
|
|
#if !SCHEDULE_ON_ALL_PROCESSORS
|
2021-01-27 06:44:01 +03:00
|
|
|
bool is_bsp = Processor::id() == 0;
|
2020-10-25 18:13:47 +03:00
|
|
|
if (!is_bsp)
|
|
|
|
return; // TODO: This prevents scheduling on other CPUs!
|
2021-01-27 08:45:30 +03:00
|
|
|
#endif
|
2021-03-02 19:19:35 +03:00
|
|
|
|
|
|
|
PerformanceEventBuffer* perf_events = nullptr;
|
|
|
|
|
|
|
|
if (g_profiling_all_threads) {
|
|
|
|
VERIFY(g_global_perf_events);
|
|
|
|
// FIXME: We currently don't collect samples while idle.
|
|
|
|
// That will be an interesting mode to add in the future. :^)
|
2021-03-02 21:01:02 +03:00
|
|
|
if (current_thread != Processor::current().idle_thread()) {
|
2021-03-02 19:19:35 +03:00
|
|
|
perf_events = g_global_perf_events;
|
2021-03-02 21:01:02 +03:00
|
|
|
if (current_thread->process().space().enforces_syscall_regions()) {
|
|
|
|
// FIXME: This is very nasty! We dump the current process's address
|
|
|
|
// space layout *every time* it's sampled. We should figure out
|
|
|
|
// a way to do this less often.
|
|
|
|
perf_events->add_process(current_thread->process());
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 19:19:35 +03:00
|
|
|
} else if (current_thread->process().is_profiling()) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(current_thread->process().perf_events());
|
2021-03-02 19:19:35 +03:00
|
|
|
perf_events = current_thread->process().perf_events();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (perf_events) {
|
|
|
|
[[maybe_unused]] auto rc = perf_events->append_with_eip_and_ebp(regs.eip, regs.ebp, PERF_EVENT_SAMPLE, 0, 0);
|
2019-12-11 22:36:56 +03:00
|
|
|
}
|
|
|
|
|
2021-01-26 02:37:36 +03:00
|
|
|
if (current_thread->tick())
|
2018-11-08 02:24:59 +03:00
|
|
|
return;
|
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
|
|
|
VERIFY(Processor::current().in_irq());
|
2020-06-27 22:42:28 +03:00
|
|
|
Processor::current().invoke_scheduler_async();
|
2018-11-08 02:24:59 +03:00
|
|
|
}
|
2019-09-14 20:44:22 +03:00
|
|
|
|
2020-06-27 22:42:28 +03:00
|
|
|
void Scheduler::invoke_async()
|
2019-09-14 20:44:22 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
2020-08-01 23:37:40 +03:00
|
|
|
auto& proc = Processor::current();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!proc.in_irq());
|
2020-08-01 23:37:40 +03:00
|
|
|
|
|
|
|
// Since this function is called when leaving critical sections (such
|
|
|
|
// as a SpinLock), we need to check if we're not already doing this
|
|
|
|
// to prevent recursion
|
|
|
|
if (!proc.get_scheduler_data().m_in_scheduler)
|
|
|
|
pick_next();
|
2019-09-14 20:44:22 +03:00
|
|
|
}
|
|
|
|
|
2020-12-01 05:04:36 +03:00
|
|
|
void Scheduler::yield_from_critical()
|
|
|
|
{
|
|
|
|
auto& proc = Processor::current();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(proc.in_critical());
|
|
|
|
VERIFY(!proc.in_irq());
|
2020-12-01 05:04:36 +03:00
|
|
|
|
|
|
|
yield(); // Flag a context switch
|
|
|
|
|
|
|
|
u32 prev_flags;
|
2021-01-28 01:23:21 +03:00
|
|
|
u32 prev_crit = Processor::current().clear_critical(prev_flags, false);
|
2020-12-01 05:04:36 +03:00
|
|
|
|
|
|
|
// Note, we may now be on a different CPU!
|
2021-01-28 01:23:21 +03:00
|
|
|
Processor::current().restore_critical(prev_crit, prev_flags);
|
2020-12-01 05:04:36 +03:00
|
|
|
}
|
|
|
|
|
2020-07-05 23:32:07 +03:00
|
|
|
void Scheduler::notify_finalizer()
|
|
|
|
{
|
|
|
|
if (g_finalizer_has_work.exchange(true, AK::MemoryOrder::memory_order_acq_rel) == false)
|
|
|
|
g_finalizer_wait_queue->wake_all();
|
|
|
|
}
|
|
|
|
|
2020-11-17 06:51:34 +03:00
|
|
|
void Scheduler::idle_loop(void*)
|
2019-09-14 20:44:22 +03:00
|
|
|
{
|
2020-10-29 01:06:16 +03:00
|
|
|
auto& proc = Processor::current();
|
|
|
|
dbgln("Scheduler[{}]: idle loop running", proc.get_id());
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(are_interrupts_enabled());
|
2020-07-06 16:27:22 +03:00
|
|
|
|
2019-09-14 20:44:22 +03:00
|
|
|
for (;;) {
|
2020-10-29 01:06:16 +03:00
|
|
|
proc.idle_begin();
|
2019-09-14 20:44:22 +03:00
|
|
|
asm("hlt");
|
2020-07-30 22:46:06 +03:00
|
|
|
|
2020-10-29 01:06:16 +03:00
|
|
|
proc.idle_end();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_ENABLED();
|
2021-01-27 08:45:30 +03:00
|
|
|
#if SCHEDULE_ON_ALL_PROCESSORS
|
|
|
|
yield();
|
|
|
|
#else
|
2020-10-29 01:06:16 +03:00
|
|
|
if (Processor::current().id() == 0)
|
2020-07-30 22:46:06 +03:00
|
|
|
yield();
|
2021-01-27 08:45:30 +03:00
|
|
|
#endif
|
2019-09-14 20:44:22 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|