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.
19 #include <gtest/gtest.h>
21 #include <folly/AtomicLinkedList.h>
23 class TestIntrusiveObject {
25 explicit TestIntrusiveObject(size_t id__) : id_(id__) {}
31 folly::AtomicIntrusiveLinkedListHook<TestIntrusiveObject> hook_;
35 using List = folly::AtomicIntrusiveLinkedList<
37 &TestIntrusiveObject::hook_>;
40 TEST(AtomicIntrusiveLinkedList, Basic) {
41 TestIntrusiveObject a(1), b(2), c(3);
43 TestIntrusiveObject::List list;
45 EXPECT_TRUE(list.empty());
48 EXPECT_TRUE(list.insertHead(&a));
49 EXPECT_FALSE(list.insertHead(&b));
51 EXPECT_FALSE(list.empty());
55 [&](TestIntrusiveObject* obj) mutable { EXPECT_EQ(++id, obj->id()); });
57 EXPECT_TRUE(list.empty());
60 // Try re-inserting the same item (b) and a new item (c)
62 EXPECT_TRUE(list.insertHead(&b));
63 EXPECT_FALSE(list.insertHead(&c));
65 EXPECT_FALSE(list.empty());
69 [&](TestIntrusiveObject* obj) mutable { EXPECT_EQ(++id, obj->id()); });
71 EXPECT_TRUE(list.empty());
74 TestIntrusiveObject::List movedList = std::move(list);
77 TEST(AtomicIntrusiveLinkedList, Move) {
78 TestIntrusiveObject a(1), b(2);
80 TestIntrusiveObject::List list1;
82 EXPECT_TRUE(list1.insertHead(&a));
83 EXPECT_FALSE(list1.insertHead(&b));
85 EXPECT_FALSE(list1.empty());
87 TestIntrusiveObject::List list2(std::move(list1));
89 EXPECT_TRUE(list1.empty());
90 EXPECT_FALSE(list2.empty());
92 TestIntrusiveObject::List list3;
94 EXPECT_TRUE(list3.empty());
96 list3 = std::move(list2);
98 EXPECT_TRUE(list2.empty());
99 EXPECT_FALSE(list3.empty());
103 [&](TestIntrusiveObject* obj) mutable { EXPECT_EQ(++id, obj->id()); });
106 TEST(AtomicIntrusiveLinkedList, Stress) {
107 constexpr size_t kNumThreads = 32;
108 constexpr size_t kNumElements = 100000;
110 std::vector<TestIntrusiveObject> elements;
111 for (size_t i = 0; i < kNumThreads * kNumElements; ++i) {
112 elements.emplace_back(i);
115 TestIntrusiveObject::List list;
117 std::vector<std::thread> threads;
118 for (size_t threadId = 0; threadId < kNumThreads; ++threadId) {
119 threads.emplace_back(
120 [threadId, kNumThreads, kNumElements, &list, &elements]() {
121 for (size_t id = 0; id < kNumElements; ++id) {
122 list.insertHead(&elements[threadId + kNumThreads * id]);
127 std::vector<size_t> ids;
128 TestIntrusiveObject* prev{nullptr};
130 while (ids.size() < kNumThreads * kNumElements) {
131 list.sweep([&](TestIntrusiveObject* current) {
132 ids.push_back(current->id());
134 if (prev && prev->id() % kNumThreads == current->id() % kNumThreads) {
135 EXPECT_EQ(prev->id() + kNumThreads, current->id());
142 std::sort(ids.begin(), ids.end());
144 for (size_t i = 0; i < kNumThreads * kNumElements; ++i) {
145 EXPECT_EQ(i, ids[i]);
148 for (auto& thread : threads) {
155 TestObject(size_t id__, std::shared_ptr<void> ptr) : id_(id__), ptr_(ptr) {}
163 std::shared_ptr<void> ptr_;
166 TEST(AtomicLinkedList, Basic) {
167 constexpr size_t kNumElements = 10;
169 using List = folly::AtomicLinkedList<TestObject>;
172 std::shared_ptr<void> ptr = std::make_shared<int>(42);
174 for (size_t id = 0; id < kNumElements; ++id) {
175 list.insertHead({id, ptr});
180 list.sweep([&](TestObject object) {
181 EXPECT_EQ(counter, object.id());
183 EXPECT_EQ(1 + kNumElements - counter, ptr.use_count());
188 EXPECT_TRUE(ptr.unique());