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 <gtest/gtest.h>
21 #include <folly/detail/Futex.h>
22 #include <folly/experimental/LockFreeRingBuffer.h>
23 #include <folly/test/DeterministicSchedule.h>
27 TEST(LockFreeRingBuffer, writeReadSequentially) {
28 const int capacity = 256;
31 LockFreeRingBuffer<int> rb(capacity);
32 LockFreeRingBuffer<int>::Cursor cur = rb.currentHead();
33 for (unsigned int turn = 0; turn < turns; turn++) {
34 for (unsigned int write = 0; write < capacity; write++) {
35 int val = turn*capacity + write;
39 for (unsigned int write = 0; write < capacity; write++) {
41 ASSERT_TRUE(rb.tryRead(dest, cur));
42 ASSERT_EQ(turn*capacity + write, dest);
48 TEST(LockFreeRingBuffer, writeReadSequentiallyBackward) {
49 const int capacity = 256;
52 LockFreeRingBuffer<int> rb(capacity);
53 for (unsigned int turn = 0; turn < turns; turn++) {
54 for (unsigned int write = 0; write < capacity; write++) {
55 int val = turn*capacity + write;
59 LockFreeRingBuffer<int>::Cursor cur = rb.currentHead();
60 cur.moveBackward(1); /// last write
61 for (int write = capacity - 1; write >= 0; write--) {
63 ASSERT_TRUE(rb.tryRead(foo, cur));
64 ASSERT_EQ(turn*capacity + write, foo);
70 TEST(LockFreeRingBuffer, readsCanBlock) {
71 // Start a reader thread, confirm that reading can block
72 std::atomic<bool> readerHasRun(false);
73 LockFreeRingBuffer<int> rb(1);
74 auto cursor = rb.currentHead();
75 cursor.moveForward(3); // wait for the 4th write
77 const int sentinel = 0xfaceb00c;
79 auto reader = std::thread([&]() {
81 EXPECT_TRUE(rb.waitAndTryRead(val, cursor));
83 EXPECT_EQ(sentinel, val);
86 for (int i = 0; i < 4; i++) {
87 EXPECT_FALSE(readerHasRun);
92 EXPECT_TRUE(readerHasRun);
95 // expose the cursor raw value via a wrapper type
96 template<typename T, template<typename> class Atom>
97 uint64_t value(const typename LockFreeRingBuffer<T, Atom>::Cursor& rbcursor) {
98 typedef typename LockFreeRingBuffer<T,Atom>::Cursor RBCursor;
100 struct ExposedCursor : RBCursor {
101 ExposedCursor(const RBCursor& cursor): RBCursor(cursor) {}
106 return ExposedCursor(rbcursor).value();
109 template<template<typename> class Atom>
111 LockFreeRingBuffer<int, Atom>& rb, std::atomic<int32_t>& writes
114 while ((idx = writes--) > 0) {
119 template<template<typename> class Atom>
120 void runWritesNeverFail(
121 int capacity, int writes, int writers
123 using folly::test::DeterministicSchedule;
125 DeterministicSchedule sched(DeterministicSchedule::uniform(0));
126 LockFreeRingBuffer<int, Atom> rb(capacity);
128 std::atomic<int32_t> writes_remaining(writes);
129 std::vector<std::thread> threads(writers);
131 for (int i = 0; i < writers; i++) {
132 threads[i] = DeterministicSchedule::thread(
133 std::bind(runReader<Atom>, std::ref(rb), std::ref(writes_remaining))
137 for (auto& thread : threads) {
138 DeterministicSchedule::join(thread);
141 EXPECT_EQ(writes, (value<int, Atom>)(rb.currentHead()));
144 TEST(LockFreeRingBuffer, writesNeverFail) {
145 using folly::test::DeterministicAtomic;
146 using folly::detail::EmulatedFutexAtomic;
148 runWritesNeverFail<DeterministicAtomic>(1, 100, 4);
149 runWritesNeverFail<DeterministicAtomic>(10, 100, 4);
150 runWritesNeverFail<DeterministicAtomic>(100, 1000, 8);
151 runWritesNeverFail<DeterministicAtomic>(1000, 10000, 16);
153 runWritesNeverFail<std::atomic>(1, 100, 4);
154 runWritesNeverFail<std::atomic>(10, 100, 4);
155 runWritesNeverFail<std::atomic>(100, 1000, 8);
156 runWritesNeverFail<std::atomic>(1000, 10000, 16);
158 runWritesNeverFail<EmulatedFutexAtomic>(1, 100, 4);
159 runWritesNeverFail<EmulatedFutexAtomic>(10, 100, 4);
160 runWritesNeverFail<EmulatedFutexAtomic>(100, 1000, 8);
161 runWritesNeverFail<EmulatedFutexAtomic>(1000, 10000, 16);
164 TEST(LockFreeRingBuffer, readerCanDetectSkips) {
165 const int capacity = 4;
166 const int rounds = 4;
168 LockFreeRingBuffer<int> rb(capacity);
169 auto cursor = rb.currentHead();
170 cursor.moveForward(1);
172 for (int round = 0; round < rounds; round++) {
173 for (int i = 0; i < capacity; i++) {
174 int val = round * capacity + i;
180 EXPECT_FALSE(rb.tryRead(result, cursor));
181 EXPECT_FALSE(rb.waitAndTryRead(result, cursor));
182 EXPECT_EQ(-1, result);
184 cursor = rb.currentTail();
185 EXPECT_TRUE(rb.tryRead(result, cursor));
186 EXPECT_EQ(capacity * (rounds - 1), result);
188 cursor = rb.currentTail(1.0);
189 EXPECT_TRUE(rb.tryRead(result, cursor));
190 EXPECT_EQ((capacity * rounds) - 1, result);
194 TEST(LockFreeRingBuffer, currentTailRange) {
195 const int capacity = 4;
196 LockFreeRingBuffer<int> rb(capacity);
198 // Workaround for template deduction failure
199 auto (&cursorValue)(value<int, std::atomic>);
201 // Empty buffer - everything points to 0
202 EXPECT_EQ(0, cursorValue(rb.currentTail(0)));
203 EXPECT_EQ(0, cursorValue(rb.currentTail(0.5)));
204 EXPECT_EQ(0, cursorValue(rb.currentTail(1)));
211 EXPECT_EQ(0, cursorValue(rb.currentTail(0)));
212 EXPECT_EQ(1, cursorValue(rb.currentTail(1)));
218 EXPECT_EQ(0, cursorValue(rb.currentTail(0)));
219 EXPECT_EQ(3, cursorValue(rb.currentTail(1)));
221 auto midvalue = cursorValue(rb.currentTail(0.5));
222 // both rounding behaviours are acceptable
223 EXPECT_TRUE(midvalue == 1 || midvalue == 2);
226 TEST(LockFreeRingBuffer, cursorFromWrites) {
227 const int capacity = 3;
228 LockFreeRingBuffer<int> rb(capacity);
230 // Workaround for template deduction failure
231 auto (&cursorValue)(value<int, std::atomic>);
233 int val = 0xfaceb00c;
234 EXPECT_EQ(0, cursorValue(rb.writeAndGetCursor(val)));
235 EXPECT_EQ(1, cursorValue(rb.writeAndGetCursor(val)));
236 EXPECT_EQ(2, cursorValue(rb.writeAndGetCursor(val)));
238 // Check that rb is giving out actual cursors and not just
239 // pointing to the current slot.
240 EXPECT_EQ(3, cursorValue(rb.writeAndGetCursor(val)));
243 TEST(LockFreeRingBuffer, moveBackwardsCanFail) {
244 const int capacity = 3;
245 LockFreeRingBuffer<int> rb(capacity);
247 // Workaround for template deduction failure
248 auto (&cursorValue)(value<int, std::atomic>);
250 int val = 0xfaceb00c;
254 auto cursor = rb.currentHead(); // points to 2
255 EXPECT_EQ(2, cursorValue(cursor));
256 EXPECT_TRUE(cursor.moveBackward());
257 EXPECT_TRUE(cursor.moveBackward()); // now at 0
258 EXPECT_FALSE(cursor.moveBackward()); // moving back does nothing