/Users/deen/code/yugabyte-db/src/yb/util/barrier.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #ifndef YB_UTIL_BARRIER_H |
34 | | #define YB_UTIL_BARRIER_H |
35 | | #pragma once |
36 | | |
37 | | #include "yb/gutil/macros.h" |
38 | | #include "yb/util/condition_variable.h" |
39 | | #include "yb/util/mutex.h" |
40 | | #include "yb/util/thread_restrictions.h" |
41 | | |
42 | | namespace yb { |
43 | | |
44 | | // Implementation of pthread-style Barriers. |
45 | | class Barrier { |
46 | | public: |
47 | | // Initialize the barrier with the given initial count. |
48 | | explicit Barrier(size_t count) : |
49 | | cond_(&mutex_), |
50 | | count_(count), |
51 | 3 | wait_count_(count) { |
52 | 3 | } |
53 | | |
54 | | // Wait until all threads have reached the barrier. |
55 | | // Once all threads have reached the barrier, it is reset to the initial count. |
56 | 18 | void Wait() { |
57 | 18 | WaitImpl(false /* detach */); |
58 | 18 | } |
59 | | |
60 | | // Wait until all threads have reached the barrier. |
61 | | // Once all threads have reached the barrier, it is reset to the initial count minus 1. |
62 | | // This method must be called in case thread is finished. |
63 | | // Other threads will not wait for it on next loop. |
64 | 0 | void Detach() { |
65 | 0 | WaitImpl(true /* detach */); |
66 | 0 | } |
67 | | |
68 | | private: |
69 | 18 | void WaitImpl(bool detach) { |
70 | 18 | ThreadRestrictions::AssertWaitAllowed(); |
71 | 18 | MutexLock l(mutex_); |
72 | 18 | if (detach) { |
73 | 0 | DCHECK_GT(wait_count_, 0); |
74 | 0 | --wait_count_; |
75 | 0 | } |
76 | 18 | if (--count_ == 0) { |
77 | 3 | count_ = wait_count_; |
78 | 3 | ++cycle_count_; |
79 | 3 | cond_.Broadcast(); |
80 | 3 | return; |
81 | 3 | } |
82 | | |
83 | 30 | for (const auto initial_cycle = cycle_count_; cycle_count_ == initial_cycle;) { |
84 | 15 | cond_.Wait(); |
85 | 15 | } |
86 | 15 | } |
87 | | |
88 | | Mutex mutex_; |
89 | | ConditionVariable cond_; |
90 | | size_t count_; |
91 | | size_t cycle_count_ = 0; |
92 | | size_t wait_count_; |
93 | | DISALLOW_COPY_AND_ASSIGN(Barrier); |
94 | | }; |
95 | | |
96 | | } // namespace yb |
97 | | #endif // YB_UTIL_BARRIER_H |