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).
11 #include "rocksdb/status.h"
13 namespace ROCKSDB_NAMESPACE
{
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.
19 // To open a TransactionDB with a custom TransactionDBMutexFactory, set
20 // TransactionDBOptions.custom_mutex_factory.
22 class TransactionDBMutex
{
24 virtual ~TransactionDBMutex() {}
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;
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;
38 // Unlock Mutex that was successfully locked by Lock() or TryLockUntil()
39 virtual void UnLock() = 0;
42 class TransactionDBCondVar
{
44 virtual ~TransactionDBCondVar() {}
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;
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.
57 // If timeout is non-negative, operation should be failed after this many
59 // If implementing a custom version of this class, the implementation may
60 // choose to ignore the timeout.
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;
70 // If any threads are waiting on *this, unblock at least one of the
72 virtual void Notify() = 0;
74 // Unblocks all threads waiting on *this.
75 virtual void NotifyAll() = 0;
78 // Factory class that can allocate mutexes and condition variables.
79 class TransactionDBMutexFactory
{
81 // Create a TransactionDBMutex object.
82 virtual std::shared_ptr
<TransactionDBMutex
> AllocateMutex() = 0;
84 // Create a TransactionDBCondVar object.
85 virtual std::shared_ptr
<TransactionDBCondVar
> AllocateCondVar() = 0;
87 virtual ~TransactionDBMutexFactory() {}
90 } // namespace ROCKSDB_NAMESPACE
92 #endif // ROCKSDB_LITE