Misc: Replace std::mutex with Threading::Mutex

And unnecessary unique_lock -> lock_guard.
wip3-rebase
Stenzek 6 days ago
parent 3bb2b0b1d6
commit 22072c934f
No known key found for this signature in database

@ -5,6 +5,7 @@
#include "assert.h"
#include "crash_handler.h"
#include "threading.h"
#include <cstdio>
#include <cstdlib>
@ -21,7 +22,7 @@
#pragma clang diagnostic ignored "-Winvalid-noreturn"
#endif
static std::mutex s_AssertFailedMutex;
static Threading::Mutex s_AssertFailedMutex;
static HANDLE FreezeThreads()
{
@ -81,7 +82,7 @@ void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char*
std::snprintf(szMsg, sizeof(szMsg), "%s in function %s (%s:%u)\n", szMessage, szFunction, szFile, uLine);
#if defined(_WIN32)
std::unique_lock lock(s_AssertFailedMutex);
std::lock_guard lock(s_AssertFailedMutex);
HANDLE pHandle = FreezeThreads();
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
@ -118,7 +119,7 @@ void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char*
std::snprintf(szMsg, sizeof(szMsg), "%s in function %s (%s:%u)\n", szMessage, szFunction, szFile, uLine);
#if defined(_WIN32)
std::unique_lock guard(s_AssertFailedMutex);
std::lock_guard guard(s_AssertFailedMutex);
HANDLE pHandle = FreezeThreads();
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);

