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.
18 #include <folly/Baton.h>
19 #include <folly/experimental/RCURefCount.h>
20 #include <folly/experimental/TLRefCount.h>
22 #include <gtest/gtest.h>
26 template <typename RefCount>
28 constexpr size_t numIters = 100000;
29 constexpr size_t numThreads = 10;
37 std::vector<std::thread> ts;
38 folly::Baton<> threadBatons[numThreads];
39 for (size_t t = 0; t < numThreads; ++t) {
40 ts.emplace_back([&count, &b, &got0, numIters, t, &threadBatons]() {
41 for (size_t i = 0; i < numIters; ++i) {
46 threadBatons[t].post();
54 for (size_t i = 0; i < numIters; ++i) {
59 EXPECT_EQ(numIters - 1, i);
65 for (size_t t = 0; t < numThreads; ++t) {
66 threadBatons[t].wait();
82 EXPECT_EQ(0, ++count);
83 EXPECT_EQ(0, ++count);
86 template <typename RefCount>
88 constexpr size_t kItersCount = 10000;
90 for (size_t i = 0; i < kItersCount; ++i) {
95 std::thread t1([&]() {
98 std::lock_guard<std::mutex> lg(mutex);
105 std::thread t2([&]() {
108 std::lock_guard<std::mutex> lg(mutex);
116 EXPECT_EQ(0, ++count);
120 TEST(RCURefCount, Basic) {
121 basicTest<RCURefCount>();
124 TEST(TLRefCount, Basic) {
125 basicTest<TLRefCount>();
128 TEST(RCURefCount, Stress) {
129 stressTest<TLRefCount>();
132 TEST(TLRefCount, Stress) {
133 stressTest<TLRefCount>();