blob: f7bc8b4d057ea5f51f8420520a06be234b96ae81 (
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
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <atomic>
#include <mutex> // Kernel headers that use std::mutex get it through this one
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "core/hle/kernel/k_scoped_lock.h"
namespace Kernel {
class KSpinLock {
public:
explicit KSpinLock() = default;
YUZU_NON_COPYABLE(KSpinLock);
YUZU_NON_MOVEABLE(KSpinLock);
void Lock();
void Unlock();
bool TryLock();
private:
// 0 free, 1 held, 2 held with (possible) sleepers. The scheduler lock is taken by every
// core thousands of times a frame and held for microseconds: a contended Lock spins briefly
// before sleeping, instead of putting the host thread to sleep at once and paying its
// wake-up latency (std::mutex did).
std::atomic<u32> m_state{0};
};
// TODO(bunnei): Alias for now, in case we want to implement these accurately in the future.
using KAlignedSpinLock = KSpinLock;
using KNotAlignedSpinLock = KSpinLock;
using KScopedSpinLock = KScopedLock<KSpinLock>;
using KScopedAlignedSpinLock = KScopedLock<KAlignedSpinLock>;
using KScopedNotAlignedSpinLock = KScopedLock<KNotAlignedSpinLock>;
} // namespace Kernel
|