2020-02-14 14:56:27 +00:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-02-22 14:27:40 +00:00
|
|
|
#include "common/assert.h"
|
2020-02-14 14:56:27 +00:00
|
|
|
#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/kernel.h"
|
2020-02-27 02:26:53 +00:00
|
|
|
#include "core/hle/kernel/scheduler.h"
|
2020-02-14 14:56:27 +00:00
|
|
|
#include "core/hle/kernel/thread.h"
|
|
|
|
#include "core/hle/kernel/time_manager.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2020-02-27 02:26:53 +00:00
|
|
|
TimeManager::TimeManager(Core::System& system_) : system{system_} {
|
2020-02-14 14:56:27 +00:00
|
|
|
time_manager_event_type = Core::Timing::CreateEvent(
|
2020-07-27 23:00:41 +00:00
|
|
|
"Kernel::TimeManagerCallback",
|
|
|
|
[this](std::uintptr_t thread_handle, std::chrono::nanoseconds) {
|
|
|
|
const SchedulerLock lock(system.Kernel());
|
|
|
|
const auto proper_handle = static_cast<Handle>(thread_handle);
|
2020-02-27 02:26:53 +00:00
|
|
|
if (cancelled_events[proper_handle]) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-27 23:00:41 +00:00
|
|
|
auto thread = this->system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
|
2020-02-25 02:04:12 +00:00
|
|
|
thread->OnWakeUp();
|
2020-02-14 14:56:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeManager::ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64 nanoseconds) {
|
2020-02-27 02:26:53 +00:00
|
|
|
event_handle = timetask->GetGlobalHandle();
|
2020-02-14 14:56:27 +00:00
|
|
|
if (nanoseconds > 0) {
|
|
|
|
ASSERT(timetask);
|
2020-03-08 20:20:05 +00:00
|
|
|
ASSERT(timetask->GetStatus() != ThreadStatus::Ready);
|
|
|
|
ASSERT(timetask->GetStatus() != ThreadStatus::WaitMutex);
|
2020-07-15 22:30:06 +00:00
|
|
|
system.CoreTiming().ScheduleEvent(std::chrono::nanoseconds{nanoseconds},
|
|
|
|
time_manager_event_type, event_handle);
|
2020-02-14 14:56:27 +00:00
|
|
|
} else {
|
|
|
|
event_handle = InvalidHandle;
|
|
|
|
}
|
2020-02-27 02:26:53 +00:00
|
|
|
cancelled_events[event_handle] = false;
|
2020-02-14 14:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimeManager::UnscheduleTimeEvent(Handle event_handle) {
|
2020-02-22 14:27:40 +00:00
|
|
|
if (event_handle == InvalidHandle) {
|
|
|
|
return;
|
2020-02-14 14:56:27 +00:00
|
|
|
}
|
2020-02-22 14:27:40 +00:00
|
|
|
system.CoreTiming().UnscheduleEvent(time_manager_event_type, event_handle);
|
2020-02-27 02:26:53 +00:00
|
|
|
cancelled_events[event_handle] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeManager::CancelTimeEvent(Thread* time_task) {
|
|
|
|
Handle event_handle = time_task->GetGlobalHandle();
|
|
|
|
UnscheduleTimeEvent(event_handle);
|
2020-02-14 14:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|