proxygen
ThreadLocalDestroyBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #include <folly/Benchmark.h>
18 #include <folly/ThreadLocal.h>
19 #include <condition_variable>
20 #include <mutex>
21 #include <thread>
22 
23 using namespace folly;
24 
25 template <typename Tag1, typename Tag2>
26 void runTestTag(int iters, int numThreads) {
27  BenchmarkSuspender susp;
28 
30 
31  t1.reset(new int(1));
32 
33  std::mutex m, mw;
34  std::condition_variable cv, cvw;
35  bool running = true;
36  int numRunning = 0;
37 
38  std::vector<std::thread> threads;
39  for (int i = 0; i < numThreads; i++) {
40  threads.push_back(std::thread([&]() mutable {
41  t1.reset(new int(1));
42 
43  // notify if all the threads have created the t1
44  bool notify = false;
45  {
46  std::lock_guard<std::mutex> lk(m);
47  if (++numRunning == numThreads) {
48  notify = true;
49  }
50  }
51 
52  if (notify) {
53  cv.notify_one();
54  }
55 
56  // now wait
57  {
58  std::unique_lock<std::mutex> lk(mw);
59  cvw.wait(lk, [&]() { return !running; });
60  }
61  }));
62  }
63 
64  // wait for the threads to create the t1
65  {
66  std::unique_lock<std::mutex> lk(m);
67  cv.wait(lk, [&]() { return numRunning == numThreads; });
68  }
69 
71  t3.reset(new int(2));
72 
73  susp.dismiss();
74 
75  // run the test loop
76  for (int i = 0; i < iters; i++) {
78  t2.reset(new int(2));
79  }
80 
81  susp.rehire();
82 
83  {
84  std::lock_guard<std::mutex> lk(mw);
85  running = false;
86  }
87 
88  cvw.notify_all();
89 
90  for (auto& t : threads) {
91  t.join();
92  }
93 }
94 
95 void runTestSameTag(int iters, int numThreads) {
96  runTestTag<void, void>(iters, numThreads);
97 }
98 
99 void runTestDiffTag(int iters, int numThreads) {
100  runTestTag<void, int>(iters, numThreads);
101 }
102 
118 
119 int main(int argc, char* argv[]) {
120  gflags::ParseCommandLineFlags(&argc, &argv, true);
122 
123  return 0;
124 }
int main(int argc, char *argv[])
void reset(T *newPtr=nullptr)
Definition: ThreadLocal.h:176
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
void runTestSameTag(int iters, int numThreads)
std::vector< std::thread::id > threads
void runTestTag(int iters, int numThreads)
char ** argv
void runTestDiffTag(int iters, int numThreads)
static map< string, int > m
#define BENCHMARK_PARAM(name, param)
Definition: Benchmark.h:417
std::mutex mutex
BENCHMARK_DRAW_LINE()