summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_scoped_lock.h
blob: 03320859fd7101ef72fa8517ffe30954ae4c1ce1 (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
// 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"

namespace Kernel {

template <typename T>
concept KLockable = !std::is_reference<T>::value && requires(T & t) {
    { t.Lock() }
    ->std::same_as<void>;
    { t.Unlock() }
    ->std::same_as<void>;
};

template <typename T>
requires KLockable<T> class KScopedLock : NonCopyable {

private:
    T* lock_ptr;

public:
    explicit KScopedLock(T* l) : lock_ptr(l) {
        this->lock_ptr->Lock();
    }
    explicit KScopedLock(T& l) : KScopedLock(std::addressof(l)) { /* ... */
    }
    ~KScopedLock() {
        this->lock_ptr->Unlock();
    }
};

} // namespace Kernel