2020-12-04 06:56:02 +01:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
// This file references various implementation details from Atmosphere, an open-source firmware for
|
|
|
|
// the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2020-12-31 08:01:08 +01:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2020-12-04 06:56:02 +01:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/time_manager.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-04-07 07:44:30 +02:00
|
|
|
class [[nodiscard]] KScopedSchedulerLockAndSleep {
|
2020-12-04 06:56:02 +01:00
|
|
|
public:
|
2021-09-24 07:21:07 +02:00
|
|
|
explicit KScopedSchedulerLockAndSleep(KernelCore& kernel_, KThread* t, s64 timeout)
|
2021-05-16 07:46:30 +02:00
|
|
|
: kernel(kernel_), thread(t), timeout_tick(timeout) {
|
2020-12-05 08:37:35 +01:00
|
|
|
// Lock the scheduler.
|
2020-12-04 06:56:02 +01:00
|
|
|
kernel.GlobalSchedulerContext().scheduler_lock.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~KScopedSchedulerLockAndSleep() {
|
2020-12-05 08:37:35 +01:00
|
|
|
// Register the sleep.
|
2021-01-20 06:05:24 +01:00
|
|
|
if (timeout_tick > 0) {
|
|
|
|
kernel.TimeManager().ScheduleTimeEvent(thread, timeout_tick);
|
2020-12-04 06:56:02 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 08:37:35 +01:00
|
|
|
// Unlock the scheduler.
|
2020-12-04 06:56:02 +01:00
|
|
|
kernel.GlobalSchedulerContext().scheduler_lock.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CancelSleep() {
|
2021-01-20 06:05:24 +01:00
|
|
|
timeout_tick = 0;
|
2020-12-04 06:56:02 +01:00
|
|
|
}
|
2020-12-05 09:07:44 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
KernelCore& kernel;
|
2020-12-31 08:01:08 +01:00
|
|
|
KThread* thread{};
|
2020-12-05 09:07:44 +01:00
|
|
|
s64 timeout_tick{};
|
2020-12-04 06:56:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Kernel
|