mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-06 15:01:28 +12:00
Cleanup semaphore<> (sema.h) and mutex.h (shared_mutex)
Remove semaphore_lock and writer_lock classes, replace with std::lock_guard Change semaphore<> interface to Lockable (+ exotic try_unlock method)
This commit is contained in:
parent
5e556a87ff
commit
ca5158a03e
50 changed files with 283 additions and 382 deletions
|
@ -217,7 +217,7 @@ std::shared_ptr<fs::device_base> fs::device_manager::get_device(const std::strin
|
|||
|
||||
std::shared_ptr<fs::device_base> fs::device_manager::set_device(const std::string& name, const std::shared_ptr<device_base>& device)
|
||||
{
|
||||
writer_lock lock(m_mutex);
|
||||
std::lock_guard lock(m_mutex);
|
||||
|
||||
return m_map[name] = device;
|
||||
}
|
||||
|
|
|
@ -177,7 +177,7 @@ struct MemoryManager : llvm::RTDyldMemoryManager
|
|||
if ((u64)s_memory > 0x80000000 - s_memory_size ? (u64)addr - (u64)s_memory >= s_memory_size : addr >= 0x80000000)
|
||||
{
|
||||
// Lock memory manager
|
||||
writer_lock lock(s_mutex);
|
||||
std::lock_guard lock(s_mutex);
|
||||
|
||||
// Allocate memory for trampolines
|
||||
if (!m_tramps)
|
||||
|
@ -213,7 +213,7 @@ struct MemoryManager : llvm::RTDyldMemoryManager
|
|||
u8* allocateCodeSection(std::uintptr_t size, uint align, uint sec_id, llvm::StringRef sec_name) override
|
||||
{
|
||||
// Lock memory manager
|
||||
writer_lock lock(s_mutex);
|
||||
std::lock_guard lock(s_mutex);
|
||||
|
||||
// Simple allocation
|
||||
const u64 next = ::align((u64)s_next + size, 4096);
|
||||
|
@ -234,7 +234,7 @@ struct MemoryManager : llvm::RTDyldMemoryManager
|
|||
u8* allocateDataSection(std::uintptr_t size, uint align, uint sec_id, llvm::StringRef sec_name, bool is_ro) override
|
||||
{
|
||||
// Lock memory manager
|
||||
writer_lock lock(s_mutex);
|
||||
std::lock_guard lock(s_mutex);
|
||||
|
||||
// Simple allocation
|
||||
const u64 next = ::align((u64)s_next + size, 4096);
|
||||
|
@ -259,7 +259,7 @@ struct MemoryManager : llvm::RTDyldMemoryManager
|
|||
bool finalizeMemory(std::string* = nullptr) override
|
||||
{
|
||||
// Lock memory manager
|
||||
writer_lock lock(s_mutex);
|
||||
std::lock_guard lock(s_mutex);
|
||||
|
||||
// TODO: make only read-only sections read-only
|
||||
//#ifdef _WIN32
|
||||
|
@ -277,7 +277,7 @@ struct MemoryManager : llvm::RTDyldMemoryManager
|
|||
{
|
||||
#ifdef _WIN32
|
||||
// Lock memory manager
|
||||
writer_lock lock(s_mutex);
|
||||
std::lock_guard lock(s_mutex);
|
||||
|
||||
// Use s_memory as a BASE, compute the difference
|
||||
const u64 unwind_diff = (u64)addr - (u64)s_memory;
|
||||
|
@ -427,7 +427,7 @@ struct EventListener : llvm::JITEventListener
|
|||
}
|
||||
|
||||
// Lock memory manager
|
||||
writer_lock lock(s_mutex);
|
||||
std::lock_guard lock(s_mutex);
|
||||
|
||||
// Use s_memory as a BASE, compute the difference
|
||||
const u64 code_diff = (u64)m_mem.m_code_addr - (u64)s_memory;
|
||||
|
@ -649,7 +649,7 @@ u64 jit_compiler::get(const std::string& name)
|
|||
std::unordered_map<std::string, u64> jit_compiler::add(std::unordered_map<std::string, std::string> data)
|
||||
{
|
||||
// Lock memory manager
|
||||
writer_lock lock(s_mutex);
|
||||
std::lock_guard lock(s_mutex);
|
||||
|
||||
std::unordered_map<std::string, u64> result;
|
||||
|
||||
|
|
|
@ -188,7 +188,7 @@ namespace logs
|
|||
|
||||
void reset()
|
||||
{
|
||||
semaphore_lock lock(g_mutex);
|
||||
std::lock_guard lock(g_mutex);
|
||||
|
||||
for (auto&& pair : get_logger()->channels)
|
||||
{
|
||||
|
@ -198,7 +198,7 @@ namespace logs
|
|||
|
||||
void set_level(const std::string& ch_name, level value)
|
||||
{
|
||||
semaphore_lock lock(g_mutex);
|
||||
std::lock_guard lock(g_mutex);
|
||||
|
||||
get_logger()->channels[ch_name].set_level(value);
|
||||
}
|
||||
|
@ -208,7 +208,7 @@ namespace logs
|
|||
{
|
||||
if (!g_init)
|
||||
{
|
||||
semaphore_lock lock(g_mutex);
|
||||
std::lock_guard lock(g_mutex);
|
||||
get_logger()->messages.clear();
|
||||
g_init = true;
|
||||
}
|
||||
|
@ -224,7 +224,7 @@ void logs::listener::add(logs::listener* _new)
|
|||
// Get first (main) listener
|
||||
listener* lis = get_logger();
|
||||
|
||||
semaphore_lock lock(g_mutex);
|
||||
std::lock_guard lock(g_mutex);
|
||||
|
||||
// Install new listener at the end of linked list
|
||||
while (lis->m_next || !lis->m_next.compare_and_swap_test(nullptr, _new))
|
||||
|
@ -247,7 +247,7 @@ void logs::message::broadcast(const char* fmt, const fmt_type_info* sup, const u
|
|||
// Register channel
|
||||
if (ch->enabled == level::_uninit)
|
||||
{
|
||||
semaphore_lock lock(g_mutex);
|
||||
std::lock_guard lock(g_mutex);
|
||||
|
||||
auto& info = get_logger()->channels[ch->name];
|
||||
|
||||
|
@ -278,7 +278,7 @@ void logs::message::broadcast(const char* fmt, const fmt_type_info* sup, const u
|
|||
|
||||
if (!g_init)
|
||||
{
|
||||
semaphore_lock lock(g_mutex);
|
||||
std::lock_guard lock(g_mutex);
|
||||
|
||||
if (!g_init)
|
||||
{
|
||||
|
@ -468,7 +468,7 @@ logs::file_writer::~file_writer()
|
|||
|
||||
bool logs::file_writer::flush(u64 bufv)
|
||||
{
|
||||
semaphore_lock lock(m_m);
|
||||
std::lock_guard lock(m_m);
|
||||
|
||||
const u64 st = +m_out;
|
||||
const u64 end = std::min<u64>((st + s_log_size) & ~(s_log_size - 1), bufv >> 24);
|
||||
|
|
|
@ -1260,7 +1260,7 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
|
||||
auto pf_entries = fxm::get_always<page_fault_event_entries>();
|
||||
{
|
||||
semaphore_lock pf_lock(pf_entries->pf_mutex);
|
||||
std::lock_guard pf_lock(pf_entries->pf_mutex);
|
||||
page_fault_event pf_event{ cpu->id, addr };
|
||||
pf_entries->events.emplace_back(pf_event);
|
||||
}
|
||||
|
@ -1689,7 +1689,7 @@ void thread_ctrl::finalize(std::exception_ptr eptr) noexcept
|
|||
--g_thread_count;
|
||||
|
||||
// Untangle circular reference, set exception
|
||||
semaphore_lock{m_mutex}, m_self.reset(), m_exception = eptr;
|
||||
std::lock_guard{m_mutex}, m_self.reset(), m_exception = eptr;
|
||||
|
||||
// Signal joining waiters
|
||||
m_jcv.notify_all();
|
||||
|
@ -1715,7 +1715,7 @@ bool thread_ctrl::_wait_for(u64 usec)
|
|||
|
||||
void unlock()
|
||||
{
|
||||
ref.post();
|
||||
ref.unlock();
|
||||
}
|
||||
}
|
||||
_lock{_this->m_mutex};
|
||||
|
@ -1741,7 +1741,7 @@ bool thread_ctrl::_wait_for(u64 usec)
|
|||
}
|
||||
|
||||
// Lock (semaphore)
|
||||
_this->m_mutex.wait();
|
||||
_this->m_mutex.lock();
|
||||
|
||||
// Double-check the value
|
||||
if (u32 sig = _this->m_signal.load())
|
||||
|
@ -1754,7 +1754,7 @@ bool thread_ctrl::_wait_for(u64 usec)
|
|||
if (sig & 1)
|
||||
{
|
||||
_this->m_signal &= ~1;
|
||||
_this->m_mutex.post();
|
||||
_this->m_mutex.unlock();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1769,7 +1769,7 @@ bool thread_ctrl::_wait_for(u64 usec)
|
|||
{
|
||||
std::exception_ptr ex = std::exchange(m_exception, std::exception_ptr{});
|
||||
m_signal &= ~3;
|
||||
m_mutex.post();
|
||||
m_mutex.unlock();
|
||||
std::rethrow_exception(std::move(ex));
|
||||
}
|
||||
|
||||
|
@ -1778,8 +1778,8 @@ void thread_ctrl::_notify(cond_variable thread_ctrl::* ptr)
|
|||
// Optimized lock + unlock
|
||||
if (!m_mutex.get())
|
||||
{
|
||||
m_mutex.wait();
|
||||
m_mutex.post();
|
||||
m_mutex.lock();
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
||||
(this->*ptr).notify_one();
|
||||
|
@ -1804,13 +1804,13 @@ thread_ctrl::~thread_ctrl()
|
|||
|
||||
std::exception_ptr thread_ctrl::get_exception() const
|
||||
{
|
||||
semaphore_lock lock(m_mutex);
|
||||
std::lock_guard lock(m_mutex);
|
||||
return m_exception;
|
||||
}
|
||||
|
||||
void thread_ctrl::set_exception(std::exception_ptr ptr)
|
||||
{
|
||||
semaphore_lock lock(m_mutex);
|
||||
std::lock_guard lock(m_mutex);
|
||||
m_exception = ptr;
|
||||
|
||||
if (m_exception)
|
||||
|
@ -1830,7 +1830,7 @@ void thread_ctrl::join()
|
|||
//verify("thread_ctrl::join" HERE), WaitForSingleObjectEx((HANDLE)m_thread.load(), -1, false) == WAIT_OBJECT_0;
|
||||
#endif
|
||||
|
||||
semaphore_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_mutex);
|
||||
|
||||
while (m_self)
|
||||
{
|
||||
|
@ -1888,14 +1888,14 @@ void thread_ctrl::test()
|
|||
|
||||
if (_this->m_signal & 2)
|
||||
{
|
||||
_this->m_mutex.wait();
|
||||
_this->m_mutex.lock();
|
||||
|
||||
if (_this->m_exception)
|
||||
{
|
||||
_this->_throw();
|
||||
}
|
||||
|
||||
_this->m_mutex.post();
|
||||
_this->m_mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include "types.h"
|
||||
#include "Atomic.h"
|
||||
|
||||
|
@ -113,25 +114,13 @@ class reader_lock final
|
|||
shared_mutex& m_mutex;
|
||||
bool m_upgraded = false;
|
||||
|
||||
void lock()
|
||||
{
|
||||
m_upgraded ? m_mutex.lock() : m_mutex.lock_shared();
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
m_upgraded ? m_mutex.unlock() : m_mutex.unlock_shared();
|
||||
}
|
||||
|
||||
friend class cond_variable;
|
||||
|
||||
public:
|
||||
reader_lock(const reader_lock&) = delete;
|
||||
|
||||
explicit reader_lock(shared_mutex& mutex)
|
||||
: m_mutex(mutex)
|
||||
{
|
||||
lock();
|
||||
m_mutex.lock_shared();
|
||||
}
|
||||
|
||||
// One-way lock upgrade
|
||||
|
@ -146,39 +135,7 @@ public:
|
|||
|
||||
~reader_lock()
|
||||
{
|
||||
unlock();
|
||||
}
|
||||
};
|
||||
|
||||
// Simplified exclusive (writer) lock implementation.
|
||||
class writer_lock final
|
||||
{
|
||||
shared_mutex& m_mutex;
|
||||
|
||||
void lock()
|
||||
{
|
||||
m_mutex.lock();
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
||||
friend class cond_variable;
|
||||
|
||||
public:
|
||||
writer_lock(const writer_lock&) = delete;
|
||||
|
||||
explicit writer_lock(shared_mutex& mutex)
|
||||
: m_mutex(mutex)
|
||||
{
|
||||
lock();
|
||||
}
|
||||
|
||||
~writer_lock()
|
||||
{
|
||||
unlock();
|
||||
m_upgraded ? m_mutex.unlock() : m_mutex.unlock_shared();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -188,18 +145,6 @@ class safe_reader_lock final
|
|||
shared_mutex& m_mutex;
|
||||
bool m_is_owned;
|
||||
|
||||
void lock()
|
||||
{
|
||||
m_mutex.lock_shared();
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
m_mutex.unlock_shared();
|
||||
}
|
||||
|
||||
friend class cond_variable;
|
||||
|
||||
public:
|
||||
safe_reader_lock(const safe_reader_lock&) = delete;
|
||||
|
||||
|
@ -215,18 +160,6 @@ class safe_writer_lock final
|
|||
bool m_is_owned;
|
||||
bool m_is_upgraded;
|
||||
|
||||
void lock()
|
||||
{
|
||||
m_mutex.lock();
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
||||
friend class cond_variable;
|
||||
|
||||
public:
|
||||
safe_writer_lock(const safe_writer_lock&) = delete;
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include "types.h"
|
||||
#include "Atomic.h"
|
||||
|
||||
|
@ -13,8 +14,6 @@ class semaphore_base
|
|||
|
||||
void imp_post(s32 _old);
|
||||
|
||||
friend class semaphore_lock;
|
||||
|
||||
protected:
|
||||
explicit constexpr semaphore_base(s32 value)
|
||||
: m_value{value}
|
||||
|
@ -73,36 +72,36 @@ class semaphore final : public semaphore_base
|
|||
public:
|
||||
// Default constructor (recommended)
|
||||
constexpr semaphore()
|
||||
: base{Def}
|
||||
: base(Def)
|
||||
{
|
||||
}
|
||||
|
||||
// Explicit value constructor (not recommended)
|
||||
explicit constexpr semaphore(s32 value)
|
||||
: base{value}
|
||||
: base(value)
|
||||
{
|
||||
}
|
||||
|
||||
// Obtain a semaphore
|
||||
void wait()
|
||||
void lock()
|
||||
{
|
||||
return base::wait();
|
||||
}
|
||||
|
||||
// Try to obtain a semaphore
|
||||
explicit_bool_t try_wait()
|
||||
bool try_lock()
|
||||
{
|
||||
return base::try_wait();
|
||||
}
|
||||
|
||||
// Return a semaphore
|
||||
void post()
|
||||
void unlock()
|
||||
{
|
||||
return base::post(Max);
|
||||
}
|
||||
|
||||
// Try to return a semaphore
|
||||
explicit_bool_t try_post()
|
||||
bool try_unlock()
|
||||
{
|
||||
return base::try_post(Max);
|
||||
}
|
||||
|
@ -113,34 +112,3 @@ public:
|
|||
return Max;
|
||||
}
|
||||
};
|
||||
|
||||
class semaphore_lock
|
||||
{
|
||||
semaphore_base& m_base;
|
||||
|
||||
void lock()
|
||||
{
|
||||
m_base.wait();
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
m_base.post(INT32_MAX);
|
||||
}
|
||||
|
||||
friend class cond_variable;
|
||||
|
||||
public:
|
||||
explicit semaphore_lock(const semaphore_lock&) = delete;
|
||||
|
||||
semaphore_lock(semaphore_base& sema)
|
||||
: m_base(sema)
|
||||
{
|
||||
lock();
|
||||
}
|
||||
|
||||
~semaphore_lock()
|
||||
{
|
||||
unlock();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -65,7 +65,7 @@ inline int futex(int* uaddr, int futex_op, int val, const timespec* timeout, int
|
|||
|
||||
int operator()(int* uaddr, int futex_op, int val, const timespec* timeout, int*, uint val3)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
std::unique_lock lock(mutex);
|
||||
|
||||
switch (futex_op)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue