2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <gtest/gtest.h>
22 #include <folly/AtomicLinkedList.h>
24 class TestIntrusiveObject {
26 explicit TestIntrusiveObject(size_t id__) : id_(id__) {}
32 folly::AtomicIntrusiveLinkedListHook<TestIntrusiveObject> hook_;
36 using List = folly::AtomicIntrusiveLinkedList<
38 &TestIntrusiveObject::hook_>;
41 TEST(AtomicIntrusiveLinkedList, Basic) {
42 TestIntrusiveObject a(1), b(2), c(3);
44 TestIntrusiveObject::List list;
46 EXPECT_TRUE(list.empty());
49 EXPECT_TRUE(list.insertHead(&a));
50 EXPECT_FALSE(list.insertHead(&b));
52 EXPECT_FALSE(list.empty());
56 [&](TestIntrusiveObject* obj) mutable { EXPECT_EQ(++id, obj->id()); });
58 EXPECT_TRUE(list.empty());
61 // Try re-inserting the same item (b) and a new item (c)
63 EXPECT_TRUE(list.insertHead(&b));
64 EXPECT_FALSE(list.insertHead(&c));
66 EXPECT_FALSE(list.empty());
70 [&](TestIntrusiveObject* obj) mutable { EXPECT_EQ(++id, obj->id()); });
72 EXPECT_TRUE(list.empty());
75 TestIntrusiveObject::List movedList = std::move(list);
78 TEST(AtomicIntrusiveLinkedList, Move) {
79 TestIntrusiveObject a(1), b(2);
81 TestIntrusiveObject::List list1;
83 EXPECT_TRUE(list1.insertHead(&a));
84 EXPECT_FALSE(list1.insertHead(&b));
86 EXPECT_FALSE(list1.empty());
88 TestIntrusiveObject::List list2(std::move(list1));
90 EXPECT_TRUE(list1.empty());
91 EXPECT_FALSE(list2.empty());
93 TestIntrusiveObject::List list3;
95 EXPECT_TRUE(list3.empty());
97 list3 = std::move(list2);
99 EXPECT_TRUE(list2.empty());
100 EXPECT_FALSE(list3.empty());
104 [&](TestIntrusiveObject* obj) mutable { EXPECT_EQ(++id, obj->id()); });
107 TEST(AtomicIntrusiveLinkedList, Stress) {
108 constexpr size_t kNumThreads = 32;
109 constexpr size_t kNumElements = 100000;
111 std::vector<TestIntrusiveObject> elements;
112 for (size_t i = 0; i < kNumThreads * kNumElements; ++i) {
113 elements.emplace_back(i);
116 TestIntrusiveObject::List list;
118 std::vector<std::thread> threads;
119 for (size_t threadId = 0; threadId < kNumThreads; ++threadId) {
120 threads.emplace_back(
121 [threadId, kNumThreads, kNumElements, &list, &elements]() {
122 for (size_t id = 0; id < kNumElements; ++id) {
123 list.insertHead(&elements[threadId + kNumThreads * id]);
128 std::vector<size_t> ids;
129 TestIntrusiveObject* prev{nullptr};
131 while (ids.size() < kNumThreads * kNumElements) {
132 list.sweep([&](TestIntrusiveObject* current) {
133 ids.push_back(current->id());
135 if (prev && prev->id() % kNumThreads == current->id() % kNumThreads) {
136 EXPECT_EQ(prev->id() + kNumThreads, current->id());
143 std::sort(ids.begin(), ids.end());
145 for (size_t i = 0; i < kNumThreads * kNumElements; ++i) {
146 EXPECT_EQ(i, ids[i]);
149 for (auto& thread : threads) {
156 TestObject(size_t id__, std::shared_ptr<void> ptr) : id_(id__), ptr_(ptr) {}
164 std::shared_ptr<void> ptr_;
167 TEST(AtomicLinkedList, Basic) {
168 constexpr size_t kNumElements = 10;
170 using List = folly::AtomicLinkedList<TestObject>;
173 std::shared_ptr<void> ptr = std::make_shared<int>(42);
175 for (size_t id = 0; id < kNumElements; ++id) {
176 list.insertHead({id, ptr});
181 list.sweep([&](TestObject object) {
182 EXPECT_EQ(counter, object.id());
184 EXPECT_EQ(1 + kNumElements - counter, ptr.use_count());
189 EXPECT_TRUE(ptr.unique());