xrpld
Loading...
Searching...
No Matches
IntrusiveShared.cpp
1#include <xrpl/basics/IntrusivePointer.h> // IWYU pragma: keep
2#include <xrpl/basics/IntrusivePointer.ipp> // IWYU pragma: keep
3#include <xrpl/basics/IntrusiveRefCounts.h>
4
5#include <gtest/gtest.h>
6
7#include <algorithm>
8#include <array>
9#include <atomic>
10#include <chrono> // IWYU pragma: keep
11#include <condition_variable>
12#include <cstddef>
13#include <cstdint>
14#include <functional>
15#include <latch>
16#include <mutex>
17#include <optional>
18#include <random>
19#include <stdexcept>
20#include <thread>
21#include <utility>
22#include <variant>
23#include <vector>
24
25namespace xrpl::tests {
26
27/*
28 * Experimentally, we discovered that using std::barrier performs extremely
29 * poorly (~1 hour vs ~1 minute to run the test suite) in certain macOS
30 * environments. To unblock our macOS CI pipeline, we replaced std::barrier with a
31 * custom mutex-based barrier (Barrier) that significantly improves performance
32 * without compromising correctness. For future reference, if we ever consider
33 * reintroducing std::barrier, the following configuration is known to exhibit the
34 * problem:
35 *
36 * Model Name: Mac mini
37 * Model Identifier: Mac14,3
38 * Model Number: Z16K000R4LL/A
39 * Chip: Apple M2
40 * Total Number of Cores: 8 (4 performance and 4 efficiency)
41 * Memory: 24 GB
42 * System Firmware Version: 11881.41.5
43 * OS Loader Version: 11881.1.1
44 * Apple clang version 16.0.0 (clang-1600.0.26.3)
45 * Target: arm64-apple-darwin24.0.0
46 * Thread model: posix
47 *
48 */
49struct Barrier
50{
56
57 explicit Barrier(std::size_t n) : count(n), initial(n)
58 {
59 }
60
61 void
63 {
65 auto const currentGeneration = generation;
66 if (--count == 0)
67 {
68 ++generation;
69 count = initial;
70 cv.notify_all();
71 }
72 else
73 {
74 cv.wait(lock, [&] { return generation != currentGeneration; });
75 }
76 }
77};
78
79namespace {
80enum class TrackedState : std::uint8_t {
81 Uninitialized,
82 Alive,
83 PartiallyDeletedStarted,
84 PartiallyDeleted,
85 DeletedStarted,
86 Deleted,
87};
88
89class TIBase : public IntrusiveRefCounts
90{
91public:
92 static constexpr std::size_t kMaxStates = 128;
93 static std::array<std::atomic<TrackedState>, kMaxStates> state;
94 static std::atomic<std::size_t> nextId;
95
96 [[nodiscard]] static TrackedState
97 getState(std::size_t id)
98 {
99 if (id >= state.size())
100 throw std::out_of_range("TIBase state id out of range");
101
102 return state[id].load(std::memory_order_acquire);
103 }
104
105 static void
106 resetStates(bool resetCallback)
107 {
108 for (std::size_t i = 0; i < kMaxStates; ++i)
109 state[i].store(TrackedState::Uninitialized, std::memory_order_release);
110 nextId.store(0, std::memory_order_release);
111 if (resetCallback)
112 TIBase::tracingCallback = [](TrackedState, std::optional<TrackedState>) {};
113 }
114
115 struct ResetStatesGuard
116 {
117 bool resetCallback{false};
118
119 ResetStatesGuard(bool resetCallback) : resetCallback{resetCallback}
120 {
121 TIBase::resetStates(resetCallback);
122 }
123
124 ~ResetStatesGuard()
125 {
126 TIBase::resetStates(resetCallback);
127 }
128 };
129
130 TIBase() : id{checkoutID()}
131 {
132 state[id].store(TrackedState::Alive, std::memory_order_relaxed);
133 }
134
135 ~TIBase() override
136 {
137 using enum TrackedState;
138
139 tracingCallback(state[id].load(std::memory_order_relaxed), DeletedStarted);
140
141 // Use relaxed memory order to try to avoid atomic operations from
142 // adding additional memory synchronizations that may hide threading
143 // errors in the underlying shared pointer class.
144 state[id].store(DeletedStarted, std::memory_order_relaxed);
145
146 tracingCallback(DeletedStarted, Deleted);
147
148 state[id].store(TrackedState::Deleted, std::memory_order_relaxed);
149
150 tracingCallback(TrackedState::Deleted, std::nullopt);
151 }
152
153 void
154 partialDestructor() const
155 {
156 using enum TrackedState;
157
158 tracingCallback(state[id].load(std::memory_order_relaxed), PartiallyDeletedStarted);
159
160 state[id].store(PartiallyDeletedStarted, std::memory_order_relaxed);
161
162 tracingCallback(PartiallyDeletedStarted, PartiallyDeleted);
163
164 state[id].store(PartiallyDeleted, std::memory_order_relaxed);
165
166 tracingCallback(PartiallyDeleted, std::nullopt);
167 }
168
169 static std::function<void(TrackedState, std::optional<TrackedState>)> tracingCallback;
170
171 std::size_t const id;
172
173private:
174 [[nodiscard]] static std::size_t
175 checkoutID()
176 {
177 auto const id = nextId.fetch_add(1, std::memory_order_acq_rel);
178 if (id >= state.size())
179 throw std::out_of_range("TIBase state capacity exceeded");
180
181 return id;
182 }
183};
184
185std::array<std::atomic<TrackedState>, TIBase::kMaxStates> TIBase::state;
186std::atomic<std::size_t> TIBase::nextId{0};
187
188std::function<void(TrackedState, std::optional<TrackedState>)> TIBase::tracingCallback =
189 [](TrackedState, std::optional<TrackedState>) {};
190
191} // namespace
192
193TEST(IntrusiveSharedTest, ref_counts_on_a_bare_object)
194{
195 TIBase::ResetStatesGuard const rsg{true};
196
197 TIBase const b;
198 EXPECT_EQ(b.useCount(), 1);
199 b.addWeakRef();
200 EXPECT_EQ(b.useCount(), 1);
201 auto const s = b.releaseStrongRef();
203 EXPECT_EQ(b.useCount(), 0);
204 TIBase const* pb = &b;
206 EXPECT_FALSE(pb);
207 auto const w = b.releaseWeakRef();
208 EXPECT_EQ(w, ReleaseWeakRefAction::Destroy);
209}
210
211TEST(IntrusiveSharedTest, strong_and_weak_containers_track_liveness)
212{
215 TIBase::ResetStatesGuard const rsg{true};
216
217 using enum TrackedState;
219 auto id = b->id;
220 EXPECT_EQ(TIBase::getState(id), Alive);
221 EXPECT_EQ(b->useCount(), 1);
222 // No reserve(): letting the vectors reallocate also exercises moving the pointers.
223 for (auto i = 0uz; i < 10; ++i)
224 {
225 // NOLINTNEXTLINE(performance-inefficient-vector-operation)
226 strong.push_back(b);
227 }
228 b.reset();
229 EXPECT_EQ(TIBase::getState(id), Alive);
230 strong.resize(strong.size() - 1);
231 EXPECT_EQ(TIBase::getState(id), Alive);
232 strong.clear();
233 EXPECT_EQ(TIBase::getState(id), Deleted);
234
236 id = b->id;
237 EXPECT_EQ(TIBase::getState(id), Alive);
238 EXPECT_EQ(b->useCount(), 1);
239 for (auto i = 0uz; i < 10; ++i)
240 {
241 weak.emplace_back(b);
242 EXPECT_EQ(b->useCount(), 1);
243 }
244 EXPECT_EQ(TIBase::getState(id), Alive);
245 weak.resize(weak.size() - 1);
246 EXPECT_EQ(TIBase::getState(id), Alive);
247 b.reset();
248 EXPECT_EQ(TIBase::getState(id), PartiallyDeleted);
249 while (!weak.empty())
250 {
251 if (weak.resize(weak.size() - 1); !weak.empty())
252 {
253 EXPECT_EQ(TIBase::getState(id), PartiallyDeleted);
254 }
255 }
256 EXPECT_EQ(TIBase::getState(id), Deleted);
257}
258
259TEST(IntrusiveSharedTest, a_weak_pointer_cannot_revive_a_partially_deleted_object)
260{
261 TIBase::ResetStatesGuard const rsg{true};
262
263 using enum TrackedState;
265 auto const id = b->id;
266 EXPECT_EQ(TIBase::getState(id), Alive);
268 EXPECT_EQ(TIBase::getState(id), Alive);
269 auto s = w.lock();
270 EXPECT_TRUE(s && s->useCount() == 2);
271 b.reset();
272 EXPECT_TRUE(TIBase::getState(id) == Alive);
273 EXPECT_TRUE(s && s->useCount() == 1);
274 s.reset();
275 EXPECT_EQ(TIBase::getState(id), PartiallyDeleted);
276 EXPECT_TRUE(w.expired());
277 s = w.lock();
278 // Cannot convert a weak pointer to a strong pointer if object is
279 // already partially deleted
280 EXPECT_FALSE(s);
281 w.reset();
282 EXPECT_EQ(TIBase::getState(id), Deleted);
283}
284
285TEST(IntrusiveSharedTest, shared_weak_union_converts_between_strong_and_weak)
286{
287 TIBase::ResetStatesGuard const rsg{true};
288
289 using enum TrackedState;
290 using SharedWeak = SharedWeakUnion<TIBase>;
291 SharedWeak b = makeSharedIntrusive<TIBase>();
292 EXPECT_TRUE(b.isStrong() && b.useCount() == 1);
293 auto const id = b.get()->id;
294 EXPECT_EQ(TIBase::getState(id), Alive);
295 SharedWeak w = b;
296 EXPECT_TRUE(TIBase::getState(id) == Alive);
297 EXPECT_TRUE(w.isStrong() && b.useCount() == 2);
298 w.convertToWeak();
299 EXPECT_TRUE(w.isWeak() && b.useCount() == 1);
300 SharedWeak s = w;
301 EXPECT_TRUE(s.isWeak() && b.useCount() == 1);
302 s.convertToStrong();
303 EXPECT_TRUE(s.isStrong() && b.useCount() == 2);
304 b.reset();
305 EXPECT_EQ(TIBase::getState(id), Alive);
306 EXPECT_EQ(s.useCount(), 1);
307 EXPECT_FALSE(w.expired());
308 s.reset();
309 EXPECT_EQ(TIBase::getState(id), PartiallyDeleted);
310 EXPECT_TRUE(w.expired());
311 w.convertToStrong();
312 // Cannot convert a weak pointer to a strong pointer if object is
313 // already partially deleted
314 EXPECT_TRUE(w.isWeak());
315 w.reset();
316 EXPECT_EQ(TIBase::getState(id), Deleted);
317}
318
319TEST(IntrusiveSharedTest, shared_weak_union_assignment)
320{
321 // Testing SharedWeakUnion assignment operator
322
323 TIBase::ResetStatesGuard const rsg{true};
324
325 auto const strong1 = makeSharedIntrusive<TIBase>();
326 auto strong2 = makeSharedIntrusive<TIBase>();
327
328 auto const id1 = strong1->id;
329 auto const id2 = strong2->id;
330
331 EXPECT_NE(id1, id2);
332
333 SharedWeakUnion<TIBase> union1 = strong1;
334 SharedWeakUnion<TIBase> union2 = strong2;
335
336 EXPECT_TRUE(union1.isStrong());
337 EXPECT_TRUE(union2.isStrong());
338 EXPECT_EQ(union1.get(), strong1.get());
339 EXPECT_EQ(union2.get(), strong2.get());
340
341 // 1) Normal assignment: explicitly calls SharedWeakUnion assignment
342 union1 = union2;
343 EXPECT_TRUE(union1.isStrong());
344 EXPECT_TRUE(union2.isStrong());
345 EXPECT_EQ(union1.get(), union2.get());
346 EXPECT_EQ(TIBase::getState(id1), TrackedState::Alive);
347 EXPECT_EQ(TIBase::getState(id2), TrackedState::Alive);
348
349 // 2) Test self-assignment
350 EXPECT_TRUE(union1.isStrong());
351 EXPECT_EQ(TIBase::getState(id2), TrackedState::Alive);
352 int const initialRefCount = strong2->useCount();
353#pragma clang diagnostic push
354#pragma clang diagnostic ignored "-Wself-assign-overloaded"
355 union1 = union1; // Self-assignment
356#pragma clang diagnostic pop
357 EXPECT_TRUE(union1.isStrong());
358 EXPECT_EQ(union1.get(), strong2.get());
359 EXPECT_EQ(TIBase::getState(id2), TrackedState::Alive);
360 EXPECT_EQ(strong2->useCount(), initialRefCount);
361
362 // 3) Test assignment from null union pointer
363 union1 = SharedWeakUnion<TIBase>();
364 EXPECT_EQ(union1.get(), nullptr);
365
366 // 4) Test assignment to expired union pointer
367 strong2.reset();
368 union2.reset();
369 union1 = union2;
370 EXPECT_EQ(union1.get(), nullptr);
371 EXPECT_EQ(TIBase::getState(id2), TrackedState::Deleted);
372}
373
374TEST(IntrusiveSharedTest, partial_delete)
375{
376 // This test creates two threads. One with a strong pointer and one
377 // with a weak pointer. The strong pointer is reset while the weak
378 // pointer still holds a reference, triggering a partial delete.
379 // While the partial delete function runs (a sleep is inserted) the
380 // weak pointer is reset. The destructor should wait to run until
381 // after the partial delete function has completed running.
382
383 using enum TrackedState;
384
385 TIBase::ResetStatesGuard const rsg{true};
386
387 auto strong = makeSharedIntrusive<TIBase>();
388 WeakIntrusive<TIBase> weak{strong};
389 std::atomic<bool> destructorRan{false};
390 std::atomic<bool> partialDeleteRan{false};
391 std::latch partialDeleteStartedSyncPoint{2};
392
393 strong->tracingCallback = [&](TrackedState cur, std::optional<TrackedState> next) {
394 using enum TrackedState;
395 if (!next)
396 return;
397
398 switch (*next)
399 {
400 case DeletedStarted:
401 // strong goes out of scope while weak is still in scope
402 // This checks that partialDelete has run to completion
403 // before the destructor is called. A sleep is inserted
404 // inside the partial delete to make sure the destructor is
405 // given an opportunity to run during partial delete.
406 EXPECT_EQ(cur, PartiallyDeleted);
407 break;
408
409 case PartiallyDeletedStarted: {
410 partialDeleteStartedSyncPoint.arrive_and_wait();
411 using namespace std::chrono_literals;
412 // Sleep and let the weak pointer go out of scope,
413 // potentially triggering a destructor while partial delete
414 // is running. The test is to make sure that doesn't happen.
416 break;
417 }
418
419 case PartiallyDeleted:
420 EXPECT_FALSE(partialDeleteRan.exchange(true) || destructorRan.load());
421 break;
422
423 case Deleted:
424 EXPECT_FALSE(destructorRan.exchange(true));
425 break;
426
427 case Uninitialized:
428 case Alive:
429 break;
430 }
431 };
432
433 std::thread t1{[&] {
434 partialDeleteStartedSyncPoint.arrive_and_wait();
435 weak.reset(); // Trigger a full delete as soon as the partial
436 // delete starts
437 }};
438
439 std::thread t2{[&] {
440 strong.reset(); // Trigger a partial delete
441 }};
442
443 t1.join();
444 t2.join();
445
446 EXPECT_TRUE(destructorRan.load() && partialDeleteRan.load());
447}
448
449TEST(IntrusiveSharedTest, convert_last_strong_to_weak)
450{
451 using enum TrackedState;
452
453 TIBase::ResetStatesGuard const rsg{true};
454
456 auto const id = p.get()->id;
457 EXPECT_EQ(TIBase::getState(id), Alive);
458
459 EXPECT_TRUE(p.convertToWeak());
460 EXPECT_TRUE(p.isWeak());
461 EXPECT_TRUE(p.expired());
462 EXPECT_EQ(TIBase::getState(id), PartiallyDeleted);
463
464 p.reset();
465 EXPECT_EQ(TIBase::getState(id), Deleted);
466}
467
468TEST(IntrusiveSharedTest, multithreaded_convert_to_weak_partial_delete)
469{
470 using enum TrackedState;
471
472 TIBase::ResetStatesGuard const rsg{true};
473
475 SharedWeakUnion<TIBase> other = converted;
476 EXPECT_TRUE(other.convertToWeak());
477 EXPECT_EQ(converted.useCount(), 1);
478 auto const id = converted.get()->id;
479
480 std::atomic<bool> destructorRan{false};
481 std::atomic<bool> partialDeleteRan{false};
482 std::latch partialDeleteStartedSyncPoint{1};
483 std::latch otherReleasedSyncPoint{1};
484
485 TIBase::tracingCallback = [&](TrackedState cur, std::optional<TrackedState> next) {
486 if (!next)
487 return;
488
489 switch (*next)
490 {
491 case DeletedStarted:
492 EXPECT_EQ(cur, PartiallyDeleted);
493 break;
494
495 case PartiallyDeletedStarted:
496 partialDeleteStartedSyncPoint.count_down();
497 // Keep the partial delete running until the other weak pointer has been released.
498 otherReleasedSyncPoint.wait();
499 break;
500
501 case PartiallyDeleted:
502 EXPECT_FALSE(partialDeleteRan.exchange(true) || destructorRan.load());
503 break;
504
505 case Deleted:
506 EXPECT_FALSE(destructorRan.exchange(true));
507 break;
508
509 case Uninitialized:
510 case Alive:
511 break;
512 }
513 };
514
515 std::thread t1{[&] {
516 partialDeleteStartedSyncPoint.wait();
517 other.reset(); // Not the last weak ref, so must not trigger a delete
518 otherReleasedSyncPoint.count_down();
519 }};
520
521 std::thread t2{[&] {
522 EXPECT_TRUE(converted.convertToWeak()); // Trigger a partial delete
523 }};
524
525 t1.join();
526 t2.join();
527
528 EXPECT_TRUE(partialDeleteRan.load());
529 EXPECT_FALSE(destructorRan.load());
530 EXPECT_TRUE(converted.isWeak());
531 EXPECT_EQ(TIBase::getState(id), PartiallyDeleted);
532
533 converted.reset(); // Release the last weak ref
534 EXPECT_TRUE(destructorRan.load());
535 EXPECT_EQ(TIBase::getState(id), Deleted);
536}
537
538TEST(IntrusiveSharedTest, destructor)
539{
540 // This test creates two threads. One with a strong pointer and one
541 // with a weak pointer. The weak pointer is reset while the strong
542 // pointer still holds a reference. Then the strong pointer is
543 // reset. Only the destructor should run. The partial destructor
544 // should not be called. Since the weak reset runs to completion
545 // before the strong pointer is reset, threading doesn't add much to
546 // this test, but there is no harm in keeping it.
547
548 using enum TrackedState;
549
550 TIBase::ResetStatesGuard const rsg{true};
551
552 auto strong = makeSharedIntrusive<TIBase>();
553 WeakIntrusive<TIBase> weak{strong};
554 std::atomic<bool> destructorRan{false};
555 std::atomic<bool> partialDeleteRan{false};
556 std::latch weakResetSyncPoint{2};
557 strong->tracingCallback = [&](TrackedState cur, std::optional<TrackedState> next) {
558 using enum TrackedState;
559 if (!next)
560 return;
561
562 switch (*next)
563 {
564 case PartiallyDeleted:
565 EXPECT_FALSE(partialDeleteRan.exchange(true) || destructorRan.load());
566 break;
567
568 case Deleted:
569 EXPECT_FALSE(destructorRan.exchange(true));
570 break;
571
572 case Uninitialized:
573 case Alive:
574 case PartiallyDeletedStarted:
575 case DeletedStarted:
576 break;
577 }
578 };
579 std::thread t1{[&] {
580 weak.reset();
581 weakResetSyncPoint.arrive_and_wait();
582 }};
583 std::thread t2{[&] {
584 weakResetSyncPoint.arrive_and_wait();
585 strong.reset(); // Trigger a partial delete
586 }};
587 t1.join();
588 t2.join();
589
590 EXPECT_TRUE(destructorRan.load() && !partialDeleteRan.load());
591}
592
593TEST(IntrusiveSharedTest, multithreaded_clear_mixed_variant)
594{
595 // This test creates and destroys many strong and weak pointers in a
596 // loop. There is a random mix of strong and weak pointers stored in
597 // a vector (held as a variant). Both threads clear all the pointers
598 // and check that the invariants hold.
599
600 using enum TrackedState;
601 TIBase::ResetStatesGuard const rsg{true};
602
603 std::atomic<int> destructionState{0};
604 // returns destructorRan and partialDestructorRan (in that order)
605 auto getDestructorState = [&] -> std::pair<bool, bool> {
606 int const s = destructionState.load(std::memory_order_relaxed);
607 return {(s & 1) != 0, (s & 2) != 0};
608 };
609 auto setDestructorRan = [&] -> void {
610 destructionState.fetch_or(1, std::memory_order_acq_rel);
611 };
612 auto setPartialDeleteRan = [&] -> void {
613 destructionState.fetch_or(2, std::memory_order_acq_rel);
614 };
615 auto tracingCallback = [&](TrackedState cur, std::optional<TrackedState> next) {
616 using enum TrackedState;
617 auto [destructorRan, partialDeleteRan] = getDestructorState();
618 if (!next)
619 return;
620
621 switch (*next)
622 {
623 case PartiallyDeleted:
624 EXPECT_FALSE(partialDeleteRan || destructorRan);
625 setPartialDeleteRan();
626 break;
627
628 case Deleted:
629 EXPECT_FALSE(destructorRan);
630 setDestructorRan();
631 break;
632
633 case Uninitialized:
634 case Alive:
635 case PartiallyDeletedStarted:
636 case DeletedStarted:
637 break;
638 }
639 };
640 auto createVecOfPointers = [&](auto const& toClone, std::default_random_engine& eng)
644 std::uniform_int_distribution<> isStrongDist(0, 1);
645 auto const numToCreate = toCreateDist(eng);
646 result.reserve(numToCreate);
647 for (auto i = 0uz; i < numToCreate; ++i)
648 {
649 if (isStrongDist(eng))
650 {
651 result.emplace_back(SharedIntrusive<TIBase>(toClone));
652 }
653 else
654 {
655 result.emplace_back(WeakIntrusive<TIBase>(toClone));
656 }
657 }
658 return result;
659 };
660 constexpr auto kLoopIters = 2uz * 1024;
661 constexpr auto kNumThreads = 16uz;
663 Barrier loopStartSyncPoint{kNumThreads};
664 Barrier postCreateToCloneSyncPoint{kNumThreads};
665 Barrier postCreateVecOfPointersSyncPoint{kNumThreads};
666 auto engines = [&] -> std::vector<std::default_random_engine> {
669 result.reserve(kNumThreads);
670 for (auto i = 0uz; i < kNumThreads; ++i)
671 result.emplace_back(rd());
672 return result;
673 }();
674
675 // cloneAndDestroy clones the strong pointer into a vector of mixed
676 // strong and weak pointers and destroys them all at once.
677 // threadId==0 is special.
678 auto const cloneAndDestroy = [&](std::size_t threadId) {
679 for (auto i = 0uz; i < kLoopIters; ++i)
680 {
681 // ------ Sync Point ------
682 loopStartSyncPoint.arriveAndWait();
683
684 // only thread 0 should reset the state
686 if (threadId == 0)
687 {
688 // Thread 0 is the genesis thread. It creates the strong
689 // pointers to be cloned by the other threads. This
690 // thread will also check that the destructor ran and
691 // clear the temporary variables.
692
693 rsg.emplace(false);
694 auto [destructorRan, partialDeleteRan] = getDestructorState();
695 EXPECT_TRUE(i == 0 || destructorRan);
696 destructionState.store(0, std::memory_order_release);
697
698 toClone.clear();
699 toClone.resize(kNumThreads);
700 auto const strong = makeSharedIntrusive<TIBase>();
701 strong->tracingCallback = tracingCallback;
702 std::ranges::fill(toClone, strong);
703 }
704
705 // ------ Sync Point ------
706 postCreateToCloneSyncPoint.arriveAndWait();
707
708 auto v = createVecOfPointers(toClone[threadId], engines[threadId]);
709 toClone[threadId].reset();
710
711 // ------ Sync Point ------
712 postCreateVecOfPointersSyncPoint.arriveAndWait();
713
714 v.clear();
715 }
716 };
718 threads.reserve(kNumThreads);
719 for (auto i = 0uz; i < kNumThreads; ++i)
720 {
721 threads.emplace_back(cloneAndDestroy, i);
722 }
723 for (auto i = 0uz; i < kNumThreads; ++i)
724 {
725 threads[i].join();
726 }
727}
728
729TEST(IntrusiveSharedTest, multithreaded_clear_mixed_union)
730{
731 // This test creates and destroys many SharedWeak pointers in a
732 // loop. All the pointers start as strong and a loop randomly
733 // convert them between strong and weak pointers. Both threads clear
734 // all the pointers and check that the invariants hold.
735 //
736 // Note: This test also differs from the test above in that the pointers
737 // randomly change from strong to weak and from weak to strong in a
738 // loop. This can't be done in the variant test above because variant is
739 // not thread safe while the SharedWeakUnion is thread safe.
740
741 using enum TrackedState;
742
743 TIBase::ResetStatesGuard const rsg{true};
744
745 std::atomic<int> destructionState{0};
746 // returns destructorRan and partialDestructorRan (in that order)
747 auto getDestructorState = [&] -> std::pair<bool, bool> {
748 int const s = destructionState.load(std::memory_order_relaxed);
749 return {(s & 1) != 0, (s & 2) != 0};
750 };
751 auto setDestructorRan = [&] -> void {
752 destructionState.fetch_or(1, std::memory_order_acq_rel);
753 };
754 auto setPartialDeleteRan = [&] -> void {
755 destructionState.fetch_or(2, std::memory_order_acq_rel);
756 };
757 auto tracingCallback = [&](TrackedState cur, std::optional<TrackedState> next) {
758 using enum TrackedState;
759 auto [destructorRan, partialDeleteRan] = getDestructorState();
760 if (!next)
761 return;
762
763 switch (*next)
764 {
765 case PartiallyDeleted:
766 EXPECT_FALSE(partialDeleteRan || destructorRan);
767 setPartialDeleteRan();
768 break;
769
770 case Deleted:
771 EXPECT_FALSE(destructorRan);
772 setDestructorRan();
773 break;
774
775 case Uninitialized:
776 case Alive:
777 case PartiallyDeletedStarted:
778 case DeletedStarted:
779 break;
780 }
781 };
782 auto createVecOfPointers =
783 [&](auto const& toClone,
787 auto const numToCreate = toCreateDist(eng);
788 result.reserve(numToCreate);
789 for (auto i = 0uz; i < numToCreate; ++i)
790 result.emplace_back(SharedIntrusive<TIBase>(toClone));
791 return result;
792 };
793 constexpr auto kLoopIters = 2uz * 1024;
794 constexpr auto kFlipPointersLoopIters = 256uz;
795 constexpr auto kNumThreads = 16uz;
797 Barrier loopStartSyncPoint{kNumThreads};
798 Barrier postCreateToCloneSyncPoint{kNumThreads};
799 Barrier postCreateVecOfPointersSyncPoint{kNumThreads};
800 Barrier postFlipPointersLoopSyncPoint{kNumThreads};
801 auto engines = [&] -> std::vector<std::default_random_engine> {
804 result.reserve(kNumThreads);
805 for (auto i = 0uz; i < kNumThreads; ++i)
806 result.emplace_back(rd());
807 return result;
808 }();
809
810 // cloneAndDestroy clones the strong pointer into a vector of
811 // mixed strong and weak pointers, runs a loop that randomly
812 // changes strong pointers to weak pointers, and destroys them
813 // all at once.
814 auto const cloneAndDestroy = [&](std::size_t threadId) {
815 for (auto i = 0uz; i < kLoopIters; ++i)
816 {
817 // ------ Sync Point ------
818 loopStartSyncPoint.arriveAndWait();
819
820 // only thread 0 should reset the state
822 if (threadId == 0)
823 {
824 // threadId 0 is the genesis thread. It creates the
825 // strong point to be cloned by the other threads. This
826 // thread will also check that the destructor ran and
827 // clear the temporary variables.
828 rsg.emplace(false);
829 auto [destructorRan, partialDeleteRan] = getDestructorState();
830 EXPECT_TRUE(i == 0 || destructorRan);
831 destructionState.store(0, std::memory_order_release);
832
833 toClone.clear();
834 toClone.resize(kNumThreads);
835 auto const strong = makeSharedIntrusive<TIBase>();
836 strong->tracingCallback = tracingCallback;
837 std::ranges::fill(toClone, strong);
838 }
839
840 // ------ Sync Point ------
841 postCreateToCloneSyncPoint.arriveAndWait();
842
843 auto v = createVecOfPointers(toClone[threadId], engines[threadId]);
844 toClone[threadId].reset();
845
846 // ------ Sync Point ------
847 postCreateVecOfPointersSyncPoint.arriveAndWait();
848
849 std::uniform_int_distribution<> isStrongDist(0, 1);
850 for (auto f = 0uz; f < kFlipPointersLoopIters; ++f)
851 {
852 for (auto& p : v)
853 {
854 if (isStrongDist(engines[threadId]))
855 {
856 p.convertToStrong();
857 }
858 else
859 {
860 p.convertToWeak();
861 }
862 }
863 }
864
865 // ------ Sync Point ------
866 postFlipPointersLoopSyncPoint.arriveAndWait();
867
868 v.clear();
869 }
870 };
872 threads.reserve(kNumThreads);
873 for (auto i = 0uz; i < kNumThreads; ++i)
874 {
875 threads.emplace_back(cloneAndDestroy, i);
876 }
877 for (auto i = 0uz; i < kNumThreads; ++i)
878 {
879 threads[i].join();
880 }
881}
882
883TEST(IntrusiveSharedTest, multithreaded_locking_weak)
884{
885 // This test creates a single shared atomic pointer that multiple thread
886 // create weak pointers from. The threads then lock the weak pointers.
887 // Both threads clear all the pointers and check that the invariants
888 // hold.
889
890 using enum TrackedState;
891
892 TIBase::ResetStatesGuard const rsg{true};
893
894 std::atomic<int> destructionState{0};
895 // returns destructorRan and partialDestructorRan (in that order)
896 auto getDestructorState = [&] -> std::pair<bool, bool> {
897 int const s = destructionState.load(std::memory_order_relaxed);
898 return {(s & 1) != 0, (s & 2) != 0};
899 };
900 auto setDestructorRan = [&] -> void {
901 destructionState.fetch_or(1, std::memory_order_acq_rel);
902 };
903 auto setPartialDeleteRan = [&] -> void {
904 destructionState.fetch_or(2, std::memory_order_acq_rel);
905 };
906 auto tracingCallback = [&](TrackedState cur, std::optional<TrackedState> next) {
907 using enum TrackedState;
908 auto [destructorRan, partialDeleteRan] = getDestructorState();
909 if (!next)
910 return;
911
912 switch (*next)
913 {
914 case PartiallyDeleted:
915 EXPECT_FALSE(partialDeleteRan || destructorRan);
916 setPartialDeleteRan();
917 break;
918
919 case Deleted:
920 EXPECT_FALSE(destructorRan);
921 setDestructorRan();
922 break;
923
924 case Uninitialized:
925 case Alive:
926 case PartiallyDeletedStarted:
927 case DeletedStarted:
928 break;
929 }
930 };
931
932 constexpr auto kLoopIters = 2uz * 1024;
933 constexpr auto kLockWeakLoopIters = 256uz;
934 constexpr auto kNumThreads = 16uz;
936 Barrier loopStartSyncPoint{kNumThreads};
937 Barrier postCreateToLockSyncPoint{kNumThreads};
938 Barrier postLockWeakLoopSyncPoint{kNumThreads};
939
940 // lockAndDestroy creates weak pointers from the strong pointer
941 // and runs a loop that locks the weak pointer. At the end of the loop
942 // all the pointers are destroyed all at once.
943 auto const lockAndDestroy = [&](std::size_t threadId) {
944 for (auto i = 0uz; i < kLoopIters; ++i)
945 {
946 // ------ Sync Point ------
947 loopStartSyncPoint.arriveAndWait();
948
949 // only thread 0 should reset the state
951 if (threadId == 0)
952 {
953 // threadId 0 is the genesis thread. It creates the
954 // strong point to be locked by the other threads. This
955 // thread will also check that the destructor ran and
956 // clear the temporary variables.
957 rsg.emplace(false);
958 auto [destructorRan, partialDeleteRan] = getDestructorState();
959 EXPECT_TRUE(i == 0 || destructorRan);
960 destructionState.store(0, std::memory_order_release);
961
962 toLock.clear();
963 toLock.resize(kNumThreads);
964 auto const strong = makeSharedIntrusive<TIBase>();
965 strong->tracingCallback = tracingCallback;
966 std::ranges::fill(toLock, strong);
967 }
968
969 // ------ Sync Point ------
970 postCreateToLockSyncPoint.arriveAndWait();
971
972 // Multiple threads all create a weak pointer from the same
973 // strong pointer
974 WeakIntrusive const weak{toLock[threadId]};
975 for (auto wi = 0uz; wi < kLockWeakLoopIters; ++wi)
976 {
977 EXPECT_FALSE(weak.expired());
978 auto const strong = weak.lock();
979 EXPECT_TRUE(strong);
980 }
981
982 // ------ Sync Point ------
983 postLockWeakLoopSyncPoint.arriveAndWait();
984
985 toLock[threadId].reset();
986 }
987 };
989 threads.reserve(kNumThreads);
990 for (auto i = 0uz; i < kNumThreads; ++i)
991 {
992 threads.emplace_back(lockAndDestroy, i);
993 }
994 for (auto i = 0uz; i < kNumThreads; ++i)
995 {
996 threads[i].join();
997 }
998}
999
1000} // namespace xrpl::tests
T arrive_and_wait(T... args)
A shared intrusive pointer class that supports weak pointers.
A combination of a strong and a weak intrusive pointer stored in the space of a single pointer.
bool expired() const
Return true if there is a non-zero strong count.
void reset()
Set the pointer to null, decrement the appropriate ref count, and run the appropriate release action.
T * get() const
If this is a strong pointer, return the raw pointer.
bool convertToWeak()
If this is a strong pointer, attempt to convert it to a weak pointer.
bool isStrong() const
Return true is this represents a strong pointer.
std::size_t useCount() const
If this is a strong pointer, return the strong count.
bool isWeak() const
Return true is this represents a weak pointer.
A weak intrusive pointer class for the SharedIntrusive pointer class.
SharedIntrusive< T > lock() const
Get a strong pointer from the weak pointer, if possible.
bool expired() const
Return true if the strong count is zero.
void reset()
Set the pointer to null and decrement the weak count.
T clear(T... args)
T count_down(T... args)
T emplace_back(T... args)
T emplace(T... args)
T empty(T... args)
T exchange(T... args)
T fetch_or(T... args)
T fill(T... args)
T join(T... args)
T load(T... args)
TEST(IntrusiveSharedTest, ref_counts_on_a_bare_object)
void partialDestructorFinished(T **o)
SharedIntrusive< TT > makeSharedIntrusive(Args &&... args)
Create a shared intrusive pointer.
T push_back(T... args)
T reserve(T... args)
T resize(T... args)
T size(T... args)
T sleep_for(T... args)
T store(T... args)
std::condition_variable cv
std::size_t const initial
T wait(T... args)