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.
17 #include <gflags/gflags.h>
18 #include <gtest/gtest.h>
22 #include <folly/detail/Futex.h>
23 #include <folly/experimental/LockFreeRingBuffer.h>
24 #include <folly/test/DeterministicSchedule.h>
28 TEST(LockFreeRingBuffer, writeReadSequentially) {
29 const int capacity = 256;
32 LockFreeRingBuffer<int> rb(capacity);
33 LockFreeRingBuffer<int>::Cursor cur = rb.currentHead();
34 for (unsigned int turn = 0; turn < turns; turn++) {
35 for (unsigned int write = 0; write < capacity; write++) {
36 int val = turn*capacity + write;
40 for (unsigned int write = 0; write < capacity; write++) {
42 ASSERT_TRUE(rb.tryRead(dest, cur));
43 ASSERT_EQ(turn*capacity + write, dest);
49 TEST(LockFreeRingBuffer, writeReadSequentiallyBackward) {
50 const int capacity = 256;
53 LockFreeRingBuffer<int> rb(capacity);
54 for (unsigned int turn = 0; turn < turns; turn++) {
55 for (unsigned int write = 0; write < capacity; write++) {
56 int val = turn*capacity + write;
60 LockFreeRingBuffer<int>::Cursor cur = rb.currentHead();
61 cur.moveBackward(1); /// last write
62 for (int write = capacity - 1; write >= 0; write--) {
64 ASSERT_TRUE(rb.tryRead(foo, cur));
65 ASSERT_EQ(turn*capacity + write, foo);
71 TEST(LockFreeRingBuffer, readsCanBlock) {
72 // Start a reader thread, confirm that reading can block
73 std::atomic<bool> readerHasRun(false);
74 LockFreeRingBuffer<int> rb(1);
75 auto cursor = rb.currentHead();
76 cursor.moveForward(3); // wait for the 4th write
78 const int sentinel = 0xfaceb00c;
80 auto reader = std::thread([&]() {
82 EXPECT_TRUE(rb.waitAndTryRead(val, cursor));
84 EXPECT_EQ(sentinel, val);
87 for (int i = 0; i < 4; i++) {
88 EXPECT_FALSE(readerHasRun);
93 EXPECT_TRUE(readerHasRun);
96 // expose the cursor raw value via a wrapper type
97 template<typename T, template<typename> class Atom>
98 uint64_t value(const typename LockFreeRingBuffer<T, Atom>::Cursor& rbcursor) {
99 typedef typename LockFreeRingBuffer<T,Atom>::Cursor RBCursor;
101 struct ExposedCursor : RBCursor {
102 ExposedCursor(const RBCursor& cursor): RBCursor(cursor) {}
107 return ExposedCursor(rbcursor).value();
110 template<template<typename> class Atom>
112 LockFreeRingBuffer<int, Atom>& rb, std::atomic<int32_t>& writes
115 while ((idx = writes--) > 0) {
120 template<template<typename> class Atom>
121 void runWritesNeverFail(
122 int capacity, int writes, int writers
124 using folly::test::DeterministicSchedule;
126 DeterministicSchedule sched(DeterministicSchedule::uniform(0));
127 LockFreeRingBuffer<int, Atom> rb(capacity);
129 std::atomic<int32_t> writes_remaining(writes);
130 std::vector<std::thread> threads(writers);
132 for (int i = 0; i < writers; i++) {
133 threads[i] = DeterministicSchedule::thread(
134 std::bind(runReader<Atom>, std::ref(rb), std::ref(writes_remaining))
138 for (auto& thread : threads) {
139 DeterministicSchedule::join(thread);
142 EXPECT_EQ(writes, (value<int, Atom>)(rb.currentHead()));
145 TEST(LockFreeRingBuffer, writesNeverFail) {
146 using folly::test::DeterministicAtomic;
147 using folly::detail::EmulatedFutexAtomic;
149 runWritesNeverFail<DeterministicAtomic>(1, 100, 4);
150 runWritesNeverFail<DeterministicAtomic>(10, 100, 4);
151 runWritesNeverFail<DeterministicAtomic>(100, 1000, 8);
152 runWritesNeverFail<DeterministicAtomic>(1000, 10000, 16);
154 runWritesNeverFail<std::atomic>(1, 100, 4);
155 runWritesNeverFail<std::atomic>(10, 100, 4);
156 runWritesNeverFail<std::atomic>(100, 1000, 8);
157 runWritesNeverFail<std::atomic>(1000, 10000, 16);
159 runWritesNeverFail<EmulatedFutexAtomic>(1, 100, 4);
160 runWritesNeverFail<EmulatedFutexAtomic>(10, 100, 4);
161 runWritesNeverFail<EmulatedFutexAtomic>(100, 1000, 8);
162 runWritesNeverFail<EmulatedFutexAtomic>(1000, 10000, 16);
165 TEST(LockFreeRingBuffer, readerCanDetectSkips) {
166 const int capacity = 4;
167 const int rounds = 4;
169 LockFreeRingBuffer<int> rb(capacity);
170 auto cursor = rb.currentHead();
171 cursor.moveForward(1);
173 for (int round = 0; round < rounds; round++) {
174 for (int i = 0; i < capacity; i++) {
175 int val = round * capacity + i;
181 EXPECT_FALSE(rb.tryRead(result, cursor));
182 EXPECT_FALSE(rb.waitAndTryRead(result, cursor));
183 EXPECT_EQ(-1, result);
185 cursor = rb.currentTail();
186 EXPECT_TRUE(rb.tryRead(result, cursor));
187 EXPECT_EQ(capacity * (rounds - 1), result);
189 cursor = rb.currentTail(1.0);
190 EXPECT_TRUE(rb.tryRead(result, cursor));
191 EXPECT_EQ((capacity * rounds) - 1, result);
195 TEST(LockFreeRingBuffer, currentTailRange) {
196 const int capacity = 4;
197 LockFreeRingBuffer<int> rb(capacity);
199 // Workaround for template deduction failure
200 auto (&cursorValue)(value<int, std::atomic>);
202 // Empty buffer - everything points to 0
203 EXPECT_EQ(0, cursorValue(rb.currentTail(0)));
204 EXPECT_EQ(0, cursorValue(rb.currentTail(0.5)));
205 EXPECT_EQ(0, cursorValue(rb.currentTail(1)));
212 EXPECT_EQ(0, cursorValue(rb.currentTail(0)));
213 EXPECT_EQ(1, cursorValue(rb.currentTail(1)));
219 EXPECT_EQ(0, cursorValue(rb.currentTail(0)));
220 EXPECT_EQ(3, cursorValue(rb.currentTail(1)));
222 auto midvalue = cursorValue(rb.currentTail(0.5));
223 // both rounding behaviours are acceptable
224 EXPECT_TRUE(midvalue == 1 || midvalue == 2);
227 TEST(LockFreeRingBuffer, cursorFromWrites) {
228 const int capacity = 3;
229 LockFreeRingBuffer<int> rb(capacity);
231 // Workaround for template deduction failure
232 auto (&cursorValue)(value<int, std::atomic>);
234 int val = 0xfaceb00c;
235 EXPECT_EQ(0, cursorValue(rb.writeAndGetCursor(val)));
236 EXPECT_EQ(1, cursorValue(rb.writeAndGetCursor(val)));
237 EXPECT_EQ(2, cursorValue(rb.writeAndGetCursor(val)));
239 // Check that rb is giving out actual cursors and not just
240 // pointing to the current slot.
241 EXPECT_EQ(3, cursorValue(rb.writeAndGetCursor(val)));
244 TEST(LockFreeRingBuffer, moveBackwardsCanFail) {
245 const int capacity = 3;
246 LockFreeRingBuffer<int> rb(capacity);
248 // Workaround for template deduction failure
249 auto (&cursorValue)(value<int, std::atomic>);
251 int val = 0xfaceb00c;
255 auto cursor = rb.currentHead(); // points to 2
256 EXPECT_EQ(2, cursorValue(cursor));
257 EXPECT_TRUE(cursor.moveBackward());
258 EXPECT_TRUE(cursor.moveBackward()); // now at 0
259 EXPECT_FALSE(cursor.moveBackward()); // moving back does nothing