@ -5,9 +5,10 @@
#include "assert.h"
#include "file_system.h"
#include "small_string.h"
#include "threading.h"
#include "timer.h"
#include "fmt/format.h"
#include <fmt/format.h>
#include <array>
#include <bitset>
@ -39,9 +40,9 @@ struct RegisteredCallback
using ChannelBitSet = std::bitset<static_cast<size_t>(Channel::MaxCount)>;
static void RegisterCallback(CallbackFunctionType callbackFunction, void* pUserParam,
const std::unique_lock<std::mutex>& lock);
const std::unique_lock<Threading::Mutex>& lock);
static void UnregisterCallback(CallbackFunctionType callbackFunction, void* pUserParam,
const std::unique_lock<std::mutex>& lock);
const std::unique_lock<Threading::Mutex>& lock);
static void UpdateEffectiveLevel();
static bool FilterTest(Channel channel, Level level);
@ -81,7 +82,7 @@ struct State
ChannelBitSet log_channels_enabled = ChannelBitSet().set();
std::vector<RegisteredCallback> callbacks;
std::mutex callbacks_mutex;
Threading::Mutex callbacks_mutex;
Timer::Value start_timestamp = Timer::GetCurrentValue();
@ -113,7 +114,7 @@ void Log::RegisterCallback(CallbackFunctionType callbackFunction, void* pUserPar
}
void Log::RegisterCallback(CallbackFunctionType callbackFunction, void* pUserParam,
const std::unique_lock<std::mutex>& lock)
const std::unique_lock<Threading::Mutex>& lock)
{
RegisteredCallback Callback;
Callback.Function = callbackFunction;
@ -130,7 +131,7 @@ void Log::UnregisterCallback(CallbackFunctionType callbackFunction, void* pUserP
}
void Log::UnregisterCallback(CallbackFunctionType callbackFunction, void* pUserParam,
const std::unique_lock<std::mutex>& lock)
const std::unique_lock<Threading::Mutex>& lock)
{
for (auto iter = s_state.callbacks.begin(); iter != s_state.callbacks.end(); ++iter)
{

@ -4,9 +4,11 @@
#include "perf_scope.h"
#include "assert.h"
#include "string_util.h"
#include "threading.h"
#include <array>
#include <cstring>
#include <mutex>
#ifdef __linux__
#include <atomic>
@ -26,10 +28,10 @@
static std::FILE* s_map_file = nullptr;
static bool s_map_file_opened = false;
static std::mutex s_mutex;
static Threading::Mutex s_mutex;
static void RegisterMethod(const void* ptr, size_t size, const char* symbol)
{
std::unique_lock lock(s_mutex);
std::lock_guard lock(s_mutex);
if (!s_map_file)
{
@ -96,16 +98,16 @@ static u64 JitDumpTimestamp()
return (static_cast<u64>(ts.tv_sec) * 1000000000ULL) + static_cast<u64>(ts.tv_nsec);
}
static FILE* s_jitdump_file = nullptr;
static std::FILE* s_jitdump_file = nullptr;
static bool s_jitdump_file_opened = false;
static std::mutex s_jitdump_mutex;
static Threading::Mutex s_jitdump_mutex;
static u32 s_jitdump_record_id;
static void RegisterMethod(const void* ptr, size_t size, const char* symbol)
{
const u32 namelen = std::strlen(symbol) + 1;
std::unique_lock lock(s_jitdump_mutex);
std::lock_guard lock(s_jitdump_mutex);
if (!s_jitdump_file)
{
if (!s_jitdump_file_opened)

@ -82,7 +82,7 @@ void TaskQueue::WaitForAll()
WaitForAll(lock);
}
void TaskQueue::WaitForAll(std::unique_lock<std::mutex>& lock)
void TaskQueue::WaitForAll(std::unique_lock<Threading::Mutex>& lock)
{
// while we're waiting, execute work on the calling thread
m_tasks_done_cv.wait(lock, [this, &lock]() {
@ -106,7 +106,7 @@ bool TaskQueue::ExecuteOneTask()
return true;
}
void TaskQueue::ExecuteOneTask(std::unique_lock<std::mutex>& lock)
void TaskQueue::ExecuteOneTask(std::unique_lock<Threading::Mutex>& lock)
{
TaskFunctionType func = std::move(m_tasks.front());
m_tasks.pop_front();

@ -47,23 +47,23 @@ private:
/// Waits for all submitted tasks to complete execution.
/// This is a helper function that assumes a lock is already held.
/// @param lock A unique_lock object holding the mutex.
void WaitForAll(std::unique_lock<std::mutex>& lock);
void WaitForAll(std::unique_lock<Threading::Mutex>& lock);
/// Executes one task from the queue.
/// This is a helper function that assumes a lock is already held.
/// @param lock A unique_lock object holding the mutex.
void ExecuteOneTask(std::unique_lock<std::mutex>& lock);
void ExecuteOneTask(std::unique_lock<Threading::Mutex>& lock);
/// Entry point for worker threads. Executes tasks from the queue until termination is signaled.
void WorkerThreadEntryPoint();
std::mutex m_mutex;
Threading::Mutex m_mutex;
std::deque<TaskFunctionType> m_tasks;
size_t m_tasks_outstanding = 0;
u32 m_threads_busy = 0;
u16 m_max_threads = 0;
bool m_threads_done = false;
std::condition_variable m_task_wait_cv;
std::condition_variable m_tasks_done_cv;
Threading::ConditionVariable m_task_wait_cv;
Threading::ConditionVariable m_tasks_done_cv;
std::vector<std::thread> m_threads;
};

@ -27,6 +27,7 @@
#include "common/settings_interface.h"
#include "common/small_string.h"
#include "common/string_util.h"
#include "common/threading.h"
#include "IconsEmoji.h"
#include "IconsFontAwesome.h"
@ -236,7 +237,7 @@ struct Locals
struct ArchiveLocals
{
std::mutex zip_mutex;
Threading::Mutex zip_mutex;
CheatArchive patches_zip;
CheatArchive cheats_zip;
};

@ -16,6 +16,7 @@
#include "common/log.h"
#include "common/lru_cache.h"
#include "common/path.h"
#include "common/threading.h"
#include <algorithm>
#include <cstring>
@ -89,7 +90,7 @@ using ActiveSoundEntry = std::variant<PlayingStreamedEffect, PlayingCachedEffect
struct Locals
{
std::mutex state_mutex;
Threading::Mutex state_mutex;
std::deque<ActiveSoundEntry> active_sounds;
std::unique_ptr<AudioStream> audio_stream;
DynamicHeapArray<AudioStream::SampleType> temp_buffer;
@ -109,7 +110,8 @@ static bool LockedIsInitialized();
static bool LoadCachedEffect(const std::string& resource_name, const CachedEffectPtr& effect, Error* error);
/// Looks up a cached effect, loading it if necessary.
static const CachedEffectPtr* LookupOrLoadCachedEffect(std::string resource_name, std::unique_lock<std::mutex>& lock);
static const CachedEffectPtr* LookupOrLoadCachedEffect(std::string resource_name,
std::unique_lock<Threading::Mutex>& lock);
/// Opens a WAV file for streaming, checking that it matches the correct format.
static bool OpenFileForStreaming(const char* path, WAVReader* reader, Error* error);
@ -260,7 +262,7 @@ bool SoundEffectManager::LoadCachedEffect(const std::string& resource_name, cons
}
const SoundEffectManager::CachedEffectPtr*
SoundEffectManager::LookupOrLoadCachedEffect(std::string resource_name, std::unique_lock<std::mutex>& lock)
SoundEffectManager::LookupOrLoadCachedEffect(std::string resource_name, std::unique_lock<Threading::Mutex>& lock)
{
const CachedEffectPtr* cached_effect = s_locals.effect_cache.Lookup(resource_name);
if (cached_effect)

@ -65,8 +65,8 @@ static void ProcessCoreThreadEvents();
struct RegTestHostState
{
ALIGN_TO_CACHE_LINE std::mutex core_thread_events_mutex;
std::condition_variable core_thread_event_done;
ALIGN_TO_CACHE_LINE Threading::Mutex core_thread_events_mutex;
Threading::ConditionVariable core_thread_event_done;
std::deque<std::pair<std::function<void()>, bool>> cpu_thread_events;
u32 blocking_cpu_events_pending = 0;
};

@ -10,9 +10,10 @@
#include "common/error.h"
#include "common/log.h"
#include "common/string_util.h"
#include "common/threading.h"
#include "cubeb/cubeb.h"
#include "fmt/format.h"
#include <cubeb/cubeb.h>
#include <fmt/format.h>
#include <mutex>
#include <string>
@ -23,7 +24,7 @@ namespace {
struct CubebContextHolder
{
std::mutex mutex;
Threading::Mutex mutex;
cubeb* context = nullptr;
u32 reference_count = 0;
std::string driver_name;
@ -95,7 +96,7 @@ static void CubebLogCallback(const char* fmt, ...)
static cubeb* GetCubebContext(std::string_view driver_name, Error* error)
{
std::lock_guard<std::mutex> lock(s_cubeb_context.mutex);
std::lock_guard lock(s_cubeb_context.mutex);
if (s_cubeb_context.context)
{
// Check if the requested driver/device matches the existing context.
@ -127,7 +128,7 @@ static cubeb* GetCubebContext(std::string_view driver_name, Error* error)
static void ReleaseCubebContext(cubeb* ctx)
{
std::lock_guard<std::mutex> lock(s_cubeb_context.mutex);
std::lock_guard lock(s_cubeb_context.mutex);
AssertMsg(s_cubeb_context.context == ctx, "Cubeb context mismatch on release.");
Assert(s_cubeb_context.reference_count > 0);
s_cubeb_context.reference_count--;

@ -16,6 +16,7 @@
#include "common/log.h"
#include "common/path.h"
#include "common/string_util.h"
#include "common/threading.h"
#include "fmt/format.h"
@ -26,7 +27,7 @@
LOG_CHANNEL(GPUDevice);
// We need to synchronize instance creation because of adapter enumeration from the UI thread.
static std::mutex s_instance_mutex;
static Threading::Mutex s_instance_mutex;
static constexpr std::array<float, 4> s_clear_color = {};
static constexpr GPUTextureFormat s_swap_chain_format = GPUTextureFormat::RGBA8;
@ -63,7 +64,7 @@ bool D3D11Device::CreateDeviceAndMainSwapChain(std::string_view adapter, CreateF
const ExclusiveFullscreenMode* exclusive_fullscreen_mode,
std::optional<bool> exclusive_fullscreen_control, Error* error)
{
std::unique_lock lock(s_instance_mutex);
std::lock_guard lock(s_instance_mutex);
UINT d3d_create_flags = 0;
if (m_debug_device)
@ -146,7 +147,7 @@ bool D3D11Device::CreateDeviceAndMainSwapChain(std::string_view adapter, CreateF
void D3D11Device::DestroyDevice()
{
std::unique_lock lock(s_instance_mutex);
std::lock_guard lock(s_instance_mutex);
DestroyBuffers();
m_main_swap_chain.reset();

@ -18,6 +18,7 @@
#include "common/scoped_guard.h"
#include "common/small_string.h"
#include "common/string_util.h"
#include "common/threading.h"
#include "D3D12MemAlloc.h"
#include "fmt/format.h"
@ -46,7 +47,7 @@ enum : u32
};
// We need to synchronize instance creation because of adapter enumeration from the UI thread.
static std::mutex s_instance_mutex;
static Threading::Mutex s_instance_mutex;
static constexpr GPUTextureFormat s_swap_chain_format = GPUTextureFormat::RGBA8;
@ -142,7 +143,7 @@ bool D3D12Device::CreateDeviceAndMainSwapChain(std::string_view adapter, CreateF
const ExclusiveFullscreenMode* exclusive_fullscreen_mode,
std::optional<bool> exclusive_fullscreen_control, Error* error)
{
std::unique_lock lock(s_instance_mutex);
std::lock_guard lock(s_instance_mutex);
m_dxgi_factory = D3DCommon::CreateFactory(m_debug_device, error);
if (!m_dxgi_factory)
@ -291,7 +292,7 @@ bool D3D12Device::CreateDeviceAndMainSwapChain(std::string_view adapter, CreateF
void D3D12Device::DestroyDevice()
{
std::unique_lock lock(s_instance_mutex);
std::lock_guard lock(s_instance_mutex);
// Toss command list if we're recording...
if (InRenderPass())

@ -9,6 +9,7 @@
#include "gpu_texture.h"
#include "common/dimensional_array.h"
#include "common/threading.h"
#include "common/windows_headers.h"
#include <array>
@ -19,7 +20,6 @@
#include <dxgi1_5.h>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
@ -379,7 +379,7 @@ private:
{};
ComPtr<ID3D12PipelineLibrary> m_pipeline_library;
std::mutex m_pipeline_library_mutex;
Threading::Mutex m_pipeline_library_mutex;
};
class D3D12SwapChain : public GPUSwapChain

@ -13,6 +13,7 @@
#include "common/path.h"
#include "common/string_util.h"
#include "common/thirdparty/SmallVector.h"
#include "common/threading.h"
#include <array>
#include <deque>
@ -31,7 +32,7 @@ namespace HTTPCache {
static constexpr u32 CACHE_VERSION = 1;
static void QueueDownload(std::string_view url, FetchCallback callback, Error* error,
std::unique_lock<std::mutex>&& lock);
std::unique_lock<Threading::Mutex>&& lock);
static void DownloadCallback(const std::string& url, s32 status_code, const Error& error,
const std::string& content_type, const HTTPDownloader::RequestData& data);
@ -41,7 +42,7 @@ struct Locals
{
ObjectArchive cache_archive;
std::deque<std::pair<std::string, FetchCallback>> pending_downloads;
std::mutex pending_downloads_lock;
Threading::Mutex pending_downloads_lock;
std::once_flag cache_open_flag;
};
@ -179,7 +180,7 @@ HTTPCache::LookupResult HTTPCache::LookupOrFetch(std::string_view url, Error* er
}
void HTTPCache::QueueDownload(std::string_view url, FetchCallback callback, Error* error,
std::unique_lock<std::mutex>&& lock)
std::unique_lock<Threading::Mutex>&& lock)
{
// do we already have a request?
const bool has_request =

@ -112,7 +112,7 @@ struct Request
static void StartOrAddRequest(Request* req);
static u32 LockedGetActiveRequestCount();
static void LockedPollRequests(std::unique_lock<std::mutex>& lock);
static void LockedPollRequests(std::unique_lock<Threading::Mutex>& lock);
// Platform specific implementations
@ -156,7 +156,7 @@ struct ALIGN_TO_CACHE_LINE Locals
/// Guards pending_http_requests. Also used to serialise callback dispatch in
/// LockedPollRequests() — the lock is released around each callback invocation.
std::mutex pending_http_request_lock;
Threading::Mutex pending_http_request_lock;
/// All requests that are either queued (Pending) or actively in-flight
/// (Started / Receiving). Requests are removed just before their callback fires.
@ -180,7 +180,7 @@ struct ALIGN_TO_CACHE_LINE Locals
/// Protected by worker_queue_mutex; woken with curl_multi_wakeup().
ALIGN_TO_CACHE_LINE std::deque<std::pair<QueueAction, Request*>> worker_queue;
std::atomic_bool worker_thread_shutdown{false}; ///< Set to true to signal the worker to exit.
std::mutex worker_queue_mutex;
Threading::Mutex worker_queue_mutex;
#endif
};
@ -259,7 +259,7 @@ void HTTPDownloader::StartOrAddRequest(Request* req)
// re-acquisition to handle requests added or removed by the callback.
//
// Notifies the host when the queue transitions from non-empty to empty.
void HTTPDownloader::LockedPollRequests(std::unique_lock<std::mutex>& lock)
void HTTPDownloader::LockedPollRequests(std::unique_lock<Threading::Mutex>& lock)
{
if (s_locals.pending_http_requests.empty())
return;
@ -1094,7 +1094,7 @@ void HTTPDownloader::Shutdown()
if (s_locals.worker_thread.Joinable())
{
{
const std::unique_lock lock(s_locals.worker_queue_mutex);
const std::lock_guard lock(s_locals.worker_queue_mutex);
s_locals.worker_thread_shutdown.store(true, std::memory_order_release);
// Should break the curl_multi_poll wait.
@ -1202,7 +1202,7 @@ void HTTPDownloader::WorkerThreadEntryPoint()
// Must only be called from the worker thread.
void HTTPDownloader::ProcessQueuedActions()
{
const std::unique_lock lock(s_locals.worker_queue_mutex);
const std::lock_guard lock(s_locals.worker_queue_mutex);
while (!s_locals.worker_queue.empty())
{
const auto& [action, request] = s_locals.worker_queue.front();
@ -1338,7 +1338,7 @@ bool HTTPDownloader::StartRequest(Request* req)
req->last_update_time = req->start_time;
// Add to action queue for worker thread to process
const std::unique_lock lock(s_locals.worker_queue_mutex);
const std::lock_guard lock(s_locals.worker_queue_mutex);
s_locals.worker_queue.emplace_back(QueueAction::Add, req);
// Wake up worker thread
@ -1355,7 +1355,7 @@ void HTTPDownloader::CloseRequest(Request* req)
DebugAssert(req->handle);
// Add to action queue for worker thread to process
const std::unique_lock lock(s_locals.worker_queue_mutex);
const std::lock_guard lock(s_locals.worker_queue_mutex);
s_locals.worker_queue.emplace_back(QueueAction::RemoveAndDelete, req);
// Wake up worker thread

@ -110,7 +110,7 @@ static void AddOSDMessage(OSDMessageType type, std::string key, OSDMessageIconTy
std::string title, std::string message);
static void RemoveKeyedOSDMessage(std::string key);
static void ClearOSDMessages();
static void UpdateOSDMessageRunIdle(const std::unique_lock<std::mutex>& lock);
static void UpdateOSDMessageRunIdle(const std::unique_lock<Threading::Mutex>& lock);
static void AcquirePendingOSDMessages(Timer::Value current_time);
static void DrawOSDMessages(Timer::Value current_time);
static void CreateSoftwareCursorTextures();
@ -216,7 +216,7 @@ struct ALIGN_TO_CACHE_LINE State
std::array<ImGuiManager::SoftwareCursor, InputManager::MAX_SOFTWARE_CURSORS> software_cursors = {};
std::deque<PostedOSDMessage> osd_posted_messages;
std::mutex osd_messages_lock;
Threading::Mutex osd_messages_lock;
// Read by both threads
ALIGN_TO_CACHE_LINE ImGuiContext* imgui_context = nullptr;
@ -1101,7 +1101,7 @@ void ImGuiManager::AddOSDMessage(OSDMessageType type, std::string key, OSDMessag
UpdateOSDMessageRunIdle(lock);
}
void ImGuiManager::UpdateOSDMessageRunIdle(const std::unique_lock<std::mutex>& lock)
void ImGuiManager::UpdateOSDMessageRunIdle(const std::unique_lock<Threading::Mutex>& lock)
{
static constexpr auto cb = []() {
VideoThread::SetRunIdleReason(VideoThread::RunIdleReason::OSDMessagesActive,

@ -8,6 +8,7 @@
#include "common/log.h"
#include "common/path.h"
#include "common/string_util.h"
#include "common/threading.h"
#include <algorithm>
#include <cstring>
@ -17,7 +18,7 @@ LOG_CHANNEL(Settings);
// To prevent races between saving and loading settings, particularly with game settings,
// we only allow one ini to be parsed at any point in time.
static std::mutex s_ini_load_save_mutex;
static Threading::Mutex s_ini_load_save_mutex;
INISettingsInterface::INISettingsInterface() = default;
@ -171,7 +172,7 @@ bool INISettingsInterface::Load(Error* error)
return false;
}
std::unique_lock lock(s_ini_load_save_mutex);
std::lock_guard lock(s_ini_load_save_mutex);
std::optional<std::string> file_data = FileSystem::ReadFileToString(m_path.c_str(), error);
if (!file_data.has_value())

@ -22,7 +22,6 @@
#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <cstring>
#include <deque>
#include <limits>
@ -159,11 +158,11 @@ protected:
return (static_cast<u32>(m_audio_buffer.size()) / AUDIO_CHANNELS);
}
void ProcessFramePendingMap(std::unique_lock<std::mutex>& lock);
void ProcessAllInFlightFrames(std::unique_lock<std::mutex>& lock);
void ProcessFramePendingMap(std::unique_lock<Threading::Mutex>& lock);
void ProcessAllInFlightFrames(std::unique_lock<Threading::Mutex>& lock);
void EncoderThreadEntryPoint();
void StartEncoderThread();
void StopEncoderThread(std::unique_lock<std::mutex>& lock);
void StopEncoderThread(std::unique_lock<Threading::Mutex>& lock);
void DeleteOutputFile();
void UpdateOutputSize(u64 size);
@ -175,9 +174,9 @@ protected:
std::string_view video_codec, u32 video_bitrate, std::string_view video_codec_args,
bool capture_audio, std::string_view audio_codec, u32 audio_bitrate,
std::string_view audio_codec_args, Error* error) = 0;
virtual bool InternalEndCapture(std::unique_lock<std::mutex>& lock, Error* error);
virtual bool InternalEndCapture(std::unique_lock<Threading::Mutex>& lock, Error* error);
mutable std::mutex m_lock;
mutable Threading::Mutex m_lock;
std::string m_path;
std::atomic_bool m_capturing{false};
std::atomic_bool m_encoding_error{false};
@ -199,8 +198,8 @@ protected:
float m_encoder_thread_usage = 0.0f;
float m_encoder_thread_time = 0.0f;
std::condition_variable m_frame_ready_cv;
std::condition_variable m_frame_encoded_cv;
Threading::ConditionVariable m_frame_ready_cv;
Threading::ConditionVariable m_frame_encoded_cv;
std::array<PendingFrame, MAX_PENDING_FRAMES> m_pending_frames = {};
u32 m_pending_frames_pos = 0;
u32 m_frames_pending_map = 0;
@ -342,7 +341,7 @@ bool MediaCaptureBase::DeliverVideoFrame(GPUTexture* stex)
return true;
}
void MediaCaptureBase::ProcessFramePendingMap(std::unique_lock<std::mutex>& lock)
void MediaCaptureBase::ProcessFramePendingMap(std::unique_lock<Threading::Mutex>& lock)
{
DebugAssert(m_frames_pending_map > 0);
@ -424,7 +423,7 @@ void MediaCaptureBase::StartEncoderThread()
m_encoder_thread.Start([this]() { EncoderThreadEntryPoint(); });
}
void MediaCaptureBase::StopEncoderThread(std::unique_lock<std::mutex>& lock)
void MediaCaptureBase::StopEncoderThread(std::unique_lock<Threading::Mutex>& lock)
{
// Thread will exit when s_capturing is false.
DebugAssert(!m_capturing.load(std::memory_order_acquire));
@ -441,7 +440,7 @@ void MediaCaptureBase::StopEncoderThread(std::unique_lock<std::mutex>& lock)
}
}
void MediaCaptureBase::ProcessAllInFlightFrames(std::unique_lock<std::mutex>& lock)
void MediaCaptureBase::ProcessAllInFlightFrames(std::unique_lock<Threading::Mutex>& lock)
{
while (m_frames_pending_map > 0)
ProcessFramePendingMap(lock);
@ -517,7 +516,7 @@ bool MediaCaptureBase::DeliverAudioFrames(const s16* frames, u32 num_frames)
return true;
}
bool MediaCaptureBase::InternalEndCapture(std::unique_lock<std::mutex>& lock, Error* error)
bool MediaCaptureBase::InternalEndCapture(std::unique_lock<Threading::Mutex>& lock, Error* error)
{
DebugAssert(m_capturing.load(std::memory_order_acquire));
@ -722,7 +721,7 @@ protected:
u32 video_bitrate, std::string_view video_codec_args, bool capture_audio,
std::string_view audio_codec, u32 audio_bitrate, std::string_view audio_codec_args,
Error* error) override;
bool InternalEndCapture(std::unique_lock<std::mutex>& lock, Error* error) override;
bool InternalEndCapture(std::unique_lock<Threading::Mutex>& lock, Error* error) override;
private:
class CountingByteStream;
@ -1171,7 +1170,7 @@ bool MediaCaptureMF::InternalBeginCapture(float fps, float aspect, u32 sample_ra
return true;
}
bool MediaCaptureMF::InternalEndCapture(std::unique_lock<std::mutex>& lock, Error* error)
bool MediaCaptureMF::InternalEndCapture(std::unique_lock<Threading::Mutex>& lock, Error* error)
{
HRESULT hr = MediaCaptureBase::InternalEndCapture(lock, error) ? S_OK : E_FAIL;
@ -2185,7 +2184,7 @@ protected:
u32 video_bitrate, std::string_view video_codec_args, bool capture_audio,
std::string_view audio_codec, u32 audio_bitrate, std::string_view audio_codec_args,
Error* error) override;
bool InternalEndCapture(std::unique_lock<std::mutex>& lock, Error* error) override;
bool InternalEndCapture(std::unique_lock<Threading::Mutex>& lock, Error* error) override;
private:
static void SetAVError(Error* error, std::string_view prefix, int errnum);
@ -3085,7 +3084,7 @@ bool MediaCaptureFFmpeg::InternalBeginCapture(float fps, float aspect, u32 sampl
return true;
}
bool MediaCaptureFFmpeg::InternalEndCapture(std::unique_lock<std::mutex>& lock, Error* error)
bool MediaCaptureFFmpeg::InternalEndCapture(std::unique_lock<Threading::Mutex>& lock, Error* error)
{
if (!MediaCaptureBase::InternalEndCapture(lock, error))
return false;

@ -20,6 +20,7 @@
#include "metal_stream_buffer.h"
#include "window_info.h"
#include "common/threading.h"
#include "common/timer.h"
#include <atomic>
@ -417,7 +418,7 @@ private:
id<MTLDevice> m_device;
id<MTLCommandQueue> m_queue;
std::mutex m_fence_mutex;
Threading::Mutex m_fence_mutex;
u64 m_current_fence_counter = 0;
std::atomic<u64> m_completed_fence_counter{0};
std::deque<std::pair<u64, id>> m_cleanup_objects; // [fence_counter, object]

@ -569,7 +569,7 @@ bool MetalDevice::SetGPUTimingEnabled(bool enabled)
if (m_gpu_timing_enabled == enabled)
return true;
std::unique_lock lock(m_fence_mutex);
std::lock_guard lock(m_fence_mutex);
m_gpu_timing_enabled = enabled;
m_accumulated_gpu_time = 0.0;
m_last_gpu_time_end = 0.0;
@ -578,7 +578,7 @@ bool MetalDevice::SetGPUTimingEnabled(bool enabled)
float MetalDevice::GetAndResetAccumulatedGPUTime()
{
std::unique_lock lock(m_fence_mutex);
std::lock_guard lock(m_fence_mutex);
return std::exchange(m_accumulated_gpu_time, 0.0) * 1000.0;
}
@ -2656,7 +2656,7 @@ void MetalDevice::CreateCommandBuffer()
void MetalDevice::CommandBufferCompletedOffThread(id<MTLCommandBuffer> buffer, u64 fence_counter)
{
std::unique_lock lock(m_fence_mutex);
std::lock_guard lock(m_fence_mutex);
m_completed_fence_counter.store(std::max(m_completed_fence_counter.load(std::memory_order_acquire), fence_counter),
std::memory_order_release);

@ -71,7 +71,7 @@ constinit const std::string_view ObjectArchive::ERROR_DESCRIPTION_ALREADY_EXISTS
void ObjectArchive::Close()
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
LockedClose();
}
@ -93,7 +93,7 @@ void ObjectArchive::LockedClose()
bool ObjectArchive::Clear(Error* error)
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
if (!IsOpen())
return true;
@ -117,13 +117,13 @@ bool ObjectArchive::Clear(Error* error)
size_t ObjectArchive::GetSize() const
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
return m_index.size();
}
bool ObjectArchive::OpenPath(std::string_view base_path, u32 data_version, Error* error, bool* was_invalidated)
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
LockedClose();
@ -219,7 +219,7 @@ bool ObjectArchive::OpenPath(std::string_view base_path, u32 data_version, Error
bool ObjectArchive::OpenFile(std::FILE* index_file, std::FILE* blob_file, u32 data_version, Error* error)
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
LockedClose();
@ -236,7 +236,7 @@ bool ObjectArchive::OpenFile(std::FILE* index_file, std::FILE* blob_file, u32 da
bool ObjectArchive::CreateFile(std::FILE* index_file, std::FILE* blob_file, u32 data_version, Error* error)
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
LockedClose();
@ -361,7 +361,7 @@ std::optional<ObjectArchive::ObjectData> ObjectArchive::Lookup(KeySpan key, Erro
u32 uncompressed_size;
{
// Minimize the time locked, only the lookup+read, not the decompress.
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
if (!IsOpen()) [[unlikely]]
{
Error::SetStringView(error, ERROR_DESCRIPTION_NOT_OPEN);
@ -407,7 +407,7 @@ bool ObjectArchive::Contains(KeySpan key) const
if (key.empty() || key.size() > MAX_KEY_SIZE) [[unlikely]]
return false;
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
if (!IsOpen()) [[unlikely]]
return false;
@ -427,7 +427,7 @@ bool ObjectArchive::Insert(KeySpan key, std::span<const u8> data, CompressType c
// Lookup once before compress, and again afterwards because we don't hold the lock.
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
if (!IsOpen()) [[unlikely]]
{
Error::SetStringView(error, ERROR_DESCRIPTION_NOT_OPEN);
@ -463,7 +463,7 @@ bool ObjectArchive::Insert(KeySpan key, std::span<const u8> data, CompressType c
}
// See above.
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
if (!IsOpen()) [[unlikely]]
{
Error::SetStringView(error, ERROR_DESCRIPTION_NOT_OPEN);
@ -520,7 +520,7 @@ bool ObjectArchive::Insert(KeySpan key, std::span<const u8> data, CompressType c
u64 ObjectArchive::GetTotalObjectSize() const
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
u64 total_size = 0;
for (const CacheIndexData& entry : m_index)
total_size += entry.uncompressed_size;
@ -529,7 +529,7 @@ u64 ObjectArchive::GetTotalObjectSize() const
u64 ObjectArchive::GetTotalSize() const
{
std::unique_lock lock(m_mutex);
std::lock_guard lock(m_mutex);
u64 total_size = 0;
for (const CacheIndexData& entry : m_index)
total_size += entry.compressed_size + sizeof(CacheIndexEntryHeader) + entry.key_size;

@ -6,8 +6,8 @@
#include "compress_helpers.h"
#include "common/heap_array.h"
#include "common/threading.h"
#include <mutex>
#include <optional>
#include <span>
#include <string>
@ -101,5 +101,5 @@ private:
std::FILE* m_index_file = nullptr;
std::FILE* m_blob_file = nullptr;
mutable std::mutex m_mutex;
mutable Threading::Mutex m_mutex;
};

@ -430,7 +430,7 @@ void SocketMultiplexer::AddOpenSocket(std::shared_ptr<BaseSocket> socket)
ERROR_LOG("epoll_ctl() to add socket failed: {}", Error::CreateErrno(errno).GetDescription());
#endif
std::unique_lock lock(m_open_sockets_lock);
std::lock_guard lock(m_open_sockets_mutex);
DebugAssert(m_open_sockets.find(socket->GetDescriptor()) == m_open_sockets.end());
m_open_sockets.emplace(socket->GetDescriptor(), std::move(socket));
}
@ -443,7 +443,7 @@ void SocketMultiplexer::AddClientSocket(std::shared_ptr<BaseSocket> socket)
void SocketMultiplexer::RemoveOpenSocket(BaseSocket* socket)
{
std::unique_lock lock(m_open_sockets_lock);
std::lock_guard lock(m_open_sockets_mutex);
const auto iter = m_open_sockets.find(socket->GetDescriptor());
Assert(iter != m_open_sockets.end());
m_open_sockets.erase(iter);
@ -477,7 +477,7 @@ void SocketMultiplexer::RemoveClientSocket(BaseSocket* socket)
bool SocketMultiplexer::HasAnyOpenSockets()
{
std::unique_lock lock(m_open_sockets_lock);
std::lock_guard lock(m_open_sockets_mutex);
return !m_open_sockets.empty();
}
@ -493,7 +493,7 @@ size_t SocketMultiplexer::GetClientSocketCount()
void SocketMultiplexer::CloseAll()
{
std::unique_lock lock(m_open_sockets_lock);
std::unique_lock lock(m_open_sockets_mutex);
while (!m_open_sockets.empty())
{
@ -511,7 +511,7 @@ void SocketMultiplexer::SetNotificationMask(BaseSocket* socket, SocketDescriptor
if (epoll_ctl(m_epoll_fd, EPOLL_CTL_MOD, descriptor, &ev) != 0) [[unlikely]]
ERROR_LOG("epoll_ctl() for events 0x{:x} failed: {}", events, Error::CreateErrno(errno).GetDescription());
#else
std::unique_lock lock(m_poll_array_lock);
std::lock_guard lock(m_poll_array_mutex);
size_t free_slot = m_poll_array_active_size;
for (size_t i = 0; i < m_poll_array_active_size; i++)
{
@ -570,7 +570,7 @@ bool SocketMultiplexer::PollEventsWithTimeout(u32 milliseconds)
reinterpret_cast<PendingSocketPair*>(alloca(sizeof(PendingSocketPair) * static_cast<size_t>(nevents)));
size_t num_triggered_sockets = 0;
{
std::unique_lock open_lock(m_open_sockets_lock);
std::lock_guard open_lock(m_open_sockets_mutex);
for (int i = 0; i < nevents; i++)
{
const epoll_event& ev = events[i];
@ -609,7 +609,7 @@ bool SocketMultiplexer::PollEventsWithTimeout(u32 milliseconds)
return true;
#else
std::unique_lock lock(m_poll_array_lock);
std::unique_lock lock(m_poll_array_mutex);
if (m_poll_array_active_size == 0)
return false;
@ -623,7 +623,7 @@ bool SocketMultiplexer::PollEventsWithTimeout(u32 milliseconds)
reinterpret_cast<PendingSocketPair*>(alloca(sizeof(PendingSocketPair) * static_cast<size_t>(res)));
size_t num_triggered_sockets = 0;
{
std::unique_lock open_lock(m_open_sockets_lock);
std::lock_guard open_lock(m_open_sockets_mutex);
for (size_t i = 0; i < m_poll_array_active_size; i++)
{
const pollfd& pfd = m_poll_array[i];

@ -157,13 +157,13 @@ private:
#ifdef __linux__
int m_epoll_fd = -1;
#else
std::mutex m_poll_array_lock;
Threading::Mutex m_poll_array_mutex;
pollfd* m_poll_array = nullptr;
size_t m_poll_array_active_size = 0;
size_t m_poll_array_max_size = 0;
#endif
std::mutex m_open_sockets_lock;
Threading::Mutex m_open_sockets_mutex;
SocketMap m_open_sockets;
std::atomic_size_t m_client_socket_count{0};
};

@ -14,6 +14,7 @@
#include "common/dynamic_library.h"
#include "common/error.h"
#include "common/log.h"
#include "common/threading.h"
#include <cstdarg>
#include <cstdio>
@ -63,7 +64,7 @@ struct Locals
WindowInfoType window_type = WindowInfoType::Surfaceless;
bool is_debug_instance = false;
std::mutex mutex;
Threading::Mutex mutex;
};
static const DynamicLibrary::OptionalSymbolTable s_vulkan_module_entry_points[] = {

Loading…
Cancel
Save