/Users/deen/code/yugabyte-db/src/yb/util/countdown_latch.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/util/countdown_latch.h" |
15 | | |
16 | | #include "yb/util/thread_restrictions.h" |
17 | | |
18 | | namespace yb { |
19 | | |
20 | | CountDownLatch::CountDownLatch(uint64_t count) |
21 | | : cond_(&lock_), |
22 | 9.41M | count_(count) { |
23 | 9.41M | } |
24 | | |
25 | 8.10M | CountDownLatch::~CountDownLatch() { |
26 | | // Lock mutex to synchronize deletion. |
27 | | // All locks of this mutex are short, so we don't wait. |
28 | 8.10M | MutexLock lock(lock_); |
29 | 8.10M | } |
30 | | |
31 | 8.61M | void CountDownLatch::CountDown(uint64_t amount) { |
32 | 8.61M | MutexLock lock(lock_); |
33 | 8.61M | auto existing_value = count_.load(std::memory_order_relaxed); |
34 | 8.61M | if (existing_value == 0) { |
35 | 43 | return; |
36 | 43 | } |
37 | | |
38 | 8.61M | if (amount >= existing_value) { |
39 | 8.22M | count_.store(0, std::memory_order_release); |
40 | | // Latch has triggered. |
41 | 8.22M | cond_.Broadcast(); |
42 | 8.22M | } else { |
43 | 390k | count_.store(existing_value - amount, std::memory_order_release); |
44 | 390k | } |
45 | 8.61M | } |
46 | | |
47 | 7.03M | void CountDownLatch::Wait() const { |
48 | 7.03M | if (count_.load(std::memory_order_acquire) == 0) { |
49 | 94.8k | return; |
50 | 94.8k | } |
51 | 6.93M | ThreadRestrictions::AssertWaitAllowed(); |
52 | 6.93M | MutexLock lock(lock_); |
53 | 13.8M | while (count_.load(std::memory_order_relaxed) > 0) { |
54 | 6.93M | cond_.Wait(); |
55 | 6.93M | } |
56 | 6.93M | } |
57 | | |
58 | 285k | bool CountDownLatch::WaitUntil(CoarseTimePoint when) const { |
59 | 285k | return WaitUntil(ToSteady(when)); |
60 | 285k | } |
61 | | |
62 | 1.15M | bool CountDownLatch::WaitUntil(MonoTime deadline) const { |
63 | 1.15M | if (count_.load(std::memory_order_acquire) == 0) { |
64 | 53.5k | return true; |
65 | 53.5k | } |
66 | 1.10M | ThreadRestrictions::AssertWaitAllowed(); |
67 | 1.10M | MutexLock lock(lock_); |
68 | 1.43M | while (count_.load(std::memory_order_relaxed) > 0) { |
69 | 1.10M | if (!cond_.WaitUntil(deadline)) { |
70 | 778k | return false; |
71 | 778k | } |
72 | 1.10M | } |
73 | 326k | return true; |
74 | 1.10M | } |
75 | | |
76 | 872k | bool CountDownLatch::WaitFor(MonoDelta delta) const { |
77 | 872k | return WaitUntil(MonoTime::Now() + delta); |
78 | 872k | } |
79 | | |
80 | 203k | void CountDownLatch::Reset(uint64_t count) { |
81 | 203k | MutexLock lock(lock_); |
82 | 203k | count_.store(count, std::memory_order_release); |
83 | 203k | if (count != 0) { |
84 | 203k | return; |
85 | 203k | } |
86 | | // Awake any waiters if we reset to 0. |
87 | 250 | cond_.Broadcast(); |
88 | 250 | } |
89 | | |
90 | 5.06M | uint64_t CountDownLatch::count() const { |
91 | 5.06M | return count_.load(std::memory_order_acquire); |
92 | 5.06M | } |
93 | | |
94 | | } // namespace yb |