Skip to content

Commit

Permalink
a hazptr test for recursive while-protected retirement
Browse files Browse the repository at this point in the history
Reviewed By: Gownta

Differential Revision: D46405364

fbshipit-source-id: 6cbc914e8202dd8c7a0448909b00daafe43ed850
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Jun 8, 2023
1 parent bf03f50 commit e992255
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions folly/synchronization/test/HazptrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,40 @@ void destruction_test(hazptr_domain<Atom>& domain) {
hazptr_cleanup<Atom>(domain);
}

template <template <typename> class Atom = std::atomic>
void destruction_protected_test(hazptr_domain<Atom>& domain) {
struct Rec;

struct RecState {
hazptr_domain<Atom>& domain;
Atom<Rec*> cell{};
};

struct Rec : hazptr_obj_base<Rec, Atom> {
int rem_;
RecState& state_;

Rec(int rem, RecState& state) : rem_{rem}, state_{state} {}
~Rec() { go(rem_, state_); }

static void go(int rem, RecState& state) {
if (rem) {
auto p = new Rec(rem - 1, state);
state.cell.store(p, std::memory_order_relaxed);
auto h = make_hazard_pointer(state.domain);
h.protect(state.cell);
state.cell.store(nullptr, std::memory_order_relaxed);
p->retire(state.domain);
}
}
};

RecState state{domain};
Rec::go(2000, state);

hazptr_cleanup<Atom>(domain);
}

template <template <typename> class Atom = std::atomic>
void move_test() {
for (int i = 0; i < 100; ++i) {
Expand Down Expand Up @@ -1113,6 +1147,24 @@ TEST_F(HazptrPreInitTest, dsched_destruction) {
default_hazptr_domain<DeterministicAtomic>());
}

TEST(HazptrTest, destruction_protected) {
{
hazptr_domain<> myDomain0;
destruction_protected_test(myDomain0);
}
destruction_protected_test(default_hazptr_domain<std::atomic>());
}

TEST_F(HazptrPreInitTest, dsched_destruction_protected) {
DSched sched(DSched::uniform(0));
{
hazptr_domain<DeterministicAtomic> myDomain0;
destruction_protected_test<DeterministicAtomic>(myDomain0);
}
destruction_protected_test<DeterministicAtomic>(
default_hazptr_domain<DeterministicAtomic>());
}

TEST(HazptrTest, move) {
move_test();
}
Expand Down

0 comments on commit e992255

Please sign in to comment.