Android: And Lock and Unlock wrappers to HostThreadLock

This way we can ensure DeclareAsHostThread and UndeclareAsHostThread
are called when locking and unlocking.
This commit is contained in:
JosJuice
2023-06-11 12:18:16 +02:00
parent 5524042922
commit 3519a7070d
2 changed files with 29 additions and 9 deletions

View File

@ -12,13 +12,33 @@
// sequentially for access.
struct HostThreadLock
{
static std::mutex s_host_identity_mutex;
std::unique_lock<std::mutex> m_lock;
public:
explicit HostThreadLock() : m_lock(s_host_identity_mutex) { Core::DeclareAsHostThread(); }
~HostThreadLock()
{
if (m_lock.owns_lock())
Core::UndeclareAsHostThread();
}
HostThreadLock(const HostThreadLock& other) = delete;
HostThreadLock(HostThreadLock&& other) = delete;
HostThreadLock& operator=(const HostThreadLock& other) = delete;
HostThreadLock& operator=(HostThreadLock&& other) = delete;
~HostThreadLock() { Core::UndeclareAsHostThread(); }
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;
};