mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-04-24 14:24:54 +00:00
Common: Create AtomicUniquePtr template
This commit is contained in:
parent
fc0179c1ea
commit
92c816faad
3 changed files with 53 additions and 0 deletions
51
Source/Core/Common/AtomicUniquePtr.h
Normal file
51
Source/Core/Common/AtomicUniquePtr.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
// Copyright 2025 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
// atomic<shared_ptr<T>> is heavy and has limited compiler availability.
|
||||
// atomic<unique_ptr<T>> isn't feasible.
|
||||
|
||||
// This class provides something similar to a would-be atomic unique_ptr.
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
namespace Common
|
||||
{
|
||||
|
||||
template <typename T>
|
||||
class AtomicUniquePtr
|
||||
{
|
||||
public:
|
||||
using UniquePtr = std::unique_ptr<T>;
|
||||
using RawPtr = T*;
|
||||
|
||||
AtomicUniquePtr() = default;
|
||||
~AtomicUniquePtr() { Store(nullptr); }
|
||||
|
||||
AtomicUniquePtr(const AtomicUniquePtr&) = delete;
|
||||
void operator=(const AtomicUniquePtr&) = delete;
|
||||
|
||||
explicit AtomicUniquePtr(std::nullptr_t) {}
|
||||
explicit AtomicUniquePtr(UniquePtr ptr) { Store(std::move(ptr)); }
|
||||
|
||||
void operator=(std::nullptr_t) { Store(nullptr); }
|
||||
void operator=(UniquePtr ptr) { Store(std::move(ptr)); }
|
||||
|
||||
void Store(UniquePtr desired)
|
||||
{
|
||||
// A unique_ptr is returned and appropriately destructed here.
|
||||
Exchange(std::move(desired));
|
||||
}
|
||||
|
||||
UniquePtr Exchange(UniquePtr desired)
|
||||
{
|
||||
return UniquePtr{m_ptr.exchange(desired.release(), std::memory_order_acq_rel)};
|
||||
}
|
||||
|
||||
private:
|
||||
std::atomic<RawPtr> m_ptr = nullptr;
|
||||
};
|
||||
|
||||
} // namespace Common
|
|
@ -14,6 +14,7 @@ add_library(common
|
|||
Assembler/GekkoParser.cpp
|
||||
Assembler/GekkoParser.h
|
||||
Assert.h
|
||||
AtomicUniquePtr.h
|
||||
BitField.h
|
||||
BitSet.h
|
||||
BitUtils.h
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
<ClInclude Include="Common\Assembler\GekkoIRGen.h" />
|
||||
<ClInclude Include="Common\Assembler\GekkoLexer.h" />
|
||||
<ClInclude Include="Common\Assembler\GekkoParser.h" />
|
||||
<ClInclude Include="Common\AtomicUniquePtr.h" />
|
||||
<ClInclude Include="Common\BitField.h" />
|
||||
<ClInclude Include="Common\BitSet.h" />
|
||||
<ClInclude Include="Common\BitUtils.h" />
|
||||
|
|
Loading…
Add table
Reference in a new issue