pineapple-src/src/core/hle/kernel/time_manager.cpp

68 lines
2.2 KiB
C++
Raw Normal View History

2020-12-28 16:15:37 +01:00
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "common/assert.h"
#include "core/core.h"
#include "core/core_timing.h"
#include "core/core_timing_util.h"
#include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/k_scheduler.h"
#include "core/hle/kernel/kernel.h"
2021-01-21 07:23:23 +01:00
#include "core/hle/kernel/thread.h"
2020-12-28 16:15:37 +01:00
#include "core/hle/kernel/time_manager.h"
namespace Kernel {
TimeManager::TimeManager(Core::System& system_) : system{system_} {
time_manager_event_type = Core::Timing::CreateEvent(
"Kernel::TimeManagerCallback",
[this](std::uintptr_t thread_handle, std::chrono::nanoseconds) {
2021-01-21 07:23:23 +01:00
std::shared_ptr<Thread> thread;
2020-12-28 16:15:37 +01:00
{
std::lock_guard lock{mutex};
2021-01-21 07:23:23 +01:00
const auto proper_handle = static_cast<Handle>(thread_handle);
if (cancelled_events[proper_handle]) {
return;
}
thread = system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
}
if (thread) {
// Thread can be null if process has exited
thread->Wakeup();
2020-12-28 16:15:37 +01:00
}
});
}
2021-01-21 07:23:23 +01:00
void TimeManager::ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64 nanoseconds) {
2020-12-28 16:15:37 +01:00
std::lock_guard lock{mutex};
2021-01-21 07:23:23 +01:00
event_handle = timetask->GetGlobalHandle();
2020-12-28 16:15:37 +01:00
if (nanoseconds > 0) {
2021-01-21 07:23:23 +01:00
ASSERT(timetask);
ASSERT(timetask->GetState() != ThreadState::Runnable);
2020-12-28 16:15:37 +01:00
system.CoreTiming().ScheduleEvent(std::chrono::nanoseconds{nanoseconds},
2021-01-21 07:23:23 +01:00
time_manager_event_type, event_handle);
} else {
event_handle = InvalidHandle;
}
cancelled_events[event_handle] = false;
}
void TimeManager::UnscheduleTimeEvent(Handle event_handle) {
std::lock_guard lock{mutex};
if (event_handle == InvalidHandle) {
return;
2020-12-28 16:15:37 +01:00
}
2021-01-21 07:23:23 +01:00
system.CoreTiming().UnscheduleEvent(time_manager_event_type, event_handle);
cancelled_events[event_handle] = true;
2020-12-28 16:15:37 +01:00
}
2021-01-21 07:23:23 +01:00
void TimeManager::CancelTimeEvent(Thread* time_task) {
2020-12-28 16:15:37 +01:00
std::lock_guard lock{mutex};
2021-01-21 07:23:23 +01:00
const Handle event_handle = time_task->GetGlobalHandle();
UnscheduleTimeEvent(event_handle);
2020-12-28 16:15:37 +01:00
}
} // namespace Kernel