rpcsx/rpcs3/Emu/IPC.h

72 lines
1.5 KiB
C
Raw Permalink Normal View History

2016-05-13 16:01:48 +02:00
#pragma once
#include <unordered_map>
#include "util/mutex.h"
2016-05-13 16:01:48 +02:00
2024-12-22 19:59:48 +01:00
#include "util/shared_ptr.hpp"
2016-05-13 16:01:48 +02:00
// IPC manager for objects of type T and IPC keys of type K.
2017-07-24 17:59:48 +02:00
template <typename T, typename K>
2016-05-13 16:01:48 +02:00
class ipc_manager final
{
2024-12-22 19:59:48 +01:00
std::unordered_map<K, shared_ptr<T>> m_map;
2016-05-13 16:01:48 +02:00
mutable shared_mutex m_mutex;
2016-05-13 16:01:48 +02:00
public:
// Add new object if specified ipc_key is not used
// .first: added new object?, .second: what's at m_map[key] after this function if (peek_ptr || added new object) is true
2017-07-24 17:59:48 +02:00
template <typename F>
2024-12-22 19:59:48 +01:00
std::pair<bool, shared_ptr<T>> add(const K& ipc_key, F&& provider, bool peek_ptr = true)
2016-05-13 16:01:48 +02:00
{
std::lock_guard lock(m_mutex);
2016-05-13 16:01:48 +02:00
// Get object location
2024-12-22 19:59:48 +01:00
shared_ptr<T>& ptr = m_map[ipc_key];
const bool existed = ptr.operator bool();
if (!existed)
2016-05-13 16:01:48 +02:00
{
// Call a function which must return the object
ptr = provider();
2016-05-13 16:01:48 +02:00
}
const bool added = !existed && ptr;
2024-12-22 19:59:48 +01:00
return {added, (peek_ptr || added) ? ptr : null_ptr};
2016-05-13 16:01:48 +02:00
}
// Unregister specified ipc_key, may return true even if the object doesn't exist anymore
bool remove(const K& ipc_key)
2016-05-13 16:01:48 +02:00
{
std::lock_guard lock(m_mutex);
2016-05-13 16:01:48 +02:00
return m_map.erase(ipc_key) != 0;
2016-05-13 16:01:48 +02:00
}
// Get object with specified ipc_key
2024-12-22 19:59:48 +01:00
shared_ptr<T> get(const K& ipc_key) const
2016-05-13 16:01:48 +02:00
{
reader_lock lock(m_mutex);
2016-05-13 16:01:48 +02:00
const auto found = m_map.find(ipc_key);
2016-05-13 16:01:48 +02:00
if (found != m_map.end())
2016-05-13 16:01:48 +02:00
{
return found->second;
2016-05-13 16:01:48 +02:00
}
2024-12-22 19:59:48 +01:00
return {};
2016-05-13 16:01:48 +02:00
}
// Check whether the object actually exists
bool check(const K& ipc_key) const
2016-05-13 16:01:48 +02:00
{
reader_lock lock(m_mutex);
2016-05-13 16:01:48 +02:00
const auto found = m_map.find(ipc_key);
2016-05-13 16:01:48 +02:00
return found != m_map.end();
2016-05-13 16:01:48 +02:00
}
};