mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-07-23 10:35:03 +00:00
47 lines
1.0 KiB
C++
47 lines
1.0 KiB
C++
// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#pragma once
|
|
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
|
|
namespace Common {
|
|
|
|
// Like std::shared_mutex, but reader has priority over writer.
|
|
class SharedFirstMutex {
|
|
public:
|
|
void lock() {
|
|
std::unique_lock<std::mutex> lock(mtx);
|
|
cv.wait(lock, [this]() { return !writer_active && readers == 0; });
|
|
writer_active = true;
|
|
}
|
|
|
|
void unlock() {
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
writer_active = false;
|
|
cv.notify_all();
|
|
}
|
|
|
|
void lock_shared() {
|
|
std::unique_lock<std::mutex> lock(mtx);
|
|
cv.wait(lock, [this]() { return !writer_active; });
|
|
++readers;
|
|
}
|
|
|
|
void unlock_shared() {
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
if (--readers == 0) {
|
|
cv.notify_all();
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::mutex mtx;
|
|
std::condition_variable cv;
|
|
int readers = 0;
|
|
bool writer_active = false;
|
|
};
|
|
|
|
} // namespace Common
|