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__) {}
26 size_t id() { return id_; }
29 folly::AtomicIntrusiveLinkedListHook<TestIntrusiveObject> hook_;
33 using List = folly::AtomicIntrusiveLinkedList<TestIntrusiveObject,
34 &TestIntrusiveObject::hook_>;
37 TEST(AtomicIntrusiveLinkedList, Basic) {
38 TestIntrusiveObject a(1), b(2), c(3);
40 TestIntrusiveObject::List list;
42 EXPECT_TRUE(list.empty());
45 EXPECT_TRUE(list.insertHead(&a));
46 EXPECT_FALSE(list.insertHead(&b));
48 EXPECT_FALSE(list.empty());
52 [&](TestIntrusiveObject* obj) mutable { EXPECT_EQ(++id, obj->id()); });
54 EXPECT_TRUE(list.empty());
57 // Try re-inserting the same item (b) and a new item (c)
59 EXPECT_TRUE(list.insertHead(&b));
60 EXPECT_FALSE(list.insertHead(&c));
62 EXPECT_FALSE(list.empty());
66 [&](TestIntrusiveObject* obj) mutable { EXPECT_EQ(++id, obj->id()); });
68 EXPECT_TRUE(list.empty());
71 TestIntrusiveObject::List movedList = std::move(list);
74 TEST(AtomicIntrusiveLinkedList, Move) {
75 TestIntrusiveObject a(1), b(2);
77 TestIntrusiveObject::List list1;
79 EXPECT_TRUE(list1.insertHead(&a));
80 EXPECT_FALSE(list1.insertHead(&b));
82 EXPECT_FALSE(list1.empty());
84 TestIntrusiveObject::List list2(std::move(list1));
86 EXPECT_TRUE(list1.empty());
87 EXPECT_FALSE(list2.empty());
89 TestIntrusiveObject::List list3;
91 EXPECT_TRUE(list3.empty());
93 list3 = std::move(list2);
95 EXPECT_TRUE(list2.empty());
96 EXPECT_FALSE(list3.empty());
100 [&](TestIntrusiveObject* obj) mutable { EXPECT_EQ(++id, obj->id()); });
103 TEST(AtomicIntrusiveLinkedList, Stress) {
104 constexpr size_t kNumThreads = 32;
105 constexpr size_t kNumElements = 100000;
107 std::vector<TestIntrusiveObject> elements;
108 for (size_t i = 0; i < kNumThreads * kNumElements; ++i) {
109 elements.emplace_back(i);
112 TestIntrusiveObject::List list;
114 std::vector<std::thread> threads;
115 for (size_t threadId = 0; threadId < kNumThreads; ++threadId) {
116 threads.emplace_back(
117 [threadId, kNumThreads, kNumElements, &list, &elements]() {
118 for (size_t id = 0; id < kNumElements; ++id) {
119 list.insertHead(&elements[threadId + kNumThreads * id]);
124 std::vector<size_t> ids;
125 TestIntrusiveObject* prev{nullptr};
127 while (ids.size() < kNumThreads * kNumThreads) {
128 list.sweep([&](TestIntrusiveObject* current) {
129 ids.push_back(current->id());
131 if (prev && prev->id() % kNumThreads == current->id() % kNumThreads) {
132 EXPECT_EQ(prev->id() + kNumThreads, current->id());
139 std::sort(ids.begin(), ids.end());
141 for (size_t i = 0; i < kNumThreads * kNumElements; ++i) {
142 EXPECT_EQ(i, ids[i]);
145 for (auto& thread : threads) {
152 TestObject(size_t id__, std::shared_ptr<void> ptr) : id_(id__), ptr_(ptr) {}
154 size_t id() { return id_; }
158 std::shared_ptr<void> ptr_;
161 TEST(AtomicLinkedList, Basic) {
162 constexpr size_t kNumElements = 10;
164 using List = folly::AtomicLinkedList<TestObject>;
167 std::shared_ptr<void> ptr = std::make_shared<int>(42);
169 for (size_t id = 0; id < kNumElements; ++id) {
170 list.insertHead({id, ptr});
175 list.sweep([&](TestObject object) {
176 EXPECT_EQ(counter, object.id());
178 EXPECT_EQ(1 + kNumElements - counter, ptr.use_count());
183 EXPECT_TRUE(ptr.unique());