blob: f36da83e4d896be8cb85dd2a3771a1717b3e60a0 (
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#ifndef MBGL_UTIL_UV
#define MBGL_UTIL_UV
#include <uv.h>
#include <functional>
#include <cassert>
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif
#include <boost/lockfree/queue.hpp>
#ifdef __clang__
#pragma clang diagnostic pop
#endif
namespace uv {
class loop {
public:
inline loop() : l(uv_loop_new()) {}
inline ~loop() { uv_loop_delete(l); }
inline uv_loop_t *operator*() { return l; }
private:
uv_loop_t *l;
};
class mutex {
public:
inline mutex() {
if (uv_mutex_init(&mtx) != 0) {
throw std::runtime_error("failed to initialize mutex");
}
}
inline ~mutex() { uv_mutex_destroy(&mtx); }
inline void lock() { uv_mutex_lock(&mtx); }
inline void unlock() { uv_mutex_unlock(&mtx); }
private:
uv_mutex_t mtx;
};
class lock {
public:
lock(mutex &mtx) : mtx(mtx) { mtx.lock(); }
~lock() { mtx.unlock(); }
private:
mutex &mtx;
};
class rwlock {
public:
inline rwlock() {
if (uv_rwlock_init(&mtx) != 0) {
throw std::runtime_error("failed to initialize read-write lock");
}
}
inline ~rwlock() { uv_rwlock_destroy(&mtx); }
inline void rdlock() { uv_rwlock_rdlock(&mtx); }
inline void wrlock() { uv_rwlock_wrlock(&mtx); }
inline void rdunlock() { uv_rwlock_rdunlock(&mtx); }
inline void wrunlock() { uv_rwlock_wrunlock(&mtx); }
private:
uv_rwlock_t mtx;
};
class readlock {
public:
inline readlock(rwlock &mtx) : mtx(mtx) { mtx.rdlock(); }
inline ~readlock() { mtx.rdunlock(); }
private:
rwlock &mtx;
};
class writelock {
public:
inline writelock(rwlock &mtx) : mtx(mtx) { mtx.wrlock(); }
inline ~writelock() { mtx.wrunlock(); }
private:
rwlock &mtx;
};
class once {
public:
typedef void (*callback)();
void operator()(void (*callback)(void)) {
uv_once(&o, callback);
}
private:
uv_once_t o = UV_ONCE_INIT;
};
template <typename T>
class work {
public:
typedef void (*work_callback)(T &object);
typedef void (*after_work_callback)(T &object);
template<typename... Args>
work(const std::shared_ptr<loop> &loop, work_callback work_cb, after_work_callback after_work_cb, Args&&... args)
: loop(loop),
data(std::forward<Args>(args)...),
work_cb(work_cb),
after_work_cb(after_work_cb) {
req.data = this;
uv_queue_work(**loop, &req, do_work, after_work);
}
private:
static void do_work(uv_work_t *req) {
work<T> *w = static_cast<work<T> *>(req->data);
w->work_cb(w->data);
}
static void after_work(uv_work_t *req, int) {
work<T> *w = static_cast<work<T> *>(req->data);
w->after_work_cb(w->data);
delete w;
}
private:
std::shared_ptr<uv::loop> loop;
uv_work_t req;
T data;
work_callback work_cb;
after_work_callback after_work_cb;
};
}
#endif
|