AK: Remove thread safety gunk from userspace WeakPtr
The implicit thread safety stuff is only needed by the kernel version of WeakPtr, as userspace already makes no guarantees about this.
This commit is contained in:
parent
30fcb07fe8
commit
51e5cc4e13
Notes:
sideshowbarker
2024-07-17 19:56:05 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/51e5cc4e13f
2 changed files with 7 additions and 33 deletions
36
AK/WeakPtr.h
36
AK/WeakPtr.h
|
@ -153,40 +153,10 @@ template<typename T>
|
||||||
template<typename U>
|
template<typename U>
|
||||||
inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
|
inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
|
||||||
{
|
{
|
||||||
if constexpr (IsBaseOf<RefCountedBase, T>) {
|
if (!m_link)
|
||||||
// Checking m_being_destroyed isn't sufficient when dealing with
|
m_link = adopt_ref(*new WeakLink(const_cast<T&>(static_cast<T const&>(*this))));
|
||||||
// a RefCounted type.The reference count will drop to 0 before the
|
|
||||||
// destructor is invoked and revoke_weak_ptrs is called. So, try
|
|
||||||
// to add a ref (which should fail if the ref count is at 0) so
|
|
||||||
// that we prevent the destructor and revoke_weak_ptrs from being
|
|
||||||
// triggered until we're done.
|
|
||||||
if (!static_cast<const T*>(this)->try_ref())
|
|
||||||
return {};
|
|
||||||
} else {
|
|
||||||
// For non-RefCounted types this means a weak reference can be
|
|
||||||
// obtained until the ~Weakable destructor is invoked!
|
|
||||||
if (m_being_destroyed.load(AK::MemoryOrder::memory_order_acquire))
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
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_ref(*new WeakLink(const_cast<T&>(static_cast<const T&>(*this)))));
|
|
||||||
}
|
|
||||||
|
|
||||||
WeakPtr<U> weak_ptr(m_link);
|
return WeakPtr<U>(m_link);
|
||||||
|
|
||||||
if constexpr (IsBaseOf<RefCountedBase, T>) {
|
|
||||||
// Now drop the reference we temporarily added
|
|
||||||
if (static_cast<const T*>(this)->unref()) {
|
|
||||||
// We just dropped the last reference, which should have called
|
|
||||||
// revoke_weak_ptrs, which should have invalidated our weak_ptr
|
|
||||||
VERIFY(!weak_ptr.strong_ref());
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return weak_ptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
@ -113,7 +113,9 @@ protected:
|
||||||
|
|
||||||
~Weakable()
|
~Weakable()
|
||||||
{
|
{
|
||||||
|
#ifdef KERNEL
|
||||||
m_being_destroyed.store(true, AK::MemoryOrder::memory_order_release);
|
m_being_destroyed.store(true, AK::MemoryOrder::memory_order_release);
|
||||||
|
#endif
|
||||||
revoke_weak_ptrs();
|
revoke_weak_ptrs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +127,9 @@ protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable RefPtr<WeakLink> m_link;
|
mutable RefPtr<WeakLink> m_link;
|
||||||
|
#ifdef KERNEL
|
||||||
Atomic<bool> m_being_destroyed { false };
|
Atomic<bool> m_being_destroyed { false };
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue