mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-07-12 04:35:56 +00:00
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
46 lines
1 KiB
C++
46 lines
1 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
|