2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-09 14:50:13 +00:00
|
|
|
#include <AK/Assertions.h>
|
2020-06-12 13:38:24 +00:00
|
|
|
#include <AK/Atomic.h>
|
2020-06-12 13:44:17 +00:00
|
|
|
#include <AK/Checked.h>
|
2020-06-12 13:46:47 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2020-05-20 12:13:39 +00:00
|
|
|
#include <AK/Platform.h>
|
2020-02-09 14:50:13 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2018-12-19 21:28:09 +00:00
|
|
|
template<class T>
|
2020-12-06 02:47:20 +00:00
|
|
|
constexpr auto call_will_be_destroyed_if_present(const T* object) -> decltype(const_cast<T*>(object)->will_be_destroyed(), TrueType {})
|
2018-12-19 21:28:09 +00:00
|
|
|
{
|
2020-04-17 10:41:45 +00:00
|
|
|
const_cast<T*>(object)->will_be_destroyed();
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2018-12-19 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto call_will_be_destroyed_if_present(...) -> FalseType
|
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2018-12-19 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 01:38:09 +00:00
|
|
|
template<class T>
|
2020-12-06 02:47:20 +00:00
|
|
|
constexpr auto call_one_ref_left_if_present(const T* object) -> decltype(const_cast<T*>(object)->one_ref_left(), TrueType {})
|
2019-01-01 01:38:09 +00:00
|
|
|
{
|
2020-04-17 10:41:45 +00:00
|
|
|
const_cast<T*>(object)->one_ref_left();
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-01-01 01:38:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 13:29:31 +00:00
|
|
|
constexpr auto call_one_ref_left_if_present(...) -> FalseType
|
2019-01-01 01:38:09 +00:00
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-01-01 01:38:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 13:29:31 +00:00
|
|
|
class RefCountedBase {
|
2020-08-26 19:52:24 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(RefCountedBase);
|
|
|
|
AK_MAKE_NONMOVABLE(RefCountedBase);
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2020-11-11 22:21:01 +00:00
|
|
|
using RefCountType = unsigned int;
|
2020-11-03 14:51:56 +00:00
|
|
|
using AllowOwnPtr = FalseType;
|
2020-06-12 13:33:38 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE void ref() const
|
2018-10-10 09:53:07 +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.
2020-09-29 22:26:13 +00:00
|
|
|
auto old_ref_count = m_ref_count.fetch_add(1, AK::MemoryOrder::memory_order_relaxed);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(old_ref_count > 0);
|
|
|
|
VERIFY(!Checked<RefCountType>::addition_would_overflow(old_ref_count, 1));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 23:41:49 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE bool try_ref() const
|
2020-12-29 20:14:21 +00:00
|
|
|
{
|
|
|
|
RefCountType expected = m_ref_count.load(AK::MemoryOrder::memory_order_relaxed);
|
|
|
|
for (;;) {
|
|
|
|
if (expected == 0)
|
|
|
|
return false;
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!Checked<RefCountType>::addition_would_overflow(expected, 1));
|
2020-12-29 20:14:21 +00:00
|
|
|
if (m_ref_count.compare_exchange_strong(expected, expected + 1, AK::MemoryOrder::memory_order_acquire))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:33:38 +00:00
|
|
|
ALWAYS_INLINE RefCountType ref_count() const
|
2018-10-10 09:53:07 +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.
2020-09-29 22:26:13 +00:00
|
|
|
return m_ref_count.load(AK::MemoryOrder::memory_order_relaxed);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2021-01-10 23:29:28 +00:00
|
|
|
RefCountedBase() = default;
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE ~RefCountedBase()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_ref_count.load(AK::MemoryOrder::memory_order_relaxed) == 0);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 13:38:24 +00:00
|
|
|
ALWAYS_INLINE RefCountType deref_base() const
|
2019-03-16 12:48:56 +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.
2020-09-29 22:26:13 +00:00
|
|
|
auto old_ref_count = m_ref_count.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(old_ref_count > 0);
|
2020-06-12 13:38:24 +00:00
|
|
|
return old_ref_count - 1;
|
2019-03-16 12:48:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 13:38:24 +00:00
|
|
|
mutable Atomic<RefCountType> m_ref_count { 1 };
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2019-03-16 11:47:19 +00:00
|
|
|
template<typename T>
|
2019-06-21 13:29:31 +00:00
|
|
|
class RefCounted : public RefCountedBase {
|
2019-03-16 11:47:19 +00:00
|
|
|
public:
|
2020-12-29 20:14:21 +00:00
|
|
|
bool unref() const
|
2019-03-16 11:47:19 +00:00
|
|
|
{
|
2020-06-12 13:38:24 +00:00
|
|
|
auto new_ref_count = deref_base();
|
|
|
|
if (new_ref_count == 0) {
|
2020-04-17 10:41:45 +00:00
|
|
|
call_will_be_destroyed_if_present(static_cast<const T*>(this));
|
|
|
|
delete static_cast<const T*>(this);
|
2020-12-29 20:14:21 +00:00
|
|
|
return true;
|
2020-06-12 13:38:24 +00:00
|
|
|
} else if (new_ref_count == 1) {
|
2020-04-17 10:41:45 +00:00
|
|
|
call_one_ref_left_if_present(static_cast<const T*>(this));
|
2019-03-16 11:47:19 +00:00
|
|
|
}
|
2020-12-29 20:14:21 +00:00
|
|
|
return false;
|
2019-03-16 11:47:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 13:29:31 +00:00
|
|
|
using AK::RefCounted;
|