summaryrefslogtreecommitdiffstats
path: root/src/rocksdb/utilities/transactions/lock/range/range_tree/lib/portability/toku_external_pthread.h
blob: eb8291c1d297d071921ec5e8d828d2c4adbb8d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
  A wrapper around ROCKSDB_NAMESPACE::TransactionDBMutexFactory-provided
  condition and mutex that provides toku_pthread_*-like interface. The functions
  are named

    toku_external_{mutex|cond}_XXX

  Lock Tree uses this mutex and condition for interruptible (long) lock waits.

  (It also still uses toku_pthread_XXX calls for mutexes/conditions for
   shorter waits on internal objects)
*/

#pragma once

#include <pthread.h>
#include <stdint.h>
#include <time.h>

#include "rocksdb/utilities/transaction_db.h"
#include "rocksdb/utilities/transaction_db_mutex.h"
#include "toku_portability.h"

using ROCKSDB_NAMESPACE::TransactionDBCondVar;
using ROCKSDB_NAMESPACE::TransactionDBMutex;

typedef std::shared_ptr<ROCKSDB_NAMESPACE::TransactionDBMutexFactory>
    toku_external_mutex_factory_t;

typedef std::shared_ptr<TransactionDBMutex> toku_external_mutex_t;
typedef std::shared_ptr<TransactionDBCondVar> toku_external_cond_t;

static inline void toku_external_cond_init(
    toku_external_mutex_factory_t mutex_factory, toku_external_cond_t *cond) {
  *cond = mutex_factory->AllocateCondVar();
}

inline void toku_external_cond_destroy(toku_external_cond_t *cond) {
  cond->reset();  // this will destroy the managed object
}

inline void toku_external_cond_signal(toku_external_cond_t *cond) {
  (*cond)->Notify();
}

inline void toku_external_cond_broadcast(toku_external_cond_t *cond) {
  (*cond)->NotifyAll();
}

inline int toku_external_cond_timedwait(toku_external_cond_t *cond,
                                        toku_external_mutex_t *mutex,
                                        int64_t timeout_microsec) {
  auto res = (*cond)->WaitFor(*mutex, timeout_microsec);
  if (res.ok())
    return 0;
  else
    return ETIMEDOUT;
}

inline void toku_external_mutex_init(toku_external_mutex_factory_t factory,
                                     toku_external_mutex_t *mutex) {
  // Use placement new: the memory has been allocated but constructor wasn't
  // called
  new (mutex) toku_external_mutex_t;
  *mutex = factory->AllocateMutex();
}

inline void toku_external_mutex_lock(toku_external_mutex_t *mutex) {
  (*mutex)->Lock();
}

inline int toku_external_mutex_trylock(toku_external_mutex_t *mutex) {
  (*mutex)->Lock();
  return 0;
}

inline void toku_external_mutex_unlock(toku_external_mutex_t *mutex) {
  (*mutex)->UnLock();
}

inline void toku_external_mutex_destroy(toku_external_mutex_t *mutex) {
  mutex->reset();  // this will destroy the managed object
}