mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 13:37:45 +00:00
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they can safely be used in the Kernel. This includes obtaining a strong reference from a weak reference, which now requires an explicit call to WeakPtr::strong_ref(). Another major change is that Weakable::make_weak_ref() may require the explicit target type. Previously we used reinterpret_cast in WeakPtr, assuming that it can be properly converted. But WeakPtr does not necessarily have the knowledge to be able to do this. Instead, we now ask the class itself to deliver a WeakPtr to the type that we want. Also, WeakLink is no longer specific to a target type. The reason for this is that we want to be able to safely convert e.g. WeakPtr<T> to WeakPtr<U>, and before this we just reinterpret_cast the internal WeakLink<T> to WeakLink<U>, which is a bold assumption that it would actually produce the correct code. Instead, WeakLink now operates on just a raw pointer and we only make those constructors/operators available if we can verify that it can be safely cast. In order to guarantee thread safety, we now use the least significant bit in the pointer for locking purposes. This also means that only properly aligned pointers can be used.
This commit is contained in:
parent
3c1ef744f6
commit
75f61fe3d9
50 changed files with 819 additions and 322 deletions
|
@ -27,9 +27,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/Atomic.h>
|
||||
#include <AK/LogStream.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/Types.h>
|
||||
#ifdef KERNEL
|
||||
# include <Kernel/Arch/i386/CPU.h>
|
||||
#endif
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
@ -54,55 +58,66 @@ ALWAYS_INLINE void unref_if_not_null(T* ptr)
|
|||
|
||||
template<typename T>
|
||||
class NonnullRefPtr {
|
||||
template<typename U, typename P>
|
||||
friend class RefPtr;
|
||||
template<typename U>
|
||||
friend class NonnullRefPtr;
|
||||
template<typename U>
|
||||
friend class WeakPtr;
|
||||
|
||||
public:
|
||||
typedef T ElementType;
|
||||
|
||||
enum AdoptTag { Adopt };
|
||||
|
||||
ALWAYS_INLINE NonnullRefPtr(const T& object)
|
||||
: m_ptr(const_cast<T*>(&object))
|
||||
: m_bits((FlatPtr)&object)
|
||||
{
|
||||
m_ptr->ref();
|
||||
ASSERT(!(m_bits & 1));
|
||||
const_cast<T&>(object).ref();
|
||||
}
|
||||
template<typename U>
|
||||
ALWAYS_INLINE NonnullRefPtr(const U& object)
|
||||
: m_ptr(&const_cast<U&>(object))
|
||||
: m_bits((FlatPtr) static_cast<const T*>(&object))
|
||||
{
|
||||
m_ptr->ref();
|
||||
ASSERT(!(m_bits & 1));
|
||||
const_cast<T&>(static_cast<const T&>(object)).ref();
|
||||
}
|
||||
ALWAYS_INLINE NonnullRefPtr(AdoptTag, T& object)
|
||||
: m_ptr(&object)
|
||||
: m_bits((FlatPtr)&object)
|
||||
{
|
||||
ASSERT(!(m_bits & 1));
|
||||
}
|
||||
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr&& other)
|
||||
: m_ptr(&other.leak_ref())
|
||||
: m_bits((FlatPtr)&other.leak_ref())
|
||||
{
|
||||
ASSERT(!(m_bits & 1));
|
||||
}
|
||||
template<typename U>
|
||||
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr<U>&& other)
|
||||
: m_ptr(&other.leak_ref())
|
||||
: m_bits((FlatPtr)&other.leak_ref())
|
||||
{
|
||||
ASSERT(!(m_bits & 1));
|
||||
}
|
||||
ALWAYS_INLINE NonnullRefPtr(const NonnullRefPtr& other)
|
||||
: m_ptr(const_cast<T*>(other.ptr()))
|
||||
: m_bits((FlatPtr)other.add_ref())
|
||||
{
|
||||
m_ptr->ref();
|
||||
ASSERT(!(m_bits & 1));
|
||||
}
|
||||
template<typename U>
|
||||
ALWAYS_INLINE NonnullRefPtr(const NonnullRefPtr<U>& other)
|
||||
: m_ptr(const_cast<U*>(other.ptr()))
|
||||
: m_bits((FlatPtr)other.add_ref())
|
||||
{
|
||||
m_ptr->ref();
|
||||
ASSERT(!(m_bits & 1));
|
||||
}
|
||||
ALWAYS_INLINE ~NonnullRefPtr()
|
||||
{
|
||||
unref_if_not_null(m_ptr);
|
||||
m_ptr = nullptr;
|
||||
assign(nullptr);
|
||||
#ifdef SANITIZE_PTRS
|
||||
if constexpr (sizeof(T*) == 8)
|
||||
m_ptr = (T*)(0xb0b0b0b0b0b0b0b0);
|
||||
m_bits.store(0xb0b0b0b0b0b0b0b0, AK::MemoryOrder::memory_order_relaxed);
|
||||
else
|
||||
m_ptr = (T*)(0xb0b0b0b0);
|
||||
m_bits.store(0xb0b0b0b0, AK::MemoryOrder::memory_order_relaxed);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -120,100 +135,89 @@ public:
|
|||
|
||||
NonnullRefPtr& operator=(const NonnullRefPtr& other)
|
||||
{
|
||||
NonnullRefPtr ptr(other);
|
||||
swap(ptr);
|
||||
if (this != &other)
|
||||
assign(other.add_ref());
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
NonnullRefPtr& operator=(const NonnullRefPtr<U>& other)
|
||||
{
|
||||
NonnullRefPtr ptr(other);
|
||||
swap(ptr);
|
||||
assign(other.add_ref());
|
||||
return *this;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE NonnullRefPtr& operator=(NonnullRefPtr&& other)
|
||||
{
|
||||
NonnullRefPtr ptr(move(other));
|
||||
swap(ptr);
|
||||
if (this != &other)
|
||||
assign(&other.leak_ref());
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
NonnullRefPtr& operator=(NonnullRefPtr<U>&& other)
|
||||
{
|
||||
NonnullRefPtr ptr(move(other));
|
||||
swap(ptr);
|
||||
assign(&other.leak_ref());
|
||||
return *this;
|
||||
}
|
||||
|
||||
NonnullRefPtr& operator=(const T& object)
|
||||
{
|
||||
NonnullRefPtr ptr(object);
|
||||
swap(ptr);
|
||||
const_cast<T&>(object).ref();
|
||||
assign(const_cast<T*>(&object));
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE T& leak_ref()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return *exchange(m_ptr, nullptr);
|
||||
T* ptr = exchange(nullptr);
|
||||
ASSERT(ptr);
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T* ptr()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE const T* ptr() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T* operator->()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE const T* operator->() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T& operator*()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return *m_ptr;
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE const T& operator*() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return *m_ptr;
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE operator T*()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE operator const T*() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE operator T&()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return *m_ptr;
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
ALWAYS_INLINE operator const T&() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return *m_ptr;
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
|
||||
operator bool() const = delete;
|
||||
|
@ -221,19 +225,113 @@ public:
|
|||
|
||||
void swap(NonnullRefPtr& other)
|
||||
{
|
||||
::swap(m_ptr, other.m_ptr);
|
||||
if (this == &other)
|
||||
return;
|
||||
|
||||
// NOTE: swap is not atomic!
|
||||
T* other_ptr = other.exchange(nullptr);
|
||||
T* ptr = exchange(other_ptr);
|
||||
other.exchange(ptr);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void swap(NonnullRefPtr<U>& other)
|
||||
{
|
||||
::swap(m_ptr, other.m_ptr);
|
||||
// NOTE: swap is not atomic!
|
||||
U* other_ptr = other.exchange(nullptr);
|
||||
T* ptr = exchange(other_ptr);
|
||||
other.exchange(ptr);
|
||||
}
|
||||
|
||||
private:
|
||||
NonnullRefPtr() = delete;
|
||||
|
||||
T* m_ptr { nullptr };
|
||||
ALWAYS_INLINE T* as_ptr() const
|
||||
{
|
||||
return (T*)(m_bits.load(AK::MemoryOrder::memory_order_relaxed) & ~(FlatPtr)1);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T* as_nonnull_ptr() const
|
||||
{
|
||||
T* ptr = (T*)(m_bits.load(AK::MemoryOrder::memory_order_relaxed) & ~(FlatPtr)1);
|
||||
ASSERT(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void do_while_locked(F f) const
|
||||
{
|
||||
#ifdef KERNEL
|
||||
// We don't want to be pre-empted while we have the lock bit set
|
||||
Kernel::ScopedCritical critical;
|
||||
#endif
|
||||
FlatPtr bits;
|
||||
for (;;) {
|
||||
bits = m_bits.fetch_or(1, AK::MemoryOrder::memory_order_acq_rel);
|
||||
if (!(bits & 1))
|
||||
break;
|
||||
#ifdef KERNEL
|
||||
Kernel::Processor::wait_check();
|
||||
#endif
|
||||
}
|
||||
ASSERT(!(bits & 1));
|
||||
f((T*)bits);
|
||||
m_bits.store(bits, AK::MemoryOrder::memory_order_release);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void assign(T* new_ptr)
|
||||
{
|
||||
T* prev_ptr = exchange(new_ptr);
|
||||
unref_if_not_null(prev_ptr);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T* exchange(T* new_ptr)
|
||||
{
|
||||
ASSERT(!((FlatPtr)new_ptr & 1));
|
||||
#ifdef KERNEL
|
||||
// We don't want to be pre-empted while we have the lock bit set
|
||||
Kernel::ScopedCritical critical;
|
||||
#endif
|
||||
// Only exchange while not locked
|
||||
FlatPtr expected = m_bits.load(AK::MemoryOrder::memory_order_relaxed);
|
||||
for (;;) {
|
||||
expected &= ~(FlatPtr)1; // only if lock bit is not set
|
||||
if (m_bits.compare_exchange_strong(expected, (FlatPtr)new_ptr, AK::MemoryOrder::memory_order_acq_rel))
|
||||
break;
|
||||
#ifdef KERNEL
|
||||
Kernel::Processor::wait_check();
|
||||
#endif
|
||||
}
|
||||
ASSERT(!(expected & 1));
|
||||
return (T*)expected;
|
||||
}
|
||||
|
||||
T* add_ref() const
|
||||
{
|
||||
#ifdef KERNEL
|
||||
// We don't want to be pre-empted while we have the lock bit set
|
||||
Kernel::ScopedCritical critical;
|
||||
#endif
|
||||
// Lock the pointer
|
||||
FlatPtr expected = m_bits.load(AK::MemoryOrder::memory_order_relaxed);
|
||||
for (;;) {
|
||||
expected &= ~(FlatPtr)1; // only if lock bit is not set
|
||||
if (m_bits.compare_exchange_strong(expected, expected | 1, AK::MemoryOrder::memory_order_acq_rel))
|
||||
break;
|
||||
#ifdef KERNEL
|
||||
Kernel::Processor::wait_check();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Add a reference now that we locked the pointer
|
||||
ref_if_not_null((T*)expected);
|
||||
|
||||
// Unlock the pointer again
|
||||
m_bits.store(expected, AK::MemoryOrder::memory_order_release);
|
||||
return (T*)expected;
|
||||
}
|
||||
|
||||
mutable Atomic<FlatPtr> m_bits { 0 };
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
|
|
@ -69,26 +69,26 @@ public:
|
|||
|
||||
ALWAYS_INLINE void ref() const
|
||||
{
|
||||
auto old_ref_count = m_ref_count++;
|
||||
auto old_ref_count = m_ref_count.fetch_add(1, AK::MemoryOrder::memory_order_relaxed);
|
||||
ASSERT(old_ref_count > 0);
|
||||
ASSERT(!Checked<RefCountType>::addition_would_overflow(old_ref_count, 1));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE RefCountType ref_count() const
|
||||
{
|
||||
return m_ref_count;
|
||||
return m_ref_count.load(AK::MemoryOrder::memory_order_relaxed);
|
||||
}
|
||||
|
||||
protected:
|
||||
RefCountedBase() { }
|
||||
ALWAYS_INLINE ~RefCountedBase()
|
||||
{
|
||||
ASSERT(m_ref_count == 0);
|
||||
ASSERT(m_ref_count.load(AK::MemoryOrder::memory_order_relaxed) == 0);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE RefCountType deref_base() const
|
||||
{
|
||||
auto old_ref_count = m_ref_count--;
|
||||
auto old_ref_count = m_ref_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
|
||||
ASSERT(old_ref_count > 0);
|
||||
return old_ref_count - 1;
|
||||
}
|
||||
|
|
329
AK/RefPtr.h
329
AK/RefPtr.h
|
@ -26,11 +26,15 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Atomic.h>
|
||||
#include <AK/LogStream.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <AK/Traits.h>
|
||||
#include <AK/Types.h>
|
||||
#ifdef KERNEL
|
||||
# include <Kernel/Arch/i386/CPU.h>
|
||||
#endif
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
@ -39,19 +43,87 @@ class OwnPtr;
|
|||
|
||||
template<typename T>
|
||||
struct RefPtrTraits {
|
||||
static T* as_ptr(FlatPtr bits)
|
||||
ALWAYS_INLINE static T* as_ptr(FlatPtr bits)
|
||||
{
|
||||
return (T*)bits;
|
||||
return (T*)(bits & ~(FlatPtr)1);
|
||||
}
|
||||
|
||||
static FlatPtr as_bits(T* ptr)
|
||||
ALWAYS_INLINE static FlatPtr as_bits(T* ptr)
|
||||
{
|
||||
ASSERT(!((FlatPtr)ptr & 1));
|
||||
return (FlatPtr)ptr;
|
||||
}
|
||||
|
||||
static bool is_null(FlatPtr bits)
|
||||
template<typename U, typename PtrTraits>
|
||||
ALWAYS_INLINE static FlatPtr convert_from(FlatPtr bits)
|
||||
{
|
||||
return !bits;
|
||||
if (PtrTraits::is_null(bits))
|
||||
return default_null_value;
|
||||
return as_bits(PtrTraits::as_ptr(bits));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static bool is_null(FlatPtr bits)
|
||||
{
|
||||
return !(bits & ~(FlatPtr)1);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static FlatPtr exchange(Atomic<FlatPtr>& atomic_var, FlatPtr new_value)
|
||||
{
|
||||
// Only exchange when lock is not held
|
||||
ASSERT(!(new_value & 1));
|
||||
FlatPtr expected = atomic_var.load(AK::MemoryOrder::memory_order_relaxed);
|
||||
for (;;) {
|
||||
expected &= ~(FlatPtr)1; // only if lock bit is not set
|
||||
if (atomic_var.compare_exchange_strong(expected, new_value, AK::MemoryOrder::memory_order_acq_rel))
|
||||
break;
|
||||
#ifdef KERNEL
|
||||
Kernel::Processor::wait_check();
|
||||
#endif
|
||||
}
|
||||
return expected;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static bool exchange_if_null(Atomic<FlatPtr>& atomic_var, FlatPtr new_value)
|
||||
{
|
||||
// Only exchange when lock is not held
|
||||
ASSERT(!(new_value & 1));
|
||||
for (;;) {
|
||||
FlatPtr expected = default_null_value; // only if lock bit is not set
|
||||
if (atomic_var.compare_exchange_strong(expected, new_value, AK::MemoryOrder::memory_order_acq_rel))
|
||||
break;
|
||||
if (!is_null(expected))
|
||||
return false;
|
||||
#ifdef KERNEL
|
||||
Kernel::Processor::wait_check();
|
||||
#endif
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static FlatPtr lock(Atomic<FlatPtr>& atomic_var)
|
||||
{
|
||||
// This sets the lock bit atomically, preventing further modifications.
|
||||
// This is important when e.g. copying a RefPtr where the source
|
||||
// might be released and freed too quickly. This allows us
|
||||
// to temporarily lock the pointer so we can add a reference, then
|
||||
// unlock it
|
||||
FlatPtr bits;
|
||||
for (;;) {
|
||||
bits = atomic_var.fetch_or(1, AK::MemoryOrder::memory_order_acq_rel);
|
||||
if (!(bits & 1))
|
||||
break;
|
||||
#ifdef KERNEL
|
||||
Kernel::Processor::wait_check();
|
||||
#endif
|
||||
}
|
||||
ASSERT(!(bits & 1));
|
||||
return bits;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static void unlock(Atomic<FlatPtr>& atomic_var, FlatPtr new_value)
|
||||
{
|
||||
ASSERT(!(new_value & 1));
|
||||
atomic_var.store(new_value, AK::MemoryOrder::memory_order_release);
|
||||
}
|
||||
|
||||
static constexpr FlatPtr default_null_value = 0;
|
||||
|
@ -63,6 +135,9 @@ template<typename T, typename PtrTraits>
|
|||
class RefPtr {
|
||||
template<typename U, typename P>
|
||||
friend class RefPtr;
|
||||
template<typename U>
|
||||
friend class WeakPtr;
|
||||
|
||||
public:
|
||||
enum AdoptTag {
|
||||
Adopt
|
||||
|
@ -79,62 +154,55 @@ public:
|
|||
{
|
||||
T* ptr = const_cast<T*>(&object);
|
||||
ASSERT(ptr);
|
||||
ASSERT(!ptr == PtrTraits::is_null(m_bits));
|
||||
ASSERT(!is_null());
|
||||
ptr->ref();
|
||||
}
|
||||
RefPtr(AdoptTag, T& object)
|
||||
: m_bits(PtrTraits::as_bits(&object))
|
||||
{
|
||||
ASSERT(&object);
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
ASSERT(!is_null());
|
||||
}
|
||||
RefPtr(RefPtr&& other)
|
||||
: m_bits(other.leak_ref_raw())
|
||||
{
|
||||
}
|
||||
ALWAYS_INLINE RefPtr(const NonnullRefPtr<T>& other)
|
||||
: m_bits(PtrTraits::as_bits(const_cast<T*>(other.ptr())))
|
||||
: m_bits(PtrTraits::as_bits(const_cast<T*>(other.add_ref())))
|
||||
{
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
PtrTraits::as_ptr(m_bits)->ref();
|
||||
}
|
||||
template<typename U>
|
||||
ALWAYS_INLINE RefPtr(const NonnullRefPtr<U>& other)
|
||||
: m_bits(PtrTraits::as_bits(const_cast<U*>(other.ptr())))
|
||||
: m_bits(PtrTraits::as_bits(const_cast<U*>(other.add_ref())))
|
||||
{
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
PtrTraits::as_ptr(m_bits)->ref();
|
||||
}
|
||||
template<typename U>
|
||||
ALWAYS_INLINE RefPtr(NonnullRefPtr<U>&& other)
|
||||
: m_bits(PtrTraits::as_bits(&other.leak_ref()))
|
||||
{
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
ASSERT(!is_null());
|
||||
}
|
||||
template<typename U, typename P = RefPtrTraits<U>>
|
||||
RefPtr(RefPtr<U, P>&& other)
|
||||
: m_bits(other.leak_ref_raw())
|
||||
: m_bits(PtrTraits::template convert_from<U, P>(other.leak_ref_raw()))
|
||||
{
|
||||
}
|
||||
RefPtr(const RefPtr& other)
|
||||
: m_bits(PtrTraits::as_bits(const_cast<T*>(other.ptr())))
|
||||
: m_bits(other.add_ref_raw())
|
||||
{
|
||||
ref_if_not_null(const_cast<T*>(other.ptr()));
|
||||
}
|
||||
template<typename U, typename P = RefPtrTraits<U>>
|
||||
RefPtr(const RefPtr<U, P>& other)
|
||||
: m_bits(PtrTraits::as_bits(const_cast<U*>(other.ptr())))
|
||||
: m_bits(other.add_ref_raw())
|
||||
{
|
||||
ref_if_not_null(const_cast<U*>(other.ptr()));
|
||||
}
|
||||
ALWAYS_INLINE ~RefPtr()
|
||||
{
|
||||
clear();
|
||||
#ifdef SANITIZE_PTRS
|
||||
if constexpr (sizeof(T*) == 8)
|
||||
m_bits = 0xe0e0e0e0e0e0e0e0;
|
||||
m_bits.store(0xe0e0e0e0e0e0e0e0, AK::MemoryOrder::memory_order_relaxed);
|
||||
else
|
||||
m_bits = 0xe0e0e0e0;
|
||||
m_bits.store(0xe0e0e0e0, AK::MemoryOrder::memory_order_relaxed);
|
||||
#endif
|
||||
}
|
||||
RefPtr(std::nullptr_t) { }
|
||||
|
@ -144,79 +212,85 @@ public:
|
|||
template<typename U>
|
||||
RefPtr& operator=(const OwnPtr<U>&) = delete;
|
||||
|
||||
template<typename U>
|
||||
void swap(RefPtr<U, PtrTraits>& other)
|
||||
void swap(RefPtr& other)
|
||||
{
|
||||
::swap(m_bits, other.m_bits);
|
||||
if (this == &other)
|
||||
return;
|
||||
|
||||
// NOTE: swap is not atomic!
|
||||
FlatPtr other_bits = PtrTraits::exchange(other.m_bits, PtrTraits::default_null_value);
|
||||
FlatPtr bits = PtrTraits::exchange(m_bits, other_bits);
|
||||
PtrTraits::exchange(other.m_bits, bits);
|
||||
}
|
||||
|
||||
template<typename U, typename P = RefPtrTraits<U>>
|
||||
void swap(RefPtr<U, P>& other)
|
||||
{
|
||||
// NOTE: swap is not atomic!
|
||||
FlatPtr other_bits = P::exchange(other.m_bits, P::default_null_value);
|
||||
FlatPtr bits = PtrTraits::exchange(m_bits, PtrTraits::template convert_from<U, P>(other_bits));
|
||||
P::exchange(other.m_bits, P::template convert_from<U, P>(bits));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE RefPtr& operator=(RefPtr&& other)
|
||||
{
|
||||
RefPtr tmp = move(other);
|
||||
swap(tmp);
|
||||
if (this != &other)
|
||||
assign_raw(other.leak_ref_raw());
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
ALWAYS_INLINE RefPtr& operator=(RefPtr<U, PtrTraits>&& other)
|
||||
template<typename U, typename P = RefPtrTraits<U>>
|
||||
ALWAYS_INLINE RefPtr& operator=(RefPtr<U, P>&& other)
|
||||
{
|
||||
RefPtr tmp = move(other);
|
||||
swap(tmp);
|
||||
assign_raw(PtrTraits::template convert_from<U, P>(other.leak_ref_raw()));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
ALWAYS_INLINE RefPtr& operator=(NonnullRefPtr<U>&& other)
|
||||
{
|
||||
RefPtr tmp = move(other);
|
||||
swap(tmp);
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
assign_raw(PtrTraits::as_bits(&other.leak_ref()));
|
||||
return *this;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE RefPtr& operator=(const NonnullRefPtr<T>& other)
|
||||
{
|
||||
RefPtr tmp = other;
|
||||
swap(tmp);
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
assign_raw(PtrTraits::as_bits(other.add_ref()));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
ALWAYS_INLINE RefPtr& operator=(const NonnullRefPtr<U>& other)
|
||||
{
|
||||
RefPtr tmp = other;
|
||||
swap(tmp);
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
assign_raw(PtrTraits::as_bits(other.add_ref()));
|
||||
return *this;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE RefPtr& operator=(const RefPtr& other)
|
||||
{
|
||||
RefPtr tmp = other;
|
||||
swap(tmp);
|
||||
if (this != &other)
|
||||
assign_raw(other.add_ref_raw());
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
ALWAYS_INLINE RefPtr& operator=(const RefPtr<U>& other)
|
||||
{
|
||||
RefPtr tmp = other;
|
||||
swap(tmp);
|
||||
assign_raw(other.add_ref_raw());
|
||||
return *this;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE RefPtr& operator=(const T* ptr)
|
||||
{
|
||||
RefPtr tmp = ptr;
|
||||
swap(tmp);
|
||||
ref_if_not_null(const_cast<T*>(ptr));
|
||||
assign_raw(PtrTraits::as_bits(const_cast<T*>(ptr)));
|
||||
return *this;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE RefPtr& operator=(const T& object)
|
||||
{
|
||||
RefPtr tmp = object;
|
||||
swap(tmp);
|
||||
const_cast<T&>(object).ref();
|
||||
assign_raw(PtrTraits::as_bits(const_cast<T*>(&object)));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -226,99 +300,166 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void clear()
|
||||
ALWAYS_INLINE bool assign_if_null(RefPtr&& other)
|
||||
{
|
||||
unref_if_not_null(PtrTraits::as_ptr(m_bits));
|
||||
m_bits = PtrTraits::default_null_value;
|
||||
if (this == &other)
|
||||
return is_null();
|
||||
return PtrTraits::exchange_if_null(m_bits, other.leak_ref_raw());
|
||||
}
|
||||
|
||||
bool operator!() const { return PtrTraits::is_null(m_bits); }
|
||||
template<typename U, typename P = RefPtrTraits<U>>
|
||||
ALWAYS_INLINE bool assign_if_null(RefPtr<U, P>&& other)
|
||||
{
|
||||
if (this == &other)
|
||||
return is_null();
|
||||
return PtrTraits::exchange_if_null(m_bits, PtrTraits::template convert_from<U, P>(other.leak_ref_raw()));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void clear()
|
||||
{
|
||||
assign_raw(PtrTraits::default_null_value);
|
||||
}
|
||||
|
||||
bool operator!() const { return PtrTraits::is_null(m_bits.load(AK::MemoryOrder::memory_order_relaxed)); }
|
||||
|
||||
[[nodiscard]] T* leak_ref()
|
||||
{
|
||||
FlatPtr bits = exchange(m_bits, PtrTraits::default_null_value);
|
||||
return !PtrTraits::is_null(bits) ? PtrTraits::as_ptr(bits) : nullptr;
|
||||
FlatPtr bits = PtrTraits::exchange(m_bits, PtrTraits::default_null_value);
|
||||
return PtrTraits::as_ptr(bits);
|
||||
}
|
||||
|
||||
NonnullRefPtr<T> release_nonnull()
|
||||
{
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *leak_ref());
|
||||
FlatPtr bits = PtrTraits::exchange(m_bits, PtrTraits::default_null_value);
|
||||
ASSERT(!PtrTraits::is_null(bits));
|
||||
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *PtrTraits::as_ptr(bits));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T* ptr() { return !PtrTraits::is_null(m_bits) ? PtrTraits::as_ptr(m_bits) : nullptr; }
|
||||
ALWAYS_INLINE const T* ptr() const { return !PtrTraits::is_null(m_bits) ? PtrTraits::as_ptr(m_bits) : nullptr; }
|
||||
ALWAYS_INLINE T* ptr() { return as_ptr(); }
|
||||
ALWAYS_INLINE const T* ptr() const { return as_ptr(); }
|
||||
|
||||
ALWAYS_INLINE T* operator->()
|
||||
{
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
return PtrTraits::as_ptr(m_bits);
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE const T* operator->() const
|
||||
{
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
return PtrTraits::as_ptr(m_bits);
|
||||
return as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T& operator*()
|
||||
{
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
return *PtrTraits::as_ptr(m_bits);
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE const T& operator*() const
|
||||
{
|
||||
ASSERT(!PtrTraits::is_null(m_bits));
|
||||
return *PtrTraits::as_ptr(m_bits);
|
||||
return *as_nonnull_ptr();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE operator const T*() const { return PtrTraits::as_ptr(m_bits); }
|
||||
ALWAYS_INLINE operator T*() { return PtrTraits::as_ptr(m_bits); }
|
||||
ALWAYS_INLINE operator const T*() const { return as_ptr(); }
|
||||
ALWAYS_INLINE operator T*() { return as_ptr(); }
|
||||
|
||||
operator bool() { return !PtrTraits::is_null(m_bits); }
|
||||
operator bool() { return !is_null(); }
|
||||
|
||||
bool operator==(std::nullptr_t) const { return PtrTraits::is_null(m_bits); }
|
||||
bool operator!=(std::nullptr_t) const { return !PtrTraits::is_null(m_bits); }
|
||||
bool operator==(std::nullptr_t) const { return is_null(); }
|
||||
bool operator!=(std::nullptr_t) const { return !is_null(); }
|
||||
|
||||
bool operator==(const RefPtr& other) const { return m_bits == other.m_bits; }
|
||||
bool operator!=(const RefPtr& other) const { return m_bits != other.m_bits; }
|
||||
bool operator==(const RefPtr& other) const { return as_ptr() == other.as_ptr(); }
|
||||
bool operator!=(const RefPtr& other) const { return as_ptr() != other.as_ptr(); }
|
||||
|
||||
bool operator==(RefPtr& other) { return m_bits == other.m_bits; }
|
||||
bool operator!=(RefPtr& other) { return m_bits != other.m_bits; }
|
||||
bool operator==(RefPtr& other) { return as_ptr() == other.as_ptr(); }
|
||||
bool operator!=(RefPtr& other) { return as_ptr() != other.as_ptr(); }
|
||||
|
||||
bool operator==(const T* other) const { return PtrTraits::as_ptr(m_bits) == other; }
|
||||
bool operator!=(const T* other) const { return PtrTraits::as_ptr(m_bits) != other; }
|
||||
bool operator==(const T* other) const { return as_ptr() == other; }
|
||||
bool operator!=(const T* other) const { return as_ptr() != other; }
|
||||
|
||||
bool operator==(T* other) { return PtrTraits::as_ptr(m_bits) == other; }
|
||||
bool operator!=(T* other) { return PtrTraits::as_ptr(m_bits) != other; }
|
||||
bool operator==(T* other) { return as_ptr() == other; }
|
||||
bool operator!=(T* other) { return as_ptr() != other; }
|
||||
|
||||
bool is_null() const { return PtrTraits::is_null(m_bits.load(AK::MemoryOrder::memory_order_relaxed)); }
|
||||
|
||||
bool is_null() const { return PtrTraits::is_null(m_bits); }
|
||||
|
||||
template<typename U = T, typename EnableIf<IsSame<U, T>::value && !IsNullPointer<typename PtrTraits::NullType>::value>::Type* = nullptr>
|
||||
typename PtrTraits::NullType null_value() const
|
||||
{
|
||||
// make sure we are holding a null value
|
||||
ASSERT(PtrTraits::is_null(m_bits));
|
||||
return PtrTraits::to_null_value(m_bits);
|
||||
FlatPtr bits = m_bits.load(AK::MemoryOrder::memory_order_relaxed);
|
||||
ASSERT(PtrTraits::is_null(bits));
|
||||
return PtrTraits::to_null_value(bits);
|
||||
}
|
||||
template<typename U = T, typename EnableIf<IsSame<U, T>::value && !IsNullPointer<typename PtrTraits::NullType>::value>::Type* = nullptr>
|
||||
void set_null_value(typename PtrTraits::NullType value)
|
||||
{
|
||||
// make sure that new null value would be interpreted as a null value
|
||||
FlatPtr bits = PtrTraits::from_null_value(value);
|
||||
ASSERT(PtrTraits::is_null(bits));
|
||||
clear();
|
||||
m_bits = bits;
|
||||
// make sure that new null value would be interpreted as a null value
|
||||
FlatPtr bits = PtrTraits::from_null_value(value);
|
||||
ASSERT(PtrTraits::is_null(bits));
|
||||
assign_raw(bits);
|
||||
}
|
||||
|
||||
private:
|
||||
[[nodiscard]] FlatPtr leak_ref_raw()
|
||||
template<typename F>
|
||||
void do_while_locked(F f) const
|
||||
{
|
||||
return exchange(m_bits, PtrTraits::default_null_value);
|
||||
#ifdef KERNEL
|
||||
// We don't want to be pre-empted while we have the lock bit set
|
||||
Kernel::ScopedCritical critical;
|
||||
#endif
|
||||
FlatPtr bits = PtrTraits::lock(m_bits);
|
||||
T* ptr = PtrTraits::as_ptr(bits);
|
||||
f(ptr);
|
||||
PtrTraits::unlock(m_bits, bits);
|
||||
}
|
||||
|
||||
FlatPtr m_bits { PtrTraits::default_null_value };
|
||||
[[nodiscard]] ALWAYS_INLINE FlatPtr leak_ref_raw()
|
||||
{
|
||||
return PtrTraits::exchange(m_bits, PtrTraits::default_null_value);
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE FlatPtr add_ref_raw() const
|
||||
{
|
||||
#ifdef KERNEL
|
||||
// We don't want to be pre-empted while we have the lock bit set
|
||||
Kernel::ScopedCritical critical;
|
||||
#endif
|
||||
// This prevents a race condition between thread A and B:
|
||||
// 1. Thread A copies RefPtr, e.g. through assignment or copy constructor,
|
||||
// gets the pointer from source, but is pre-empted before adding
|
||||
// another reference
|
||||
// 2. Thread B calls clear, leak_ref, or release_nonnull on source, and
|
||||
// then drops the last reference, causing the object to be deleted
|
||||
// 3. Thread A finishes step #1 by attempting to add a reference to
|
||||
// the object that was already deleted in step #2
|
||||
FlatPtr bits = PtrTraits::lock(m_bits);
|
||||
if (T* ptr = PtrTraits::as_ptr(bits))
|
||||
ptr->ref();
|
||||
PtrTraits::unlock(m_bits, bits);
|
||||
return bits;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void assign_raw(FlatPtr bits)
|
||||
{
|
||||
FlatPtr prev_bits = PtrTraits::exchange(m_bits, bits);
|
||||
unref_if_not_null(PtrTraits::as_ptr(prev_bits));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T* as_ptr() const
|
||||
{
|
||||
return PtrTraits::as_ptr(m_bits.load(AK::MemoryOrder::memory_order_relaxed));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T* as_nonnull_ptr() const
|
||||
{
|
||||
return as_nonnull_ptr(m_bits.load(AK::MemoryOrder::memory_order_relaxed));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE T* as_nonnull_ptr(FlatPtr bits) const
|
||||
{
|
||||
ASSERT(!PtrTraits::is_null(bits));
|
||||
return PtrTraits::as_ptr(bits);
|
||||
}
|
||||
|
||||
mutable Atomic<FlatPtr> m_bits { PtrTraits::default_null_value };
|
||||
};
|
||||
|
||||
template<typename T, typename PtrTraits = RefPtrTraits<T>>
|
||||
|
@ -346,6 +487,12 @@ inline RefPtr<T> static_ptr_cast(const RefPtr<U>& ptr)
|
|||
return RefPtr<T, PtrTraits>(static_cast<const T*>(ptr.ptr()));
|
||||
}
|
||||
|
||||
template<typename T, typename PtrTraitsT, typename U, typename PtrTraitsU>
|
||||
inline void swap(RefPtr<T, PtrTraitsT>& a, RefPtr<U, PtrTraitsU>& b)
|
||||
{
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
using AK::RefPtr;
|
||||
|
|
|
@ -146,6 +146,8 @@ struct IntegralConstant {
|
|||
|
||||
typedef IntegralConstant<bool, false> FalseType;
|
||||
typedef IntegralConstant<bool, true> TrueType;
|
||||
template<typename...>
|
||||
using VoidType = void;
|
||||
|
||||
template<class T>
|
||||
struct IsLvalueReference : FalseType {
|
||||
|
@ -302,7 +304,6 @@ template<typename T>
|
|||
struct IsNullPointer : IsSame<decltype(nullptr), typename RemoveCV<T>::Type> {
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
struct RemoveReference {
|
||||
typedef T Type;
|
||||
|
|
|
@ -33,6 +33,9 @@ struct Object : public RefCounted<Object> {
|
|||
int x;
|
||||
};
|
||||
|
||||
struct Object2 : Object {
|
||||
};
|
||||
|
||||
TEST_CASE(basics)
|
||||
{
|
||||
RefPtr<Object> object = adopt(*new Object);
|
||||
|
@ -67,6 +70,42 @@ TEST_CASE(assign_ptr)
|
|||
EXPECT_EQ(object->ref_count(), 1u);
|
||||
}
|
||||
|
||||
TEST_CASE(copy_move_ref)
|
||||
{
|
||||
RefPtr<Object2> object = adopt(*new Object2);
|
||||
EXPECT_EQ(object->ref_count(), 1u);
|
||||
{
|
||||
auto object2 = object;
|
||||
EXPECT_EQ(object->ref_count(), 2u);
|
||||
|
||||
RefPtr<Object> object1 = object;
|
||||
EXPECT_EQ(object->ref_count(), 3u);
|
||||
|
||||
object1 = move(object2);
|
||||
EXPECT_EQ(object->ref_count(), 2u);
|
||||
|
||||
RefPtr<Object> object3(move(object1));
|
||||
EXPECT_EQ(object3->ref_count(), 2u);
|
||||
|
||||
object1 = object3;
|
||||
EXPECT_EQ(object3->ref_count(), 3u);
|
||||
}
|
||||
EXPECT_EQ(object->ref_count(), 1u);
|
||||
}
|
||||
|
||||
TEST_CASE(swap)
|
||||
{
|
||||
RefPtr<Object> object_a = adopt(*new Object);
|
||||
RefPtr<Object> object_b = adopt(*new Object);
|
||||
auto* ptr_a = object_a.ptr();
|
||||
auto* ptr_b = object_b.ptr();
|
||||
swap(object_a, object_b);
|
||||
EXPECT_EQ(object_a, ptr_b);
|
||||
EXPECT_EQ(object_b, ptr_a);
|
||||
EXPECT_EQ(object_a->ref_count(), 1u);
|
||||
EXPECT_EQ(object_b->ref_count(), 1u);
|
||||
}
|
||||
|
||||
TEST_CASE(assign_moved_self)
|
||||
{
|
||||
RefPtr<Object> object = adopt(*new Object);
|
||||
|
|
|
@ -35,7 +35,8 @@
|
|||
# pragma clang diagnostic ignored "-Wunused-private-field"
|
||||
#endif
|
||||
|
||||
class SimpleWeakable : public Weakable<SimpleWeakable> {
|
||||
class SimpleWeakable : public Weakable<SimpleWeakable>
|
||||
, public RefCounted<SimpleWeakable> {
|
||||
public:
|
||||
SimpleWeakable() { }
|
||||
|
||||
|
@ -53,18 +54,18 @@ TEST_CASE(basic_weak)
|
|||
WeakPtr<SimpleWeakable> weak2;
|
||||
|
||||
{
|
||||
SimpleWeakable simple;
|
||||
weak1 = simple.make_weak_ptr();
|
||||
weak2 = simple.make_weak_ptr();
|
||||
auto simple = adopt(*new SimpleWeakable);
|
||||
weak1 = simple;
|
||||
weak2 = simple;
|
||||
EXPECT_EQ(weak1.is_null(), false);
|
||||
EXPECT_EQ(weak2.is_null(), false);
|
||||
EXPECT_EQ(weak1.ptr(), &simple);
|
||||
EXPECT_EQ(weak1.ptr(), weak2.ptr());
|
||||
EXPECT_EQ(weak1.strong_ref().ptr(), simple.ptr());
|
||||
EXPECT_EQ(weak1.strong_ref().ptr(), weak2.strong_ref().ptr());
|
||||
}
|
||||
|
||||
EXPECT_EQ(weak1.is_null(), true);
|
||||
EXPECT_EQ(weak1.ptr(), nullptr);
|
||||
EXPECT_EQ(weak1.ptr(), weak2.ptr());
|
||||
EXPECT_EQ(weak1.strong_ref().ptr(), nullptr);
|
||||
EXPECT_EQ(weak1.strong_ref().ptr(), weak2.strong_ref().ptr());
|
||||
}
|
||||
|
||||
TEST_CASE(weakptr_move)
|
||||
|
@ -73,12 +74,12 @@ TEST_CASE(weakptr_move)
|
|||
WeakPtr<SimpleWeakable> weak2;
|
||||
|
||||
{
|
||||
SimpleWeakable simple;
|
||||
weak1 = simple.make_weak_ptr();
|
||||
auto simple = adopt(*new SimpleWeakable);
|
||||
weak1 = simple;
|
||||
weak2 = move(weak1);
|
||||
EXPECT_EQ(weak1.is_null(), true);
|
||||
EXPECT_EQ(weak2.is_null(), false);
|
||||
EXPECT_EQ(weak2.ptr(), &simple);
|
||||
EXPECT_EQ(weak2.strong_ref().ptr(), simple.ptr());
|
||||
}
|
||||
|
||||
EXPECT_EQ(weak2.is_null(), true);
|
||||
|
|
199
AK/WeakPtr.h
199
AK/WeakPtr.h
|
@ -31,82 +31,209 @@
|
|||
|
||||
namespace AK {
|
||||
|
||||
template<typename T>
|
||||
class OwnPtr;
|
||||
|
||||
template<typename T>
|
||||
class WeakPtr {
|
||||
friend class Weakable<T>;
|
||||
template<typename U>
|
||||
friend class Weakable;
|
||||
|
||||
public:
|
||||
WeakPtr() { }
|
||||
WeakPtr(std::nullptr_t) { }
|
||||
|
||||
template<typename U>
|
||||
WeakPtr(WeakPtr<U>&& other)
|
||||
: m_link(reinterpret_cast<WeakLink<T>*>(other.take_link().ptr()))
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr(const WeakPtr<U>& other)
|
||||
: m_link(other.m_link)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr(WeakPtr<U>&& other)
|
||||
: m_link(other.take_link())
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr& operator=(WeakPtr<U>&& other)
|
||||
{
|
||||
m_link = reinterpret_cast<WeakLink<T>*>(other.take_link().ptr());
|
||||
m_link = other.take_link();
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator bool() const { return ptr(); }
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr& operator=(const WeakPtr<U>& other)
|
||||
{
|
||||
if ((const void*)this != (const void*)&other)
|
||||
m_link = other.m_link;
|
||||
return *this;
|
||||
}
|
||||
|
||||
T* ptr() { return m_link ? m_link->ptr() : nullptr; }
|
||||
const T* ptr() const { return m_link ? m_link->ptr() : nullptr; }
|
||||
WeakPtr& operator=(std::nullptr_t)
|
||||
{
|
||||
clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
T* operator->() { return ptr(); }
|
||||
const T* operator->() const { return ptr(); }
|
||||
|
||||
T& operator*() { return *ptr(); }
|
||||
const T& operator*() const { return *ptr(); }
|
||||
|
||||
operator const T*() const { return ptr(); }
|
||||
operator T*() { return ptr(); }
|
||||
|
||||
bool is_null() const { return !m_link || !m_link->ptr(); }
|
||||
void clear() { m_link = nullptr; }
|
||||
|
||||
RefPtr<WeakLink<T>> take_link() { return move(m_link); }
|
||||
|
||||
bool operator==(const OwnPtr<T>& other) const { return ptr() == other.ptr(); }
|
||||
|
||||
private:
|
||||
WeakPtr(RefPtr<WeakLink<T>> link)
|
||||
: m_link(move(link))
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr(const U& object)
|
||||
: m_link(object.template make_weak_ptr<U>().take_link())
|
||||
{
|
||||
}
|
||||
|
||||
RefPtr<WeakLink<T>> m_link;
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr(const U* object)
|
||||
{
|
||||
if (object)
|
||||
m_link = object->template make_weak_ptr<U>().take_link();
|
||||
}
|
||||
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr(const RefPtr<U>& object)
|
||||
{
|
||||
object.do_while_locked([&](U* obj) {
|
||||
if (obj)
|
||||
obj->template make_weak_ptr<U>().take_link();
|
||||
});
|
||||
}
|
||||
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr(const NonnullRefPtr<U>& object)
|
||||
{
|
||||
object.do_while_locked([&](U* obj) {
|
||||
if (obj)
|
||||
obj->template make_weak_ptr<U>().take_link();
|
||||
});
|
||||
}
|
||||
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr& operator=(const U& object)
|
||||
{
|
||||
m_link = object.template make_weak_ptr<U>().take_link();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr& operator=(const U* object)
|
||||
{
|
||||
if (object)
|
||||
m_link = object->template make_weak_ptr<U>().take_link();
|
||||
else
|
||||
m_link = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr& operator=(const RefPtr<U>& object)
|
||||
{
|
||||
object.do_while_locked([&](U* obj) {
|
||||
if (obj)
|
||||
m_link = obj->template make_weak_ptr<U>().take_link();
|
||||
else
|
||||
m_link = nullptr;
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U, typename EnableIf<IsBaseOf<T, U>::value>::Type* = nullptr>
|
||||
WeakPtr& operator=(const NonnullRefPtr<U>& object)
|
||||
{
|
||||
object.do_while_locked([&](U* obj) {
|
||||
if (obj)
|
||||
m_link = obj->template make_weak_ptr<U>().take_link();
|
||||
else
|
||||
m_link = nullptr;
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
RefPtr<T> strong_ref() const
|
||||
{
|
||||
// This only works with RefCounted objects, but it is the only
|
||||
// safe way to get a strong reference from a WeakPtr. Any code
|
||||
// that uses objects not derived from RefCounted will have to
|
||||
// use unsafe_ptr(), but as the name suggests, it is not safe...
|
||||
RefPtr<T> ref;
|
||||
// Using do_while_locked protects against a race with clear()!
|
||||
m_link.do_while_locked([&](WeakLink* link) {
|
||||
if (link)
|
||||
ref = link->template strong_ref<T>();
|
||||
});
|
||||
return ref;
|
||||
}
|
||||
|
||||
#ifndef KERNEL
|
||||
// A lot of user mode code is single-threaded. But for kernel mode code
|
||||
// this is generally not true as everything is multi-threaded. So make
|
||||
// these shortcuts and aliases only available to non-kernel code.
|
||||
T* ptr() const { return unsafe_ptr(); }
|
||||
T* operator->() { return unsafe_ptr(); }
|
||||
const T* operator->() const { return unsafe_ptr(); }
|
||||
operator const T*() const { return unsafe_ptr(); }
|
||||
operator T*() { return unsafe_ptr(); }
|
||||
#endif
|
||||
|
||||
T* unsafe_ptr() const
|
||||
{
|
||||
T* ptr = nullptr;
|
||||
m_link.do_while_locked([&](WeakLink* link) {
|
||||
if (link)
|
||||
ptr = link->unsafe_ptr<T>();
|
||||
});
|
||||
return ptr;
|
||||
}
|
||||
|
||||
operator bool() const { return m_link ? !m_link->is_null() : false; }
|
||||
|
||||
bool is_null() const { return !m_link || m_link->is_null(); }
|
||||
void clear() { m_link = nullptr; }
|
||||
|
||||
RefPtr<WeakLink> take_link() { return move(m_link); }
|
||||
|
||||
private:
|
||||
WeakPtr(const RefPtr<WeakLink>& link)
|
||||
: m_link(link)
|
||||
{
|
||||
}
|
||||
|
||||
RefPtr<WeakLink> m_link;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline WeakPtr<T> Weakable<T>::make_weak_ptr()
|
||||
template<typename U>
|
||||
inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
|
||||
{
|
||||
#ifdef DEBUG
|
||||
ASSERT(!m_being_destroyed);
|
||||
#endif
|
||||
if (!m_link)
|
||||
m_link = adopt(*new WeakLink<T>(static_cast<T&>(*this)));
|
||||
return WeakPtr<T>(m_link);
|
||||
if (!m_link) {
|
||||
// There is a small chance that we create a new WeakLink and throw
|
||||
// it away because another thread beat us to it. But the window is
|
||||
// pretty small and the overhead isn't terrible.
|
||||
m_link.assign_if_null(adopt(*new WeakLink(const_cast<T&>(static_cast<const T&>(*this)))));
|
||||
}
|
||||
return WeakPtr<U>(m_link);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline const LogStream& operator<<(const LogStream& stream, const WeakPtr<T>& value)
|
||||
{
|
||||
#ifdef KERNEL
|
||||
auto ref = value.strong_ref();
|
||||
return stream << ref.ptr();
|
||||
#else
|
||||
return stream << value.ptr();
|
||||
#endif
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
struct Formatter<WeakPtr<T>> : Formatter<const T*> {
|
||||
void format(TypeErasedFormatParams& params, FormatBuilder& builder, const WeakPtr<T>& value)
|
||||
{
|
||||
#ifdef KERNEL
|
||||
auto ref = value.strong_ref();
|
||||
Formatter<const T*>::format(params, builder, ref.ptr());
|
||||
#else
|
||||
Formatter<const T*>::format(params, builder, value.ptr());
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "Assertions.h"
|
||||
#include "Atomic.h"
|
||||
#include "RefCounted.h"
|
||||
#include "RefPtr.h"
|
||||
|
||||
|
@ -41,20 +42,56 @@ class Weakable;
|
|||
template<typename T>
|
||||
class WeakPtr;
|
||||
|
||||
template<typename T>
|
||||
class WeakLink : public RefCounted<WeakLink<T>> {
|
||||
friend class Weakable<T>;
|
||||
class WeakLink : public RefCounted<WeakLink> {
|
||||
template<typename T>
|
||||
friend class Weakable;
|
||||
template<typename T>
|
||||
friend class WeakPtr;
|
||||
|
||||
public:
|
||||
T* ptr() { return m_ptr; }
|
||||
const T* ptr() const { return m_ptr; }
|
||||
template<typename T, typename PtrTraits = RefPtrTraits<T>>
|
||||
RefPtr<T, PtrTraits> strong_ref() const
|
||||
{
|
||||
RefPtr<T, PtrTraits> ref;
|
||||
|
||||
{
|
||||
#ifdef KERNEL
|
||||
// We don't want to be pre-empted while we have the lock bit set
|
||||
Kernel::ScopedCritical critical;
|
||||
#endif
|
||||
FlatPtr bits = RefPtrTraits<void>::lock(m_bits);
|
||||
T* ptr = static_cast<T*>(RefPtrTraits<void>::as_ptr(bits));
|
||||
if (ptr)
|
||||
ref = *ptr;
|
||||
RefPtrTraits<void>::unlock(m_bits, bits);
|
||||
}
|
||||
|
||||
return ref;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* unsafe_ptr() const
|
||||
{
|
||||
return static_cast<T*>(RefPtrTraits<void>::as_ptr(m_bits.load(AK::MemoryOrder::memory_order_acquire)));
|
||||
}
|
||||
|
||||
bool is_null() const
|
||||
{
|
||||
return RefPtrTraits<void>::is_null(m_bits.load(AK::MemoryOrder::memory_order_relaxed));
|
||||
}
|
||||
|
||||
void revoke()
|
||||
{
|
||||
RefPtrTraits<void>::exchange(m_bits, RefPtrTraits<void>::default_null_value);
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
explicit WeakLink(T& weakable)
|
||||
: m_ptr(&weakable)
|
||||
: m_bits(RefPtrTraits<void>::as_bits(&weakable))
|
||||
{
|
||||
}
|
||||
T* m_ptr;
|
||||
mutable Atomic<FlatPtr> m_bits;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
@ -63,7 +100,8 @@ private:
|
|||
class Link;
|
||||
|
||||
public:
|
||||
WeakPtr<T> make_weak_ptr();
|
||||
template<typename U = T>
|
||||
WeakPtr<U> make_weak_ptr() const;
|
||||
|
||||
protected:
|
||||
Weakable() { }
|
||||
|
@ -79,11 +117,11 @@ protected:
|
|||
void revoke_weak_ptrs()
|
||||
{
|
||||
if (m_link)
|
||||
m_link->m_ptr = nullptr;
|
||||
m_link->revoke();
|
||||
}
|
||||
|
||||
private:
|
||||
RefPtr<WeakLink<T>> m_link;
|
||||
mutable RefPtr<WeakLink> m_link;
|
||||
#ifdef WEAKABLE_DEBUG
|
||||
bool m_being_destroyed { false };
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue