/Users/deen/code/yugabyte-db/src/yb/util/sync_point.cc
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 | | // Copyright (c) 2014, Facebook, Inc. All rights reserved. |
33 | | // This source code is licensed under the BSD-style license found in the |
34 | | // LICENSE file in the root directory of this source tree. An additional grant |
35 | | // of patent rights can be found in the PATENTS file in the same directory. |
36 | | |
37 | | #include "yb/util/sync_point.h" |
38 | | |
39 | | using std::string; |
40 | | using std::vector; |
41 | | |
42 | | #ifndef NDEBUG |
43 | | namespace yb { |
44 | | |
45 | | SyncPoint::Dependency::Dependency(string predecessor, string successor) |
46 | 1 | : predecessor_(std::move(predecessor)), successor_(std::move(successor)) {} |
47 | | |
48 | | SyncPoint::SyncPoint() |
49 | | : cv_(&mutex_), |
50 | 1 | enabled_(false) { |
51 | 1 | } |
52 | | |
53 | 4 | SyncPoint* SyncPoint::GetInstance() { |
54 | 4 | static SyncPoint sync_point; |
55 | 4 | return &sync_point; |
56 | 4 | } |
57 | | |
58 | 1 | void SyncPoint::LoadDependency(const vector<Dependency>& dependencies) { |
59 | 1 | successors_.clear(); |
60 | 1 | predecessors_.clear(); |
61 | 1 | cleared_points_.clear(); |
62 | 1 | for (const Dependency& dependency : dependencies) { |
63 | 1 | successors_[dependency.predecessor_].push_back(dependency.successor_); |
64 | 1 | predecessors_[dependency.successor_].push_back(dependency.predecessor_); |
65 | 1 | } |
66 | 1 | } |
67 | | |
68 | 2 | bool SyncPoint::PredecessorsAllCleared(const string& point) { |
69 | 1 | for (const string& pred : predecessors_[point]) { |
70 | 1 | if (cleared_points_.count(pred) == 0) { |
71 | 0 | return false; |
72 | 0 | } |
73 | 1 | } |
74 | 2 | return true; |
75 | 2 | } |
76 | | |
77 | 1 | void SyncPoint::EnableProcessing() { |
78 | 1 | MutexLock lock(mutex_); |
79 | 1 | enabled_ = true; |
80 | 1 | } |
81 | | |
82 | 0 | void SyncPoint::DisableProcessing() { |
83 | 0 | MutexLock lock(mutex_); |
84 | 0 | enabled_ = false; |
85 | 0 | } |
86 | | |
87 | 0 | void SyncPoint::ClearTrace() { |
88 | 0 | MutexLock lock(mutex_); |
89 | 0 | cleared_points_.clear(); |
90 | 0 | } |
91 | | |
92 | 2 | void SyncPoint::Process(const string& point) { |
93 | 2 | MutexLock lock(mutex_); |
94 | | |
95 | 2 | if (!enabled_) return; |
96 | | |
97 | 2 | while (!PredecessorsAllCleared(point)) { |
98 | 0 | cv_.Wait(); |
99 | 0 | } |
100 | | |
101 | 2 | cleared_points_.insert(point); |
102 | 2 | cv_.Broadcast(); |
103 | 2 | } |
104 | | |
105 | | } // namespace yb |
106 | | #endif // NDEBUG |