mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-12 17:58:37 +12:00
Threads improved, ID manager improved
This commit is contained in:
parent
78bfd54ad4
commit
ca6783ba9a
48 changed files with 1113 additions and 990 deletions
|
@ -1,14 +1,157 @@
|
|||
#include "stdafx.h"
|
||||
#include "IdManager.h"
|
||||
|
||||
std::mutex idm::g_mutex;
|
||||
namespace idm
|
||||
{
|
||||
std::mutex g_mutex;
|
||||
|
||||
std::unordered_map<u32, id_data_t> idm::g_map;
|
||||
idm::map_t g_map;
|
||||
|
||||
u32 idm::g_last_raw_id = 0;
|
||||
u32 g_last_raw_id = 0;
|
||||
|
||||
thread_local u32 idm::g_tls_last_id = 0xdeadbeef;
|
||||
thread_local u32 g_tls_last_id = 0xdeadbeef;
|
||||
}
|
||||
|
||||
std::mutex fxm::g_mutex;
|
||||
namespace fxm
|
||||
{
|
||||
std::mutex g_mutex;
|
||||
|
||||
std::unordered_map<const void*, std::shared_ptr<void>> fxm::g_map;
|
||||
fxm::map_t g_map;
|
||||
}
|
||||
|
||||
void idm::clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{g_mutex};
|
||||
|
||||
// Call recorded id_finalize functions for all IDs
|
||||
for (auto& id : idm::map_t(std::move(g_map)))
|
||||
{
|
||||
(*id.second.type_index)(id.second.data.get());
|
||||
}
|
||||
|
||||
g_last_raw_id = 0;
|
||||
}
|
||||
|
||||
bool idm::check(u32 in_id, id_type_index_t type)
|
||||
{
|
||||
std::lock_guard<std::mutex> 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);
|
||||
|
||||
const auto found = g_map.find(raw_id);
|
||||
|
||||
return found == g_map.end() ? nullptr : found->second.info;
|
||||
}
|
||||
|
||||
std::shared_ptr<void> idm::get(u32 in_id, id_type_index_t type)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
|
||||
const auto found = g_map.find(in_id);
|
||||
|
||||
if (found == g_map.end() || found->second.type_index != type)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return found->second.data;
|
||||
}
|
||||
|
||||
idm::map_t idm::get_all(id_type_index_t type)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
|
||||
idm::map_t result;
|
||||
|
||||
for (auto& id : g_map)
|
||||
{
|
||||
if (id.second.type_index == type)
|
||||
{
|
||||
result.insert(id);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::shared_ptr<void> idm::withdraw(u32 in_id, id_type_index_t type)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
|
||||
const auto found = g_map.find(in_id);
|
||||
|
||||
if (found == g_map.end() || found->second.type_index != type)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto ptr = std::move(found->second.data);
|
||||
|
||||
g_map.erase(found);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
u32 idm::get_count(id_type_index_t type)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
|
||||
u32 result = 0;
|
||||
|
||||
for (auto& id : g_map)
|
||||
{
|
||||
if (id.second.type_index == type)
|
||||
{
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void fxm::clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock{g_mutex};
|
||||
|
||||
// Call recorded id_finalize functions for all IDs
|
||||
for (auto& id : fxm::map_t(std::move(g_map)))
|
||||
{
|
||||
if (id.second) (*id.first)(id.second.get());
|
||||
}
|
||||
}
|
||||
|
||||
bool fxm::check(id_type_index_t type)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
|
||||
const auto found = g_map.find(type);
|
||||
|
||||
return found != g_map.end() && found->second;
|
||||
}
|
||||
|
||||
std::shared_ptr<void> fxm::get(id_type_index_t type)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
|
||||
const auto found = g_map.find(type);
|
||||
|
||||
return found != g_map.end() ? found->second : nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<void> fxm::withdraw(id_type_index_t type)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(g_mutex);
|
||||
|
||||
const auto found = g_map.find(type);
|
||||
|
||||
return found != g_map.end() ? std::move(found->second) : nullptr;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue