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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#include <xenium/reclamation/hazard_eras.hpp>
#include <gtest/gtest.h>
namespace {
struct my_static_allocation_strategy : xenium::reclamation::he_allocation::static_strategy<2>
{
// we are redifining this method in our own strategy to enforce the
// immediate reclamation of nodes.
static constexpr size_t retired_nodes_threshold() { return 0; }
};
struct my_dynamic_allocation_strategy : xenium::reclamation::he_allocation::dynamic_strategy<2>
{
// we are redifining this method in our own strategy to enforce the
// immediate reclamation of nodes.
static constexpr size_t retired_nodes_threshold() { return 0; }
};
template <typename Policy>
struct HazardEras : ::testing::Test
{
using HE = xenium::reclamation::hazard_eras<>::with<xenium::policy::allocation_strategy<Policy>>;
struct Foo : HE::template enable_concurrent_ptr<Foo, 2>
{
Foo** instance;
Foo(Foo** instance) : instance(instance) {}
virtual ~Foo() { if (instance) *instance = nullptr; }
};
struct Bar
{
int x;
virtual ~Bar() {}
};
struct FooBar : Bar, Foo
{
FooBar(Foo** instance) : Foo(instance) {}
};
struct WithCustomDeleter;
struct DummyDeleter {
bool* called;
WithCustomDeleter* reference;
void operator()(WithCustomDeleter* obj) const;
};
struct WithCustomDeleter : HE::template enable_concurrent_ptr<WithCustomDeleter, 2, DummyDeleter> {};
template <typename T>
using concurrent_ptr = typename HE::template concurrent_ptr<T>;
template <typename T> using marked_ptr = typename concurrent_ptr<T>::marked_ptr;
Foo* foo = nullptr;
marked_ptr<Foo> mp{};
void SetUp() override {
this->foo = new Foo(&foo);
this->mp = marked_ptr<Foo>(foo, 3);
}
void TearDown() override
{
// There might be some retired nodes remaining from a testcase that need to be reclaimed.
// In order to do so we create a guard for a dummy object and mark it for reclamation.
// This triggers reclamation of all the objects in the retired_list.
advance_era();
if (mp == nullptr)
assert(foo == nullptr);
else
delete foo;
}
Foo* dummy2 = nullptr;
void advance_era() {
// In order to do advance the global era counter we create a dummy object and mark it for reclamation.
Foo* dummy = new Foo(&dummy2);
{
typename concurrent_ptr<Foo>::guard_ptr gp(dummy);
gp.reclaim();
}
}
};
template <typename Policy>
void HazardEras<Policy>::DummyDeleter::operator()(WithCustomDeleter* obj) const {
*called = true;
EXPECT_EQ(reference, obj);
delete obj;
}
using Policies = ::testing::Types<
my_static_allocation_strategy,
my_dynamic_allocation_strategy
>;
TYPED_TEST_CASE(HazardEras, Policies);
TYPED_TEST(HazardEras, mark_returns_the_same_mark_as_the_original_marked_ptr)
{
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp(this->mp);
EXPECT_EQ(this->mp.mark(), gp.mark());
}
TYPED_TEST(HazardEras, get_returns_the_same_pointer_as_the_original_marked_ptr)
{
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp(this->mp);
}
TYPED_TEST(HazardEras, acquire_guard_acquires_pointer)
{
using concurrent_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>;
concurrent_ptr foo_ptr(this->mp);
typename concurrent_ptr::guard_ptr gp = xenium::acquire_guard(foo_ptr);
EXPECT_EQ(this->mp, gp);
}
TYPED_TEST(HazardEras, additional_acquire_call_do_not_lead_to_overallocation_of_HEs)
{
using concurrent_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>;
concurrent_ptr foo_ptr(this->mp);
typename concurrent_ptr::guard_ptr gp;
gp.acquire(foo_ptr);
gp.acquire(foo_ptr);
gp.acquire(foo_ptr);
}
TYPED_TEST(HazardEras, acquire_if_equal_returns_true_and_acquires_pointer_when_values_are_equal)
{
using concurrent_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>;
concurrent_ptr foo_ptr(this->mp);
typename concurrent_ptr::guard_ptr gp;
EXPECT_TRUE(gp.acquire_if_equal(foo_ptr, this->mp));
EXPECT_EQ(this->mp, gp);
}
TYPED_TEST(HazardEras, acquire_if_equal_returns_false_and_resets_guard_when_values_are_not_equal)
{
using concurrent_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>;
using Foo = typename TestFixture::Foo;
concurrent_ptr foo_ptr(this->mp);
typename concurrent_ptr::guard_ptr gp;
Foo* other = new Foo(&other);
std::unique_ptr<Foo> other_ptr(other);
EXPECT_FALSE(gp.acquire_if_equal(foo_ptr, other));
EXPECT_EQ(nullptr, gp.get());
}
TYPED_TEST(HazardEras, static_policy_throws_bad_hazard_era_alloc_when_HE_pool_is_exceeded)
{
if (std::is_same<TypeParam , my_dynamic_allocation_strategy>::value)
return;
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp1{this->mp};
TestFixture::advance_era();
guard_ptr gp2{this->mp};
TestFixture::advance_era();
EXPECT_THROW(
guard_ptr gp3{this->mp},
xenium::reclamation::bad_hazard_era_alloc
);
}
TYPED_TEST(HazardEras, reset_releases_ownership_and_sets_pointer_to_null)
{
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp(this->mp);
gp.reset();
EXPECT_EQ(nullptr, gp.get());
}
TYPED_TEST(HazardEras, reclaim_releases_ownership_and_deletes_object_because_no_HE_protects_it)
{
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp(this->mp);
gp.reclaim();
this->mp = nullptr;
EXPECT_EQ(nullptr, this->foo);
EXPECT_EQ(nullptr, gp.get());
}
TYPED_TEST(HazardEras, supports_custom_deleters)
{
bool called = false;
using TT = typename TestFixture::WithCustomDeleter;
using Deleter = typename TestFixture::DummyDeleter;
typename TestFixture::HE::template concurrent_ptr<TT>::guard_ptr gp(new TT());
gp.reclaim(Deleter{&called, gp.get()});
EXPECT_TRUE(called);
}
TYPED_TEST(HazardEras, object_cannot_be_reclaimed_as_long_as_another_guard_protects_it)
{
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp(this->mp);
guard_ptr gp2(this->mp);
gp.reclaim();
this->mp = nullptr;
EXPECT_NE(nullptr, this->foo);
}
TYPED_TEST(HazardEras, copy_constructor_leads_to_shared_ownership_preventing_the_object_from_beeing_reclaimed)
{
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp(this->mp);
guard_ptr gp2(gp);
gp.reclaim();
this->mp = nullptr;
EXPECT_NE(nullptr, this->foo);
}
TYPED_TEST(HazardEras, move_constructor_moves_ownership_and_resets_source_object)
{
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp(this->mp);
guard_ptr gp2(std::move(gp));
EXPECT_EQ(nullptr, gp.get());
gp2.reclaim();
this->mp = nullptr;
EXPECT_EQ(nullptr, gp2.get());
EXPECT_EQ(nullptr, this->foo);
}
TYPED_TEST(HazardEras, copy_assignment_leads_to_shared_ownership_preventing_the_object_from_beeing_reclaimed)
{
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp(this->mp);
guard_ptr gp2{};
gp2 = gp;
gp.reclaim();
this->mp = nullptr;
EXPECT_NE(nullptr, this->foo);
}
TYPED_TEST(HazardEras, move_assignment_moves_ownership_and_resets_source_object)
{
using guard_ptr = typename TestFixture::template concurrent_ptr<typename TestFixture::Foo>::guard_ptr;
guard_ptr gp(this->mp);
guard_ptr gp2{};
gp2 = std::move(gp);
gp2.reclaim();
this->mp = nullptr;
EXPECT_EQ(nullptr, gp.get());
EXPECT_EQ(nullptr, this->foo);
}
TYPED_TEST(HazardEras, dynamic_policy_can_protect_more_than_K_objects)
{
if (std::is_same<TypeParam , my_static_allocation_strategy>::value)
return;
const size_t count = 100;
using Foo = typename TestFixture::Foo;
using guard_ptr = typename TestFixture::template concurrent_ptr<Foo>::guard_ptr;
std::vector<Foo*> foos(count);
std::vector<guard_ptr> guards(100);
std::vector<guard_ptr> guards2(100);
for (size_t i = 0; i < count; ++i)
{
foos[i] = new Foo(&foos[i]);
guards[i] = guard_ptr(foos[i]);
guards2[i] = guard_ptr(foos[i]);
}
for (size_t i = 0; i < count; ++i)
guards[i].reclaim();
for (size_t i = 0; i < count; ++i)
ASSERT_NE(nullptr, foos[i]);
for (size_t i = 0; i < count; ++i)
guards2[i].reset();
// reclaim another dummy node to enforce a rescan and reclamation of the retired nodes.
Foo* dummy = new Foo(&dummy);
guard_ptr{dummy}.reclaim();
for (size_t i = 0; i < count; ++i)
EXPECT_EQ(nullptr, foos[i]);
}
}
|