]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/include/rocksdb/utilities/transaction_db_mutex.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / rocksdb / include / rocksdb / utilities / transaction_db_mutex.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5
6 #pragma once
7 #ifndef ROCKSDB_LITE
8
9 #include <memory>
10
11 #include "rocksdb/status.h"
12
13 namespace ROCKSDB_NAMESPACE {
14
15 // TransactionDBMutex and TransactionDBCondVar APIs allows applications to
16 // implement custom mutexes and condition variables to be used by a
17 // TransactionDB when locking keys.
18 //
19 // To open a TransactionDB with a custom TransactionDBMutexFactory, set
20 // TransactionDBOptions.custom_mutex_factory.
21
22 class TransactionDBMutex {
23 public:
24 virtual ~TransactionDBMutex() {}
25
26 // Attempt to acquire lock. Return OK on success, or other Status on failure.
27 // If returned status is OK, TransactionDB will eventually call UnLock().
28 virtual Status Lock() = 0;
29
30 // Attempt to acquire lock. If timeout is non-negative, operation may be
31 // failed after this many microseconds.
32 // Returns OK on success,
33 // TimedOut if timed out,
34 // or other Status on failure.
35 // If returned status is OK, TransactionDB will eventually call UnLock().
36 virtual Status TryLockFor(int64_t timeout_time) = 0;
37
38 // Unlock Mutex that was successfully locked by Lock() or TryLockUntil()
39 virtual void UnLock() = 0;
40 };
41
42 class TransactionDBCondVar {
43 public:
44 virtual ~TransactionDBCondVar() {}
45
46 // Block current thread until condition variable is notified by a call to
47 // Notify() or NotifyAll(). Wait() will be called with mutex locked.
48 // Returns OK if notified.
49 // Returns non-OK if TransactionDB should stop waiting and fail the operation.
50 // May return OK spuriously even if not notified.
51 virtual Status Wait(std::shared_ptr<TransactionDBMutex> mutex) = 0;
52
53 // Block current thread until condition variable is notified by a call to
54 // Notify() or NotifyAll(), or if the timeout is reached.
55 // Wait() will be called with mutex locked.
56 //
57 // If timeout is non-negative, operation should be failed after this many
58 // microseconds.
59 // If implementing a custom version of this class, the implementation may
60 // choose to ignore the timeout.
61 //
62 // Returns OK if notified.
63 // Returns TimedOut if timeout is reached.
64 // Returns other status if TransactionDB should otherwis stop waiting and
65 // fail the operation.
66 // May return OK spuriously even if not notified.
67 virtual Status WaitFor(std::shared_ptr<TransactionDBMutex> mutex,
68 int64_t timeout_time) = 0;
69
70 // If any threads are waiting on *this, unblock at least one of the
71 // waiting threads.
72 virtual void Notify() = 0;
73
74 // Unblocks all threads waiting on *this.
75 virtual void NotifyAll() = 0;
76 };
77
78 // Factory class that can allocate mutexes and condition variables.
79 class TransactionDBMutexFactory {
80 public:
81 // Create a TransactionDBMutex object.
82 virtual std::shared_ptr<TransactionDBMutex> AllocateMutex() = 0;
83
84 // Create a TransactionDBCondVar object.
85 virtual std::shared_ptr<TransactionDBCondVar> AllocateCondVar() = 0;
86
87 virtual ~TransactionDBMutexFactory() {}
88 };
89
90 } // namespace ROCKSDB_NAMESPACE
91
92 #endif // ROCKSDB_LITE