1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <xenium/reclamation/lock_free_ref_count.hpp>
#include <gtest/gtest.h>
#include <thread>
namespace {
struct Foo : xenium::reclamation::lock_free_ref_count<>::enable_concurrent_ptr<Foo, 2>
{
Foo** instance;
Foo(Foo** instance) : instance(instance) {}
virtual ~Foo() { *instance = nullptr; }
};
template <typename T> using concurrent_ptr = xenium::reclamation::lock_free_ref_count<>::concurrent_ptr<T>;
template <typename T> using marked_ptr = typename concurrent_ptr<T>::marked_ptr;
template <typename T> using guard_ptr = typename concurrent_ptr<T>::guard_ptr;
struct LockFreeRefCount : testing::Test
{
Foo* foo = new Foo(&foo);
marked_ptr<Foo> mp = marked_ptr<Foo>(foo, 3);
void TearDown() override { delete foo; }
};
TEST_F(LockFreeRefCount, inital_ref_count_value_is_one)
{
EXPECT_EQ(1u, foo->refs());
}
TEST_F(LockFreeRefCount, mark_returns_the_same_mark_as_the_original_marked_ptr)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
EXPECT_EQ(mp.mark(), gp.mark());
}
TEST_F(LockFreeRefCount, get_returns_the_same_pointer_as_the_original_marked_ptr)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
EXPECT_EQ(mp.get(), gp.get());
}
TEST_F(LockFreeRefCount, reset_releases_ownership)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
gp.reset();
EXPECT_EQ(nullptr, gp.get());
}
TEST_F(LockFreeRefCount, reclaim_releases_ownership_and_deletes_object_if_ref_count_drops_to_zero)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
gp.reclaim();
EXPECT_EQ(nullptr, foo);
EXPECT_EQ(nullptr, gp.get());
}
TEST_F(LockFreeRefCount, guard_increments_ref_count)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
EXPECT_EQ(2u, mp->refs());
}
TEST_F(LockFreeRefCount, copy_constructor_increments_ref_count)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
concurrent_ptr<Foo>::guard_ptr gp2(gp);
EXPECT_EQ(3u, mp->refs());
}
TEST_F(LockFreeRefCount, move_constructor_does_not_increment_ref_count_and_resets_source)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
concurrent_ptr<Foo>::guard_ptr gp2(std::move(gp));
EXPECT_EQ(2u, mp->refs());
EXPECT_EQ(nullptr, gp.get());
}
TEST_F(LockFreeRefCount, copy_assignment_increments_ref_count)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
concurrent_ptr<Foo>::guard_ptr gp2{};
gp2 = gp;
EXPECT_EQ(3u, mp->refs());
}
TEST_F(LockFreeRefCount, move_assignment_does_not_increment_ref_count_and_resets_source)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
concurrent_ptr<Foo>::guard_ptr gp2{};
gp2 = std::move(gp);
EXPECT_EQ(2u, mp->refs());
EXPECT_EQ(nullptr, gp.get());
}
TEST_F(LockFreeRefCount, guard_destructor_decrements_ref_count)
{
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
}
EXPECT_EQ(1u, foo->refs());
}
TEST_F(LockFreeRefCount, parallel_allocation_and_deallocation_of_nodes)
{
struct Dummy : xenium::reclamation::lock_free_ref_count<>::enable_concurrent_ptr<Dummy> {};
std::vector<std::thread> threads;
for (int i = 0; i < 16; ++i)
{
threads.push_back(std::thread([]
{
const int MaxIterations = 10000;
for (int j = 0; j < MaxIterations; ++j)
{
auto o = new Dummy;
guard_ptr<Dummy> g(new Dummy);
delete o;
g.reclaim();
}
}));
}
for (auto& thread : threads)
thread.join();
}
}
|