YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/unique_lock.h
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
#ifndef YB_UTIL_UNIQUE_LOCK_H
14
#define YB_UTIL_UNIQUE_LOCK_H
15
16
#include <condition_variable>
17
#include <mutex>
18
19
#include "yb/gutil/thread_annotations.h"
20
21
namespace yb {
22
23
#if THREAD_ANNOTATIONS_ENABLED
24
25
// ------------------------------------------------------------------------------------------------
26
// Thread annotations enabled, using a UniqueLock wrapper class around std::unique_lock.
27
// ------------------------------------------------------------------------------------------------
28
29
2.44M
#define UNIQUE_LOCK(lock_name, mutex) ::yb::UniqueLock<decltype(mutex)> lock_name(mutex);
30
31
// A wrapper unique_lock that supports thread annotations.
32
template<typename Mutex>
33
class SCOPED_CAPABILITY UniqueLock {
34
 public:
35
95.7M
  explicit UniqueLock(Mutex &mutex) ACQUIRE(mutex) : unique_lock_(mutex) {}
yb::UniqueLock<std::__1::mutex>::UniqueLock(std::__1::mutex&)
Line
Count
Source
35
95.7M
  explicit UniqueLock(Mutex &mutex) ACQUIRE(mutex) : unique_lock_(mutex) {}
yb::UniqueLock<std::__1::shared_timed_mutex>::UniqueLock(std::__1::shared_timed_mutex&)
Line
Count
Source
35
31
  explicit UniqueLock(Mutex &mutex) ACQUIRE(mutex) : unique_lock_(mutex) {}
yb::UniqueLock<yb::percpu_rwlock>::UniqueLock(yb::percpu_rwlock&)
Line
Count
Source
35
165
  explicit UniqueLock(Mutex &mutex) ACQUIRE(mutex) : unique_lock_(mutex) {}
36
37
  explicit UniqueLock(Mutex &mutex, std::defer_lock_t defer) : unique_lock_(mutex, defer) {}
38
39
95.7M
  ~UniqueLock() RELEASE() = default;
yb::UniqueLock<std::__1::mutex>::~UniqueLock()
Line
Count
Source
39
95.7M
  ~UniqueLock() RELEASE() = default;
yb::UniqueLock<std::__1::shared_timed_mutex>::~UniqueLock()
Line
Count
Source
39
31
  ~UniqueLock() RELEASE() = default;
yb::UniqueLock<yb::percpu_rwlock>::~UniqueLock()
Line
Count
Source
39
165
  ~UniqueLock() RELEASE() = default;
40
41
495k
  void unlock() RELEASE() { unique_lock_.unlock(); }
yb::UniqueLock<std::__1::mutex>::unlock()
Line
Count
Source
41
494k
  void unlock() RELEASE() { unique_lock_.unlock(); }
yb::UniqueLock<yb::percpu_rwlock>::unlock()
Line
Count
Source
41
165
  void unlock() RELEASE() { unique_lock_.unlock(); }
42
15.6k
  void lock() ACQUIRE() { unique_lock_.lock(); }
yb::UniqueLock<std::__1::mutex>::lock()
Line
Count
Source
42
15.4k
  void lock() ACQUIRE() { unique_lock_.lock(); }
yb::UniqueLock<yb::percpu_rwlock>::lock()
Line
Count
Source
42
165
  void lock() ACQUIRE() { unique_lock_.lock(); }
43
44
29.8M
  std::unique_lock<Mutex>& internal_unique_lock() { return unique_lock_; }
45
46
103
  Mutex* mutex() RETURN_CAPABILITY(unique_lock_.mutex()) { return unique_lock_.mutex(); }
47
48
 private:
49
  std::unique_lock<Mutex> unique_lock_;
50
};
51
52
template<typename Mutex>
53
void WaitOnConditionVariable(std::condition_variable* cond_var, UniqueLock<Mutex>* lock)
54
14.5k
    REQUIRES(*lock) {
55
14.5k
  cond_var->wait(lock->internal_unique_lock());
56
14.5k
}
57
58
template<typename Mutex, typename Functor>
59
void WaitOnConditionVariable(
60
103
    std::condition_variable* cond_var, UniqueLock<Mutex>* lock, Functor f) {
61
103
  cond_var->wait(lock->internal_unique_lock(), f);
62
103
}
63
64
template <class Mutex>
65
29.8M
std::unique_lock<Mutex>& GetLockForCondition(UniqueLock<Mutex>* lock) {
66
29.8M
  return lock->internal_unique_lock();
67
29.8M
}
68
69
#else
70
71
// ------------------------------------------------------------------------------------------------
72
// Thread annotations disabled, no wrapper class needed.
73
// ------------------------------------------------------------------------------------------------
74
75
template<class Mutex>
76
using UniqueLock = std::unique_lock<Mutex>;
77
78
#define UNIQUE_LOCK(lock_name, mutex) std::unique_lock<decltype(mutex)> lock_name(mutex);
79
80
template<typename Mutex>
81
void WaitOnConditionVariable(std::condition_variable* cond_var, UniqueLock<Mutex>* lock) {
82
  cond_var->wait(*lock);
83
}
84
85
template<typename Mutex, typename Functor>
86
void WaitOnConditionVariable(
87
    std::condition_variable* cond_var, UniqueLock<Mutex>* lock, Functor f) {
88
  cond_var->wait(*lock, f);
89
}
90
91
template <class Mutex>
92
std::unique_lock<Mutex>& GetLockForCondition(UniqueLock<Mutex>* lock) {
93
  return *lock;
94
}
95
96
#endif
97
98
} // namespace yb
99
100
#endif  // YB_UTIL_UNIQUE_LOCK_H