2023-06-11 10:06:23 +00:00
|
|
|
// Copyright 2023 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#include "Core/Core.h"
|
|
|
|
|
|
|
|
// The Core only supports using a single Host thread.
|
|
|
|
// If multiple threads want to call host functions then they need to queue
|
|
|
|
// sequentially for access.
|
|
|
|
struct HostThreadLock
|
|
|
|
{
|
2023-06-11 10:18:16 +00:00
|
|
|
public:
|
2023-06-11 10:06:23 +00:00
|
|
|
explicit HostThreadLock() : m_lock(s_host_identity_mutex) { Core::DeclareAsHostThread(); }
|
2023-06-11 10:18:16 +00:00
|
|
|
|
|
|
|
~HostThreadLock()
|
|
|
|
{
|
|
|
|
if (m_lock.owns_lock())
|
|
|
|
Core::UndeclareAsHostThread();
|
|
|
|
}
|
|
|
|
|
2023-06-11 10:06:23 +00:00
|
|
|
HostThreadLock(const HostThreadLock& other) = delete;
|
|
|
|
HostThreadLock(HostThreadLock&& other) = delete;
|
|
|
|
HostThreadLock& operator=(const HostThreadLock& other) = delete;
|
|
|
|
HostThreadLock& operator=(HostThreadLock&& other) = delete;
|
2023-06-11 10:18:16 +00:00
|
|
|
|
|
|
|
void Lock()
|
|
|
|
{
|
|
|
|
m_lock.lock();
|
|
|
|
Core::DeclareAsHostThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unlock()
|
|
|
|
{
|
|
|
|
m_lock.unlock();
|
|
|
|
Core::UndeclareAsHostThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static std::mutex s_host_identity_mutex;
|
|
|
|
std::unique_lock<std::mutex> m_lock;
|
2023-06-11 10:06:23 +00:00
|
|
|
};
|