2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-04-25 08:53:23 +03:00
|
|
|
#include <LibTest/TestCase.h>
|
2020-09-23 17:31:34 +03:00
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.h>
|
2019-07-21 12:34:31 +03:00
|
|
|
#include <AK/WeakPtr.h>
|
2020-09-23 17:31:34 +03:00
|
|
|
#include <AK/Weakable.h>
|
2019-07-21 12:34:31 +03:00
|
|
|
|
2022-10-04 22:04:13 +03:00
|
|
|
#if defined(AK_COMPILER_CLANG)
|
2020-09-23 17:31:34 +03:00
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wunused-private-field"
|
2020-05-16 11: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-30 01:26:13 +03:00
|
|
|
class SimpleWeakable : public Weakable<SimpleWeakable>
|
|
|
|
, public RefCounted<SimpleWeakable> {
|
2019-07-21 12:34:31 +03:00
|
|
|
public:
|
2021-01-11 02:29:28 +03:00
|
|
|
SimpleWeakable() = default;
|
2019-07-21 12:34:31 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_member { 123 };
|
|
|
|
};
|
|
|
|
|
2022-10-04 22:04:13 +03:00
|
|
|
#if defined(AK_COMPILER_CLANG)
|
2020-09-23 17:31:34 +03:00
|
|
|
# pragma clang diagnostic pop
|
2020-05-16 11:58:29 +03:00
|
|
|
#endif
|
|
|
|
|
2019-07-21 12:34:31 +03:00
|
|
|
TEST_CASE(basic_weak)
|
|
|
|
{
|
|
|
|
WeakPtr<SimpleWeakable> weak1;
|
|
|
|
WeakPtr<SimpleWeakable> weak2;
|
|
|
|
|
|
|
|
{
|
2021-04-23 17: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-30 01:26:13 +03:00
|
|
|
weak1 = simple;
|
|
|
|
weak2 = simple;
|
2019-07-21 12: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-30 01: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 12: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-30 01:26:13 +03:00
|
|
|
EXPECT_EQ(weak1.strong_ref().ptr(), nullptr);
|
|
|
|
EXPECT_EQ(weak1.strong_ref().ptr(), weak2.strong_ref().ptr());
|
2019-07-21 12:34:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(weakptr_move)
|
|
|
|
{
|
|
|
|
WeakPtr<SimpleWeakable> weak1;
|
|
|
|
WeakPtr<SimpleWeakable> weak2;
|
|
|
|
|
|
|
|
{
|
2021-04-23 17: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-30 01:26:13 +03:00
|
|
|
weak1 = simple;
|
2019-07-21 12: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-30 01:26:13 +03:00
|
|
|
EXPECT_EQ(weak2.strong_ref().ptr(), simple.ptr());
|
2019-07-21 12:34:31 +03:00
|
|
|
}
|
|
|
|
|
2019-08-02 10:21:24 +03:00
|
|
|
EXPECT_EQ(weak2.is_null(), true);
|
2019-07-21 12:34:31 +03:00
|
|
|
}
|