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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include <xenium/reclamation/generic_epoch_based.hpp>
#include <gtest/gtest.h>
namespace {
using Reclaimer = xenium::reclamation::generic_epoch_based<>::with<xenium::policy::scan_frequency<0>>;
struct Foo : Reclaimer::enable_concurrent_ptr<Foo, 2>
{
Foo** instance;
Foo(Foo** instance) : instance(instance) {}
virtual ~Foo() { if (instance) *instance = nullptr; }
};
template <typename T>
using concurrent_ptr = Reclaimer::concurrent_ptr<T>;
template <typename T> using marked_ptr = typename concurrent_ptr<T>::marked_ptr;
struct GenericEpochBased : testing::Test
{
Foo* foo = new Foo(&foo);
marked_ptr<Foo> mp = marked_ptr<Foo>(foo, 3);
void update_epoch()
{
// UpdateThreshold is set to 0, so we simply need create a guard_ptr to some dummy object
// to trigger and epoch update.
Foo dummy(nullptr);
concurrent_ptr<Foo>::guard_ptr gp(&dummy);
}
void wrap_around_epochs()
{
update_epoch();
update_epoch();
update_epoch();
}
void TearDown() override
{
wrap_around_epochs();
if (mp == nullptr)
assert(foo == nullptr);
else
delete foo;
}
};
TEST_F(GenericEpochBased, 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(GenericEpochBased, 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(GenericEpochBased, reset_releases_ownership_and_sets_pointer_to_null)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
gp.reset();
EXPECT_EQ(nullptr, gp.get());
}
TEST_F(GenericEpochBased, reclaim_releases_ownership_and_the_object_gets_deleted_when_advancing_two_epochs)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
gp.reclaim();
this->mp = nullptr;
EXPECT_EQ(nullptr, gp.get());
EXPECT_NE(nullptr, foo);
wrap_around_epochs();
EXPECT_EQ(nullptr, foo);
}
struct WithCustomDeleter;
struct DummyDeleter {
bool* called;
WithCustomDeleter* reference;
void operator()(WithCustomDeleter* obj) const;
};
struct WithCustomDeleter : Reclaimer::enable_concurrent_ptr<WithCustomDeleter, 2, DummyDeleter> {};
void DummyDeleter::operator()(WithCustomDeleter* obj) const {
*called = true;
EXPECT_EQ(reference, obj);
delete obj;
}
TEST_F(GenericEpochBased, supports_custom_deleters)
{
bool called = false;
concurrent_ptr<WithCustomDeleter>::guard_ptr gp(new WithCustomDeleter());
gp.reclaim(DummyDeleter{&called, gp.get()});
wrap_around_epochs();
EXPECT_TRUE(called);
}
TEST_F(GenericEpochBased, object_cannot_be_reclaimed_as_long_as_another_guard_protects_it)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
concurrent_ptr<Foo>::guard_ptr gp2(mp);
gp.reclaim();
wrap_around_epochs();
EXPECT_NE(nullptr, foo);
}
TEST_F(GenericEpochBased, copy_constructor_leads_to_shared_ownership_preventing_the_object_from_beeing_reclaimed)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
concurrent_ptr<Foo>::guard_ptr gp2(gp);
gp.reclaim();
this->mp = nullptr;
wrap_around_epochs();
EXPECT_NE(nullptr, foo);
}
TEST_F(GenericEpochBased, move_constructor_moves_ownership_and_resets_source_object)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
concurrent_ptr<Foo>::guard_ptr gp2(std::move(gp));
EXPECT_EQ(nullptr, gp.get());
gp2.reclaim();
this->mp = nullptr;
wrap_around_epochs();
EXPECT_EQ(nullptr, foo);
}
TEST_F(GenericEpochBased, copy_assignment_leads_to_shared_ownership_preventing_the_object_from_beeing_reclaimed)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
concurrent_ptr<Foo>::guard_ptr gp2{};
gp2 = gp;
gp.reclaim();
this->mp = nullptr;
wrap_around_epochs();
EXPECT_NE(nullptr, foo);
}
TEST_F(GenericEpochBased, move_assignment_moves_ownership_and_resets_source_object)
{
concurrent_ptr<Foo>::guard_ptr gp(mp);
concurrent_ptr<Foo>::guard_ptr gp2{};
gp2 = std::move(gp);
EXPECT_EQ(nullptr, gp.get());
gp2.reclaim();
this->mp = nullptr;
wrap_around_epochs();
EXPECT_EQ(nullptr, gp.get());
EXPECT_EQ(nullptr, foo);
}
}
|