mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-05 22:41:25 +12:00
utils::shm: Make map_self() atomic (#9578)
This commit is contained in:
parent
e1f95ceb54
commit
c50b9bc4b4
2 changed files with 18 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "util/types.hpp"
|
#include "util/types.hpp"
|
||||||
|
#include "util/atomic.hpp"
|
||||||
|
|
||||||
namespace utils
|
namespace utils
|
||||||
{
|
{
|
||||||
|
@ -52,7 +53,7 @@ namespace utils
|
||||||
#endif
|
#endif
|
||||||
u32 m_size;
|
u32 m_size;
|
||||||
u32 m_flags;
|
u32 m_flags;
|
||||||
void* m_ptr;
|
atomic_t<void*> m_ptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit shm(u32 size, u32 flags = 0);
|
explicit shm(u32 size, u32 flags = 0);
|
||||||
|
@ -84,7 +85,7 @@ namespace utils
|
||||||
// Get memory mapped by map_self()
|
// Get memory mapped by map_self()
|
||||||
u8* get() const
|
u8* get() const
|
||||||
{
|
{
|
||||||
return reinterpret_cast<u8*>(m_ptr);
|
return static_cast<u8*>(+m_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 size() const
|
u32 size() const
|
||||||
|
|
|
@ -218,7 +218,7 @@ namespace utils
|
||||||
shm::shm(u32 size, u32 flags)
|
shm::shm(u32 size, u32 flags)
|
||||||
: m_size(utils::align(size, 0x10000))
|
: m_size(utils::align(size, 0x10000))
|
||||||
, m_flags(flags)
|
, m_flags(flags)
|
||||||
, m_ptr(0)
|
, m_ptr(nullptr)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
m_handle = ::CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, m_size, NULL);
|
m_handle = ::CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, m_size, NULL);
|
||||||
|
@ -362,12 +362,21 @@ namespace utils
|
||||||
|
|
||||||
u8* shm::map_self(protection prot)
|
u8* shm::map_self(protection prot)
|
||||||
{
|
{
|
||||||
if (!m_ptr)
|
void* ptr = m_ptr;
|
||||||
|
|
||||||
|
if (!ptr)
|
||||||
{
|
{
|
||||||
m_ptr = this->map(nullptr, prot);
|
const auto mapped = this->map(nullptr, prot);
|
||||||
|
|
||||||
|
// Install mapped memory
|
||||||
|
if (!m_ptr.compare_exchange(ptr, mapped))
|
||||||
|
{
|
||||||
|
// Mapped already, nothing to do.
|
||||||
|
this->unmap(mapped);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return static_cast<u8*>(m_ptr);
|
return static_cast<u8*>(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shm::unmap(void* ptr) const
|
void shm::unmap(void* ptr) const
|
||||||
|
@ -414,10 +423,9 @@ namespace utils
|
||||||
|
|
||||||
void shm::unmap_self()
|
void shm::unmap_self()
|
||||||
{
|
{
|
||||||
if (m_ptr)
|
if (auto ptr = m_ptr.exchange(nullptr))
|
||||||
{
|
{
|
||||||
this->unmap(m_ptr);
|
this->unmap(ptr);
|
||||||
m_ptr = nullptr;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue