YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/semaphore.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
#ifndef YB_UTIL_SEMAPHORE_H
33
#define YB_UTIL_SEMAPHORE_H
34
35
#include <semaphore.h>
36
#if defined(__APPLE__)
37
#include <dispatch/dispatch.h>
38
#include "yb/util/atomic.h"
39
#endif  // define(__APPLE__)
40
41
#include "yb/gutil/macros.h"
42
#include "yb/gutil/port.h"
43
#include "yb/util/monotime.h"
44
45
namespace yb {
46
47
// Wrapper for POSIX semaphores.
48
class Semaphore {
49
 public:
50
  // Initialize the semaphore with the specified capacity.
51
  explicit Semaphore(int capacity);
52
  ~Semaphore();
53
54
  // Acquire the semaphore.
55
  void Acquire();
56
57
  // Acquire the semaphore within the given timeout. Returns true if successful.
58
  bool TimedAcquire(const MonoDelta& timeout);
59
60
  // Try to acquire the semaphore immediately. Returns false if unsuccessful.
61
  bool TryAcquire();
62
63
  // Release the semaphore.
64
  void Release();
65
66
  // Get the current value of the semaphore.
67
  int GetValue();
68
69
  // Boost-compatible wrappers.
70
0
  void lock() { Acquire(); }
71
11
  void unlock() { Release(); }
72
22
  bool try_lock() { return TryAcquire(); }
73
74
 private:
75
#if !defined(__APPLE__)
76
  // Log a fatal error message. Separated out to keep the main functions
77
  // as small as possible in terms of code size.
78
  void Fatal(const char* action) ATTRIBUTE_NORETURN;
79
#endif  // !define(__APPLE__)
80
81
#if defined(__APPLE__)
82
  std::atomic<int32_t> count_;
83
  std::condition_variable cv_;
84
  // Mutex to use with the condition variable cv_.
85
  std::mutex mutex_;
86
#else
87
  sem_t sem_;
88
#endif  // define(__APPLE__)
89
  DISALLOW_COPY_AND_ASSIGN(Semaphore);
90
};
91
92
} // namespace yb
93
#endif /* YB_UTIL_SEMAPHORE_H */