YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/transaction_db_mutex.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
21
#pragma once
22
#ifndef ROCKSDB_LITE
23
24
#include <memory>
25
26
#include "yb/rocksdb/status.h"
27
28
namespace rocksdb {
29
30
// TransactionDBMutex and TransactionDBCondVar APIs allows applications to
31
// implement custom mutexes and condition variables to be used by a
32
// TransactionDB when locking keys.
33
//
34
// To open a TransactionDB with a custom TransactionDBMutexFactory, set
35
// TransactionDBOptions.custom_mutex_factory.
36
37
class TransactionDBMutex {
38
 public:
39
912
  virtual ~TransactionDBMutex() {}
40
41
  // Attempt to acquire lock.  Return OK on success, or other Status on failure.
42
  // If returned status is OK, TransactionDB will eventually call UnLock().
43
  virtual Status Lock() = 0;
44
45
  // Attempt to acquire lock.  If timeout is non-negative, operation may be
46
  // failed after this many microseconds.
47
  // Returns OK on success,
48
  //         TimedOut if timed out,
49
  //         or other Status on failure.
50
  // If returned status is OK, TransactionDB will eventually call UnLock().
51
  virtual Status TryLockFor(int64_t timeout_time) = 0;
52
53
  // Unlock Mutex that was successfully locked by Lock() or TryLockUntil()
54
  virtual void UnLock() = 0;
55
};
56
57
class TransactionDBCondVar {
58
 public:
59
912
  virtual ~TransactionDBCondVar() {}
60
61
  // Block current thread until condition variable is notified by a call to
62
  // Notify() or NotifyAll().  Wait() will be called with mutex locked.
63
  // Returns OK if notified.
64
  // Returns non-OK if TransactionDB should stop waiting and fail the operation.
65
  // May return OK spuriously even if not notified.
66
  virtual Status Wait(std::shared_ptr<TransactionDBMutex> mutex) = 0;
67
68
  // Block current thread until condition variable is notified by a call to
69
  // Notify() or NotifyAll(), or if the timeout is reached.
70
  // Wait() will be called with mutex locked.
71
  //
72
  // If timeout is non-negative, operation should be failed after this many
73
  // microseconds.
74
  // If implementing a custom version of this class, the implementation may
75
  // choose to ignore the timeout.
76
  //
77
  // Returns OK if notified.
78
  // Returns TimedOut if timeout is reached.
79
  // Returns other status if TransactionDB should otherwis stop waiting and
80
  //  fail the operation.
81
  // May return OK spuriously even if not notified.
82
  virtual Status WaitFor(std::shared_ptr<TransactionDBMutex> mutex,
83
                         int64_t timeout_time) = 0;
84
85
  // If any threads are waiting on *this, unblock at least one of the
86
  // waiting threads.
87
  virtual void Notify() = 0;
88
89
  // Unblocks all threads waiting on *this.
90
  virtual void NotifyAll() = 0;
91
};
92
93
// Factory class that can allocate mutexes and condition variables.
94
class TransactionDBMutexFactory {
95
 public:
96
  // Create a TransactionDBMutex object.
97
  virtual std::shared_ptr<TransactionDBMutex> AllocateMutex() = 0;
98
99
  // Create a TransactionDBCondVar object.
100
  virtual std::shared_ptr<TransactionDBCondVar> AllocateCondVar() = 0;
101
102
47
  virtual ~TransactionDBMutexFactory() {}
103
};
104
105
}  // namespace rocksdb
106
107
#endif  // ROCKSDB_LITE