SharedMutex improved

This commit is contained in:
Nekotekina 2015-11-30 18:10:17 +03:00
parent b18e337781
commit 82cb8fe5bd
6 changed files with 206 additions and 164 deletions

View file

@ -50,7 +50,7 @@ std::shared_ptr<spu_function_t> SPUDatabase::analyse(const be_t<u32>* ls, u32 en
const u64 key = entry | u64{ ls[entry / 4] } << 32;
{
std::shared_lock<shared_mutex_t> lock(m_mutex);
reader_lock lock(m_mutex);
// Try to find existing function in the database
if (auto func = find(ls + entry / 4, key, max_limit - entry))
@ -59,7 +59,7 @@ std::shared_ptr<spu_function_t> SPUDatabase::analyse(const be_t<u32>* ls, u32 en
}
}
std::lock_guard<shared_mutex_t> lock(m_mutex);
std::lock_guard<shared_mutex> lock(m_mutex);
// Double-check
if (auto func = find(ls + entry / 4, key, max_limit - entry))

View file

@ -259,7 +259,7 @@ struct spu_function_t
// SPU Function Database (must be global or PS3 process-local)
class SPUDatabase final
{
shared_mutex_t m_mutex;
shared_mutex m_mutex;
// All registered functions (uses addr and first instruction as a key)
std::unordered_multimap<u64, std::shared_ptr<spu_function_t>> m_db;

View file

@ -3,7 +3,7 @@
namespace idm
{
std::mutex g_mutex;
shared_mutex g_mutex;
idm::map_t g_map;
@ -14,16 +14,16 @@ namespace idm
namespace fxm
{
std::mutex g_mutex;
shared_mutex g_mutex;
fxm::map_t g_map;
}
void idm::clear()
{
std::lock_guard<std::mutex> lock{g_mutex};
std::lock_guard<shared_mutex> lock(g_mutex);
// Call recorded id_finalize functions for all IDs
// Call recorded finalization functions for all IDs
for (auto& id : idm::map_t(std::move(g_map)))
{
(*id.second.type_index)(id.second.data.get());
@ -34,17 +34,16 @@ void idm::clear()
bool idm::check(u32 in_id, id_type_index_t type)
{
std::lock_guard<std::mutex> lock(g_mutex);
reader_lock lock(g_mutex);
const auto found = g_map.find(in_id);
return found != g_map.end() && found->second.type_index == type;
}
// check if ID exists and return its type or nullptr
const std::type_info* idm::get_type(u32 raw_id)
{
std::lock_guard<std::mutex> lock(g_mutex);
reader_lock lock(g_mutex);
const auto found = g_map.find(raw_id);
@ -53,7 +52,7 @@ const std::type_info* idm::get_type(u32 raw_id)
std::shared_ptr<void> idm::get(u32 in_id, id_type_index_t type)
{
std::lock_guard<std::mutex> lock(g_mutex);
reader_lock lock(g_mutex);
const auto found = g_map.find(in_id);
@ -67,7 +66,7 @@ std::shared_ptr<void> idm::get(u32 in_id, id_type_index_t type)
idm::map_t idm::get_all(id_type_index_t type)
{
std::lock_guard<std::mutex> lock(g_mutex);
reader_lock lock(g_mutex);
idm::map_t result;
@ -84,7 +83,7 @@ idm::map_t idm::get_all(id_type_index_t type)
std::shared_ptr<void> idm::withdraw(u32 in_id, id_type_index_t type)
{
std::lock_guard<std::mutex> lock(g_mutex);
std::lock_guard<shared_mutex> lock(g_mutex);
const auto found = g_map.find(in_id);
@ -102,7 +101,7 @@ std::shared_ptr<void> idm::withdraw(u32 in_id, id_type_index_t type)
u32 idm::get_count(id_type_index_t type)
{
std::lock_guard<std::mutex> lock(g_mutex);
reader_lock lock(g_mutex);
u32 result = 0;
@ -120,9 +119,9 @@ u32 idm::get_count(id_type_index_t type)
void fxm::clear()
{
std::lock_guard<std::mutex> lock{g_mutex};
std::lock_guard<shared_mutex> lock(g_mutex);
// Call recorded id_finalize functions for all IDs
// Call recorded finalization functions for all IDs
for (auto& id : fxm::map_t(std::move(g_map)))
{
if (id.second) (*id.first)(id.second.get());
@ -131,7 +130,7 @@ void fxm::clear()
bool fxm::check(id_type_index_t type)
{
std::lock_guard<std::mutex> lock(g_mutex);
reader_lock lock(g_mutex);
const auto found = g_map.find(type);
@ -140,7 +139,7 @@ bool fxm::check(id_type_index_t type)
std::shared_ptr<void> fxm::get(id_type_index_t type)
{
std::lock_guard<std::mutex> lock(g_mutex);
reader_lock lock(g_mutex);
const auto found = g_map.find(type);
@ -149,7 +148,7 @@ std::shared_ptr<void> fxm::get(id_type_index_t type)
std::shared_ptr<void> fxm::withdraw(id_type_index_t type)
{
std::unique_lock<std::mutex> lock(g_mutex);
std::unique_lock<shared_mutex> lock(g_mutex);
const auto found = g_map.find(type);

View file

@ -1,5 +1,7 @@
#pragma once
#include "Utilities/SharedMutex.h"
#define ID_MANAGER_INCLUDED
// TODO: make id_aux_initialize and id_aux_finalize safer against a possible ODR violation
@ -109,12 +111,12 @@ namespace idm
template<typename T, typename Ptr>
std::shared_ptr<T> add(Ptr&& get_ptr)
{
extern std::mutex g_mutex;
extern shared_mutex g_mutex;
extern idm::map_t g_map;
extern u32 g_last_raw_id;
extern thread_local u32 g_tls_last_id;
std::lock_guard<std::mutex> lock(g_mutex);
std::lock_guard<shared_mutex> lock(g_mutex);
for (u32 raw_id = g_last_raw_id; (raw_id = id_traits<T>::next_id(raw_id)); /**/)
{
@ -283,10 +285,10 @@ namespace fxm
template<typename T, bool Always, typename Ptr>
std::pair<std::shared_ptr<T>, std::shared_ptr<T>> add(Ptr&& get_ptr)
{
extern std::mutex g_mutex;
extern shared_mutex g_mutex;
extern fxm::map_t g_map;
std::lock_guard<std::mutex> lock(g_mutex);
std::lock_guard<shared_mutex> lock(g_mutex);
auto& item = g_map[get_id_type_index<T>()];