2 * Copyright 2017 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.
18 #include <folly/Baton.h>
19 #include <folly/experimental/RCURefCount.h>
20 #include <folly/experimental/TLRefCount.h>
21 #include <folly/portability/GTest.h>
25 template <typename RefCount>
27 constexpr size_t numIters = 100000;
28 constexpr size_t numThreads = 10;
36 std::vector<std::thread> ts;
37 folly::Baton<> threadBatons[numThreads];
38 for (size_t t = 0; t < numThreads; ++t) {
39 ts.emplace_back([&count, &b, &got0, t, &threadBatons] {
40 for (size_t i = 0; i < numIters; ++i) {
45 threadBatons[t].post();
53 for (size_t i = 0; i < numIters; ++i) {
58 EXPECT_EQ(numIters - 1, i);
64 for (size_t t = 0; t < numThreads; ++t) {
65 threadBatons[t].wait();
81 EXPECT_EQ(0, ++count);
82 EXPECT_EQ(0, ++count);
85 template <typename RefCount>
86 void stressTest(size_t itersCount) {
87 for (size_t i = 0; i < itersCount; ++i) {
92 std::thread t1([&]() {
95 std::lock_guard<std::mutex> lg(mutex);
102 std::thread t2([&]() {
105 std::lock_guard<std::mutex> lg(mutex);
113 EXPECT_EQ(0, ++count);
117 TEST(RCURefCount, Basic) {
118 basicTest<RCURefCount>();
121 TEST(TLRefCount, Basic) {
122 basicTest<TLRefCount>();
125 TEST(RCURefCount, Stress) {
126 stressTest<RCURefCount>(100000);
129 TEST(TLRefCount, Stress) {
130 // This is absurdly slow, so we can't
131 // do it that many times.
132 stressTest<TLRefCount>(500);