3
0
Fork 0
forked from suyu/suyu

scheduler: Protect scheduling functions with a global mutex.

This commit is contained in:
bunnei 2018-05-07 22:12:45 -04:00
parent e6671190a5
commit 91af2f94e8
2 changed files with 18 additions and 0 deletions

View file

@ -9,6 +9,8 @@
namespace Kernel { namespace Kernel {
std::mutex Scheduler::scheduler_mutex;
Scheduler::Scheduler(ARM_Interface* cpu_core) : cpu_core(cpu_core) {} Scheduler::Scheduler(ARM_Interface* cpu_core) : cpu_core(cpu_core) {}
Scheduler::~Scheduler() { Scheduler::~Scheduler() {
@ -18,6 +20,7 @@ Scheduler::~Scheduler() {
} }
bool Scheduler::HaveReadyThreads() { bool Scheduler::HaveReadyThreads() {
std::lock_guard<std::mutex> lock(scheduler_mutex);
return ready_queue.get_first() != nullptr; return ready_queue.get_first() != nullptr;
} }
@ -90,6 +93,8 @@ void Scheduler::SwitchContext(Thread* new_thread) {
} }
void Scheduler::Reschedule() { void Scheduler::Reschedule() {
std::lock_guard<std::mutex> lock(scheduler_mutex);
Thread* cur = GetCurrentThread(); Thread* cur = GetCurrentThread();
Thread* next = PopNextReadyThread(); Thread* next = PopNextReadyThread();
@ -105,26 +110,36 @@ void Scheduler::Reschedule() {
} }
void Scheduler::AddThread(SharedPtr<Thread> thread, u32 priority) { void Scheduler::AddThread(SharedPtr<Thread> thread, u32 priority) {
std::lock_guard<std::mutex> lock(scheduler_mutex);
thread_list.push_back(thread); thread_list.push_back(thread);
ready_queue.prepare(priority); ready_queue.prepare(priority);
} }
void Scheduler::RemoveThread(Thread* thread) { void Scheduler::RemoveThread(Thread* thread) {
std::lock_guard<std::mutex> lock(scheduler_mutex);
thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread), thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread),
thread_list.end()); thread_list.end());
} }
void Scheduler::ScheduleThread(Thread* thread, u32 priority) { void Scheduler::ScheduleThread(Thread* thread, u32 priority) {
std::lock_guard<std::mutex> lock(scheduler_mutex);
ASSERT(thread->status == THREADSTATUS_READY); ASSERT(thread->status == THREADSTATUS_READY);
ready_queue.push_back(priority, thread); ready_queue.push_back(priority, thread);
} }
void Scheduler::UnscheduleThread(Thread* thread, u32 priority) { void Scheduler::UnscheduleThread(Thread* thread, u32 priority) {
std::lock_guard<std::mutex> lock(scheduler_mutex);
ASSERT(thread->status == THREADSTATUS_READY); ASSERT(thread->status == THREADSTATUS_READY);
ready_queue.remove(priority, thread); ready_queue.remove(priority, thread);
} }
void Scheduler::SetThreadPriority(Thread* thread, u32 priority) { void Scheduler::SetThreadPriority(Thread* thread, u32 priority) {
std::lock_guard<std::mutex> lock(scheduler_mutex);
// If thread was ready, adjust queues // If thread was ready, adjust queues
if (thread->status == THREADSTATUS_READY) if (thread->status == THREADSTATUS_READY)
ready_queue.move(thread, thread->current_priority, priority); ready_queue.move(thread, thread->current_priority, priority);

View file

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <mutex>
#include <vector> #include <vector>
#include "common/common_types.h" #include "common/common_types.h"
#include "common/thread_queue_list.h" #include "common/thread_queue_list.h"
@ -68,6 +69,8 @@ private:
SharedPtr<Thread> current_thread = nullptr; SharedPtr<Thread> current_thread = nullptr;
ARM_Interface* cpu_core; ARM_Interface* cpu_core;
static std::mutex scheduler_mutex;
}; };
} // namespace Kernel } // namespace Kernel