2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-04-25 02:53:23 -03:00
|
|
|
#include <LibTest/TestCase.h>
|
2020-09-23 11:31:34 -03:00
|
|
|
|
2023-12-16 11:19:34 -03:00
|
|
|
#include <AK/ByteString.h>
|
2019-07-21 06:34:31 -03:00
|
|
|
#include <AK/WeakPtr.h>
|
2020-09-23 11:31:34 -03:00
|
|
|
#include <AK/Weakable.h>
|
2019-07-21 06:34:31 -03:00
|
|
|
|
2022-10-04 16:04:13 -03:00
|
|
|
#if defined(AK_COMPILER_CLANG)
|
2020-09-23 11:31:34 -03:00
|
|
|
# pragma clang diagnostic push
|
|
|
|
|
# pragma clang diagnostic ignored "-Wunused-private-field"
|
2020-05-16 05:58:29 -03:00
|
|
|
#endif
|
|
|
|
|
|
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 19:26:13 -03:00
|
|
|
class SimpleWeakable : public Weakable<SimpleWeakable>
|
|
|
|
|
, public RefCounted<SimpleWeakable> {
|
2019-07-21 06:34:31 -03:00
|
|
|
public:
|
2021-01-10 20:29:28 -03:00
|
|
|
SimpleWeakable() = default;
|
2019-07-21 06:34:31 -03:00
|
|
|
|
2026-02-25 05:44:32 -03:00
|
|
|
int member() const { return m_member; }
|
|
|
|
|
|
2019-07-21 06:34:31 -03:00
|
|
|
private:
|
|
|
|
|
int m_member { 123 };
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-25 05:44:32 -03:00
|
|
|
class NonRefCountedWeakable : public Weakable<NonRefCountedWeakable> {
|
|
|
|
|
public:
|
|
|
|
|
NonRefCountedWeakable() = default;
|
|
|
|
|
|
|
|
|
|
int value() const { return m_value; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int m_value { 456 };
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-04 16:04:13 -03:00
|
|
|
#if defined(AK_COMPILER_CLANG)
|
2020-09-23 11:31:34 -03:00
|
|
|
# pragma clang diagnostic pop
|
2020-05-16 05:58:29 -03:00
|
|
|
#endif
|
|
|
|
|
|
2019-07-21 06:34:31 -03:00
|
|
|
TEST_CASE(basic_weak)
|
|
|
|
|
{
|
|
|
|
|
WeakPtr<SimpleWeakable> weak1;
|
|
|
|
|
WeakPtr<SimpleWeakable> weak2;
|
|
|
|
|
|
|
|
|
|
{
|
2021-04-23 11:46:57 -03:00
|
|
|
auto simple = adopt_ref(*new SimpleWeakable);
|
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 19:26:13 -03:00
|
|
|
weak1 = simple;
|
|
|
|
|
weak2 = simple;
|
2019-07-21 06:34:31 -03:00
|
|
|
EXPECT_EQ(weak1.is_null(), false);
|
|
|
|
|
EXPECT_EQ(weak2.is_null(), false);
|
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 19:26:13 -03:00
|
|
|
EXPECT_EQ(weak1.strong_ref().ptr(), simple.ptr());
|
|
|
|
|
EXPECT_EQ(weak1.strong_ref().ptr(), weak2.strong_ref().ptr());
|
2019-07-21 06:34:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(weak1.is_null(), true);
|
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 19:26:13 -03:00
|
|
|
EXPECT_EQ(weak1.strong_ref().ptr(), nullptr);
|
|
|
|
|
EXPECT_EQ(weak1.strong_ref().ptr(), weak2.strong_ref().ptr());
|
2019-07-21 06:34:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(weakptr_move)
|
|
|
|
|
{
|
|
|
|
|
WeakPtr<SimpleWeakable> weak1;
|
|
|
|
|
WeakPtr<SimpleWeakable> weak2;
|
|
|
|
|
|
|
|
|
|
{
|
2021-04-23 11:46:57 -03:00
|
|
|
auto simple = adopt_ref(*new SimpleWeakable);
|
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 19:26:13 -03:00
|
|
|
weak1 = simple;
|
2019-07-21 06:34:31 -03:00
|
|
|
weak2 = move(weak1);
|
|
|
|
|
EXPECT_EQ(weak1.is_null(), true);
|
|
|
|
|
EXPECT_EQ(weak2.is_null(), false);
|
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 19:26:13 -03:00
|
|
|
EXPECT_EQ(weak2.strong_ref().ptr(), simple.ptr());
|
2019-07-21 06:34:31 -03:00
|
|
|
}
|
|
|
|
|
|
2019-08-02 04:21:24 -03:00
|
|
|
EXPECT_EQ(weak2.is_null(), true);
|
2019-07-21 06:34:31 -03:00
|
|
|
}
|
2026-02-25 05:44:32 -03:00
|
|
|
|
|
|
|
|
TEST_CASE(weak_callback_ref_counted)
|
|
|
|
|
{
|
|
|
|
|
bool was_called = false;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
auto simple = adopt_ref(*new SimpleWeakable);
|
|
|
|
|
auto cb = weak_callback(*simple, [&was_called](auto& self) {
|
|
|
|
|
was_called = true;
|
|
|
|
|
EXPECT_EQ(self.member(), 123);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
EXPECT(was_called);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(weak_callback_ref_counted_dead)
|
|
|
|
|
{
|
|
|
|
|
auto cb = [&] {
|
|
|
|
|
auto simple = adopt_ref(*new SimpleWeakable);
|
|
|
|
|
return weak_callback(*simple, [](auto&) {
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
});
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(weak_callback_ref_counted_with_args)
|
|
|
|
|
{
|
|
|
|
|
int received_value = 0;
|
|
|
|
|
|
|
|
|
|
auto simple = adopt_ref(*new SimpleWeakable);
|
|
|
|
|
auto cb = weak_callback(*simple, [&received_value](auto& self, int value) {
|
|
|
|
|
received_value = value + self.member();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cb(42);
|
|
|
|
|
EXPECT_EQ(received_value, 42 + 123);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(weak_callback_non_ref_counted)
|
|
|
|
|
{
|
|
|
|
|
bool was_called = false;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
NonRefCountedWeakable obj;
|
|
|
|
|
auto cb = weak_callback(obj, [&was_called](auto& self) {
|
|
|
|
|
was_called = true;
|
|
|
|
|
EXPECT_EQ(self.value(), 456);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
EXPECT(was_called);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(weak_callback_non_ref_counted_dead)
|
|
|
|
|
{
|
|
|
|
|
auto cb = [&] {
|
|
|
|
|
NonRefCountedWeakable obj;
|
|
|
|
|
return weak_callback(obj, [](auto&) {
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
});
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
}
|