YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/mutexlock.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
24
#pragma once
25
26
#include <atomic>
27
#include <thread>
28
29
#include "yb/rocksdb/port/port.h"
30
31
namespace rocksdb {
32
33
// Helper class that locks a mutex on construction and unlocks the mutex when
34
// the destructor of the MutexLock object is invoked.
35
//
36
// Typical usage:
37
//
38
//   void MyClass::MyMethod() {
39
//     MutexLock l(&mu_);       // mu_ is an instance variable
40
//     ... some complex code, possibly with multiple return paths ...
41
//   }
42
43
class MutexLock {
44
 public:
45
301M
  explicit MutexLock(port::Mutex *mu) : mu_(mu) {
46
301M
    this->mu_->Lock();
47
301M
  }
48
301M
  ~MutexLock() { this->mu_->Unlock(); }
49
50
 private:
51
  port::Mutex *const mu_;
52
  // No copying allowed
53
  MutexLock(const MutexLock&);
54
  void operator=(const MutexLock&);
55
};
56
57
//
58
// Acquire a ReadLock on the specified RWMutex.
59
// The Lock will be automatically released then the
60
// object goes out of scope.
61
//
62
class ReadLock {
63
 public:
64
948
  explicit ReadLock(port::RWMutex *mu) : mu_(mu) {
65
948
    this->mu_->ReadLock();
66
948
  }
67
948
  ~ReadLock() { this->mu_->ReadUnlock(); }
68
69
 private:
70
  port::RWMutex *const mu_;
71
  // No copying allowed
72
  ReadLock(const ReadLock&);
73
  void operator=(const ReadLock&);
74
};
75
76
77
//
78
// Acquire a WriteLock on the specified RWMutex.
79
// The Lock will be automatically released then the
80
// object goes out of scope.
81
//
82
class WriteLock {
83
 public:
84
45.2k
  explicit WriteLock(port::RWMutex *mu) : mu_(mu) {
85
45.2k
    this->mu_->WriteLock();
86
45.2k
  }
87
45.2k
  ~WriteLock() { this->mu_->WriteUnlock(); }
88
89
 private:
90
  port::RWMutex *const mu_;
91
  // No copying allowed
92
  WriteLock(const WriteLock&);
93
  void operator=(const WriteLock&);
94
};
95
96
//
97
// SpinMutex has very low overhead for low-contention cases.  Method names
98
// are chosen so you can use std::unique_lock or std::lock_guard with it.
99
//
100
class SpinMutex {
101
 public:
102
7.17M
  SpinMutex() : locked_(false) {}
103
104
164M
  bool try_lock() {
105
164M
    auto currently_locked = locked_.load(std::memory_order_relaxed);
106
164M
    return !currently_locked &&
107
164M
           locked_.compare_exchange_weak(currently_locked, true,
108
164M
                                         std::memory_order_acquire,
109
164M
                                         std::memory_order_relaxed);
110
164M
  }
111
112
12.9M
  void lock() {
113
12.9M
    for (size_t tries = 0;; ++tries) {
114
12.9M
      if (try_lock()) {
115
        // success
116
12.9M
        break;
117
12.9M
      }
118
4.83k
      port::AsmVolatilePause();
119
4.83k
      if (tries > 100) {
120
1
        std::this_thread::yield();
121
1
      }
122
4.83k
    }
123
12.9M
  }
124
125
164M
  void unlock() { locked_.store(false, std::memory_order_release); }
126
127
 private:
128
  std::atomic<bool> locked_;
129
};
130
131
}  // namespace rocksdb