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"
|
2020-12-03 02:08:35 +00:00
|
|
|
#include "core/hle/kernel/k_scheduler.h"
|
2020-12-31 07:01:08 +00:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2020-02-14 14:56:27 +00:00
|
|
|
#include "core/hle/kernel/kernel.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) {
|
2020-12-31 07:01:08 +00:00
|
|
|
std::shared_ptr<KThread> thread;
|
2020-12-04 06:43:35 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
2021-01-20 05:05:24 +00:00
|
|
|
thread = SharedFrom<KThread>(reinterpret_cast<KThread*>(thread_handle));
|
2020-11-14 06:49:33 +00:00
|
|
|
}
|
2021-01-20 05:05:24 +00:00
|
|
|
thread->Wakeup();
|
2020-02-14 14:56:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-20 05:05:24 +00:00
|
|
|
void TimeManager::ScheduleTimeEvent(KThread* thread, s64 nanoseconds) {
|
2020-11-19 00:19:00 +00:00
|
|
|
std::lock_guard lock{mutex};
|
2020-02-14 14:56:27 +00:00
|
|
|
if (nanoseconds > 0) {
|
2021-01-20 05:05:24 +00:00
|
|
|
ASSERT(thread);
|
|
|
|
ASSERT(thread->GetState() != ThreadState::Runnable);
|
2020-07-15 22:30:06 +00:00
|
|
|
system.CoreTiming().ScheduleEvent(std::chrono::nanoseconds{nanoseconds},
|
2021-01-20 05:05:24 +00:00
|
|
|
time_manager_event_type,
|
|
|
|
reinterpret_cast<uintptr_t>(thread));
|
2020-02-14 14:56:27 +00:00
|
|
|
}
|
2020-02-27 02:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 05:05:24 +00:00
|
|
|
void TimeManager::UnscheduleTimeEvent(KThread* thread) {
|
2020-12-04 06:43:35 +00:00
|
|
|
std::lock_guard lock{mutex};
|
2021-01-20 05:05:24 +00:00
|
|
|
system.CoreTiming().UnscheduleEvent(time_manager_event_type,
|
|
|
|
reinterpret_cast<uintptr_t>(thread));
|
2020-02-14 14:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|