proxygen
Barrier.h
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 #pragma once
17 
18 #include <condition_variable>
19 #include <mutex>
20 
21 #include <stdint.h>
22 
23 namespace folly {
24 
25 namespace test {
26 
27 struct Barrier {
28  explicit Barrier(size_t count) : lock_(), cv_(), count_(count) {}
29 
30  void wait() {
31  std::unique_lock<std::mutex> lockHeld(lock_);
32  auto gen = gen_;
33  if (++num_ == count_) {
34  num_ = 0;
35  gen_++;
36  cv_.notify_all();
37  } else {
38  cv_.wait(lockHeld, [&]() { return gen == gen_; });
39  }
40  }
41 
42  private:
44  std::condition_variable cv_;
45  size_t num_{0};
46  size_t count_;
47  size_t gen_{0};
48 };
49 
50 } // namespace test
51 } // namespace folly
Barrier(size_t count)
Definition: Barrier.h:28
std::mutex lock_
Definition: Barrier.h:43
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::condition_variable cv_
Definition: Barrier.h:44
int * count
std::mutex mutex