]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | /* |
2 | A wrapper around ROCKSDB_NAMESPACE::TransactionDBMutexFactory-provided | |
3 | condition and mutex that provides toku_pthread_*-like interface. The functions | |
4 | are named | |
5 | ||
6 | toku_external_{mutex|cond}_XXX | |
7 | ||
8 | Lock Tree uses this mutex and condition for interruptible (long) lock waits. | |
9 | ||
10 | (It also still uses toku_pthread_XXX calls for mutexes/conditions for | |
11 | shorter waits on internal objects) | |
12 | */ | |
13 | ||
14 | #pragma once | |
15 | ||
16 | #include <pthread.h> | |
17 | #include <stdint.h> | |
18 | #include <time.h> | |
19 | ||
20 | #include "rocksdb/utilities/transaction_db.h" | |
21 | #include "rocksdb/utilities/transaction_db_mutex.h" | |
22 | #include "toku_portability.h" | |
23 | ||
24 | using ROCKSDB_NAMESPACE::TransactionDBCondVar; | |
25 | using ROCKSDB_NAMESPACE::TransactionDBMutex; | |
26 | ||
27 | typedef std::shared_ptr<ROCKSDB_NAMESPACE::TransactionDBMutexFactory> | |
28 | toku_external_mutex_factory_t; | |
29 | ||
30 | typedef std::shared_ptr<TransactionDBMutex> toku_external_mutex_t; | |
31 | typedef std::shared_ptr<TransactionDBCondVar> toku_external_cond_t; | |
32 | ||
33 | static inline void toku_external_cond_init( | |
34 | toku_external_mutex_factory_t mutex_factory, toku_external_cond_t *cond) { | |
35 | *cond = mutex_factory->AllocateCondVar(); | |
36 | } | |
37 | ||
38 | inline void toku_external_cond_destroy(toku_external_cond_t *cond) { | |
39 | cond->reset(); // this will destroy the managed object | |
40 | } | |
41 | ||
42 | inline void toku_external_cond_signal(toku_external_cond_t *cond) { | |
43 | (*cond)->Notify(); | |
44 | } | |
45 | ||
46 | inline void toku_external_cond_broadcast(toku_external_cond_t *cond) { | |
47 | (*cond)->NotifyAll(); | |
48 | } | |
49 | ||
50 | inline int toku_external_cond_timedwait(toku_external_cond_t *cond, | |
51 | toku_external_mutex_t *mutex, | |
52 | int64_t timeout_microsec) { | |
53 | auto res = (*cond)->WaitFor(*mutex, timeout_microsec); | |
54 | if (res.ok()) | |
55 | return 0; | |
56 | else | |
57 | return ETIMEDOUT; | |
58 | } | |
59 | ||
60 | inline void toku_external_mutex_init(toku_external_mutex_factory_t factory, | |
61 | toku_external_mutex_t *mutex) { | |
62 | // Use placement new: the memory has been allocated but constructor wasn't | |
63 | // called | |
64 | new (mutex) toku_external_mutex_t; | |
65 | *mutex = factory->AllocateMutex(); | |
66 | } | |
67 | ||
68 | inline void toku_external_mutex_lock(toku_external_mutex_t *mutex) { | |
69 | (*mutex)->Lock(); | |
70 | } | |
71 | ||
72 | inline int toku_external_mutex_trylock(toku_external_mutex_t *mutex) { | |
73 | (*mutex)->Lock(); | |
74 | return 0; | |
75 | } | |
76 | ||
77 | inline void toku_external_mutex_unlock(toku_external_mutex_t *mutex) { | |
78 | (*mutex)->UnLock(); | |
79 | } | |
80 | ||
81 | inline void toku_external_mutex_destroy(toku_external_mutex_t *mutex) { | |
82 | mutex->reset(); // this will destroy the managed object | |
83 | } |