2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2023-04-21 11:36:32 +00:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2019-08-02 09:56:55 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2019-08-02 09:56:55 +00:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
|
|
|
|
struct Object : public RefCounted<Object> {
|
|
|
|
int x;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
struct Object2 : Object {
|
|
|
|
};
|
|
|
|
|
2020-12-06 02:47:20 +00:00
|
|
|
struct SelfAwareObject : public RefCounted<SelfAwareObject> {
|
|
|
|
void will_be_destroyed() { ++num_destroyed; }
|
|
|
|
|
|
|
|
static size_t num_destroyed;
|
|
|
|
};
|
|
|
|
size_t SelfAwareObject::num_destroyed = 0;
|
|
|
|
|
2019-08-02 09:56:55 +00:00
|
|
|
TEST_CASE(basics)
|
|
|
|
{
|
2021-04-23 14:46:57 +00:00
|
|
|
RefPtr<Object> object = adopt_ref(*new Object);
|
2019-08-02 09:56:55 +00:00
|
|
|
EXPECT(object.ptr() != nullptr);
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2019-08-02 09:56:55 +00:00
|
|
|
object->ref();
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 2u);
|
2020-01-23 14:14:21 +00:00
|
|
|
object->unref();
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2019-08-02 09:56:55 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
NonnullRefPtr another = *object;
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 2u);
|
2019-08-02 09:56:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2019-08-02 09:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(assign_reference)
|
|
|
|
{
|
2021-04-23 14:46:57 +00:00
|
|
|
RefPtr<Object> object = adopt_ref(*new Object);
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2019-08-02 09:56:55 +00:00
|
|
|
object = *object;
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2019-08-02 09:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(assign_ptr)
|
|
|
|
{
|
2021-04-23 14:46:57 +00:00
|
|
|
RefPtr<Object> object = adopt_ref(*new Object);
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2019-08-02 09:56:55 +00:00
|
|
|
object = object.ptr();
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2019-08-02 09:56:55 +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
|
|
|
TEST_CASE(copy_move_ref)
|
|
|
|
{
|
2021-04-23 14:46:57 +00:00
|
|
|
RefPtr<Object2> object = adopt_ref(*new Object2);
|
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
|
|
|
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)
|
|
|
|
{
|
2021-04-23 14:46:57 +00:00
|
|
|
RefPtr<Object> object_a = adopt_ref(*new Object);
|
|
|
|
RefPtr<Object> object_b = adopt_ref(*new Object);
|
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* 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);
|
|
|
|
}
|
|
|
|
|
2019-08-02 09:56:55 +00:00
|
|
|
TEST_CASE(assign_moved_self)
|
|
|
|
{
|
2021-04-23 14:46:57 +00:00
|
|
|
RefPtr<Object> object = adopt_ref(*new Object);
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2023-05-01 14:59:46 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wpragmas"
|
|
|
|
#pragma GCC diagnostic ignored "-Wself-move"
|
2019-08-02 09:56:55 +00:00
|
|
|
object = move(object);
|
2023-05-01 14:59:46 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2019-08-02 09:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(assign_copy_self)
|
|
|
|
{
|
2021-04-23 14:46:57 +00:00
|
|
|
RefPtr<Object> object = adopt_ref(*new Object);
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2020-05-16 08:58:29 +00:00
|
|
|
|
2022-10-04 19:04:13 +00:00
|
|
|
#if defined(AK_COMPILER_CLANG)
|
2020-09-18 07:49:51 +00:00
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
|
|
|
|
#endif
|
|
|
|
object = object;
|
2022-10-04 19:04:13 +00:00
|
|
|
#if defined(AK_COMPILER_CLANG)
|
2020-09-18 07:49:51 +00:00
|
|
|
# pragma clang diagnostic pop
|
|
|
|
#endif
|
2020-05-16 08:58:29 +00:00
|
|
|
|
2020-06-12 13:33:38 +00:00
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
2019-08-02 09:56:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 02:47:20 +00:00
|
|
|
TEST_CASE(self_observers)
|
|
|
|
{
|
2021-05-12 10:46:16 +00:00
|
|
|
{
|
|
|
|
RefPtr<SelfAwareObject> object = adopt_ref(*new SelfAwareObject);
|
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
|
|
|
EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
|
2020-12-06 02:47:20 +00:00
|
|
|
|
2021-05-12 10:46:16 +00:00
|
|
|
object->ref();
|
|
|
|
EXPECT_EQ(object->ref_count(), 2u);
|
|
|
|
EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
|
2020-12-06 02:47:20 +00:00
|
|
|
|
2021-05-12 10:46:16 +00:00
|
|
|
object->unref();
|
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
|
|
|
EXPECT_EQ(SelfAwareObject::num_destroyed, 0u);
|
|
|
|
}
|
2020-12-06 02:47:20 +00:00
|
|
|
EXPECT_EQ(SelfAwareObject::num_destroyed, 1u);
|
|
|
|
}
|
2021-05-13 04:02:43 +00:00
|
|
|
|
|
|
|
TEST_CASE(adopt_ref_if_nonnull)
|
|
|
|
{
|
2021-06-20 08:21:16 +00:00
|
|
|
RefPtr<SelfAwareObject> object = adopt_ref_if_nonnull(new (nothrow) SelfAwareObject);
|
2021-05-13 04:02:43 +00:00
|
|
|
EXPECT_EQ(object.is_null(), false);
|
|
|
|
EXPECT_EQ(object->ref_count(), 1u);
|
|
|
|
|
|
|
|
SelfAwareObject* null_object = nullptr;
|
|
|
|
RefPtr<SelfAwareObject> failed_allocation = adopt_ref_if_nonnull(null_object);
|
|
|
|
EXPECT_EQ(failed_allocation.is_null(), true);
|
|
|
|
}
|
2023-04-21 11:36:32 +00:00
|
|
|
|
|
|
|
TEST_CASE(destroy_self_owning_refcounted_object)
|
|
|
|
{
|
|
|
|
struct SelfOwningRefCounted : public RefCounted<SelfOwningRefCounted> {
|
|
|
|
RefPtr<SelfOwningRefCounted> self;
|
|
|
|
};
|
|
|
|
RefPtr object = make_ref_counted<SelfOwningRefCounted>();
|
|
|
|
auto* object_ptr = object.ptr();
|
|
|
|
object->self = object;
|
|
|
|
object = nullptr;
|
|
|
|
object_ptr->self = nullptr;
|
|
|
|
}
|