2020-02-14 06:56:27 -08:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-02-22 06:27:40 -08:00
|
|
|
#include "common/assert.h"
|
2020-02-14 06:56:27 -08:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/core_timing.h"
|
2021-11-10 22:28:30 -08:00
|
|
|
#include "core/hle/kernel/k_scheduler.h"
|
2020-12-30 23:01:08 -08:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2020-02-14 06:56:27 -08:00
|
|
|
#include "core/hle/kernel/time_manager.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2020-02-26 18:26:53 -08:00
|
|
|
TimeManager::TimeManager(Core::System& system_) : system{system_} {
|
2021-04-03 19:11:46 -07:00
|
|
|
time_manager_event_type =
|
|
|
|
Core::Timing::CreateEvent("Kernel::TimeManagerCallback",
|
|
|
|
[this](std::uintptr_t thread_handle, std::chrono::nanoseconds) {
|
|
|
|
KThread* thread = reinterpret_cast<KThread*>(thread_handle);
|
2021-11-10 22:28:30 -08:00
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl(system.Kernel());
|
|
|
|
thread->OnTimer();
|
|
|
|
}
|
2021-04-03 19:11:46 -07:00
|
|
|
});
|
2020-02-14 06:56:27 -08:00
|
|
|
}
|
|
|
|
|
2021-01-19 21:05:24 -08:00
|
|
|
void TimeManager::ScheduleTimeEvent(KThread* thread, s64 nanoseconds) {
|
2020-11-18 16:19:00 -08:00
|
|
|
std::lock_guard lock{mutex};
|
2020-02-14 06:56:27 -08:00
|
|
|
if (nanoseconds > 0) {
|
2021-01-19 21:05:24 -08:00
|
|
|
ASSERT(thread);
|
|
|
|
ASSERT(thread->GetState() != ThreadState::Runnable);
|
2020-07-15 15:30:06 -07:00
|
|
|
system.CoreTiming().ScheduleEvent(std::chrono::nanoseconds{nanoseconds},
|
2021-01-19 21:05:24 -08:00
|
|
|
time_manager_event_type,
|
|
|
|
reinterpret_cast<uintptr_t>(thread));
|
2020-02-14 06:56:27 -08:00
|
|
|
}
|
2020-02-26 18:26:53 -08:00
|
|
|
}
|
|
|
|
|
2021-01-19 21:05:24 -08:00
|
|
|
void TimeManager::UnscheduleTimeEvent(KThread* thread) {
|
2020-12-03 22:43:35 -08:00
|
|
|
std::lock_guard lock{mutex};
|
2021-01-19 21:05:24 -08:00
|
|
|
system.CoreTiming().UnscheduleEvent(time_manager_event_type,
|
|
|
|
reinterpret_cast<uintptr_t>(thread));
|
2020-02-14 06:56:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|