YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/semaphore_macosx.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
#include <semaphore.h>
33
34
#include <glog/logging.h>
35
36
#include "yb/util/semaphore.h"
37
38
namespace yb {
39
40
Semaphore::Semaphore(int capacity)
41
111k
  : count_(capacity) {
42
111k
  DCHECK_GE(capacity, 0);
43
44
111k
}
45
46
49.9k
Semaphore::~Semaphore() {
47
49.9k
}
48
49
0
void Semaphore::Acquire() {
50
0
  std::unique_lock<std::mutex> lock(mutex_);
51
0
  cv_.wait(lock, [this] { return count_.load() > 0; });
52
0
  CHECK_GT(count_.load(), 0);
53
0
  count_.fetch_sub(1);
54
0
}
55
56
56.6k
bool Semaphore::TryAcquire() {
57
56.6k
  if (count_.load() > 0) {
58
56.6k
    std::unique_lock<std::mutex> lock(mutex_);
59
56.6k
    if (count_.load() > 0) {
60
56.6k
      count_.fetch_sub(1);
61
56.6k
      return true;
62
56.6k
    }
63
1
  }
64
1
  return false;
65
1
}
66
67
0
bool Semaphore::TimedAcquire(const MonoDelta& timeout) {
68
0
  auto time_stop = MonoTime::Now();
69
0
  time_stop.AddDelta(timeout);
70
0
  std::unique_lock<std::mutex> lock(mutex_);
71
0
  if (!cv_.wait_until(lock, time_stop.ToSteadyTimePoint(), [this] { return count_.load() > 0; })) {
72
0
    return false;
73
0
  }
74
0
  CHECK_GT(count_.load(), 0);
75
0
  count_.fetch_sub(1);
76
0
  return true;
77
0
}
78
79
56.5k
void Semaphore::Release() {
80
56.5k
  std::unique_lock<std::mutex> lock(mutex_);
81
56.5k
  CHECK_GE(count_.load(), 0);
82
56.5k
  count_.fetch_add(1);
83
56.5k
  cv_.notify_all();
84
56.5k
}
85
86
6.61M
int Semaphore::GetValue() {
87
6.61M
  return count_.load();
88
6.61M
}
89
90
} // namespace yb