2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-12-04 06:56:02 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2021-11-10 04:02:11 +01:00
|
|
|
#include "core/hle/kernel/global_scheduler_context.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
|