summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h
blob: d1cc5dda789a7841477367187a5d1926ee10b4af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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"
#include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/thread.h"
#include "core/hle/kernel/time_manager.h"

namespace Kernel {

class KScopedSchedulerLockAndSleep {
private:
    KernelCore& kernel;
    Handle& event_handle;
    Thread* thread{};
    s64 timeout_tick{};

public:
    explicit KScopedSchedulerLockAndSleep(KernelCore& kernel, Handle& event_handle, Thread* t,
                                          s64 timeout)
        : kernel(kernel), event_handle(event_handle), thread(t), timeout_tick(timeout) {
        event_handle = InvalidHandle;

        // Lock the scheduler.
        kernel.GlobalSchedulerContext().scheduler_lock.Lock();
    }

    ~KScopedSchedulerLockAndSleep() {
        // Register the sleep.
        if (this->timeout_tick > 0) {
            kernel.TimeManager().ScheduleTimeEvent(event_handle, this->thread, this->timeout_tick);
        }

        // Unlock the scheduler.
        kernel.GlobalSchedulerContext().scheduler_lock.Unlock();
    }

    void CancelSleep() {
        this->timeout_tick = 0;
    }
};

} // namespace Kernel