summaryrefslogtreecommitdiff
path: root/src/mbgl/util/uv_detail.hpp
blob: 96d5442462044d0516d7d548425a70a9d7281f90 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef MBGL_UTIL_UV_DETAIL
#define MBGL_UTIL_UV_DETAIL

#include <mbgl/util/uv.hpp>
#include <mbgl/util/noncopyable.hpp>

#include <uv.h>

#include <functional>
#include <cassert>
#include <memory>
#include <string>

#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10

// Add thread local storage to libuv API:
// https://github.com/joyent/libuv/commit/5d2434bf71e47802841bad218d521fa254d1ca2d

typedef pthread_key_t uv_key_t;

UV_EXTERN int uv_key_create(uv_key_t* key);
UV_EXTERN void uv_key_delete(uv_key_t* key);
UV_EXTERN void* uv_key_get(uv_key_t* key);
UV_EXTERN void uv_key_set(uv_key_t* key, void* value);

#endif


namespace uv {

template <class T>
void close(std::unique_ptr<T> ptr) {
    uv_close(reinterpret_cast<uv_handle_t*>(ptr.release()), [](uv_handle_t* handle) {
        delete reinterpret_cast<T*>(handle);
    });
}

class loop : public mbgl::util::noncopyable {
public:
    inline loop() {
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
        l = uv_loop_new();
        if (l == nullptr) {
#else
        l = new uv_loop_t;
        if (uv_loop_init(l) != 0) {
#endif
            throw std::runtime_error("failed to initialize loop");
        }
    }

    inline ~loop() {
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
        uv_loop_delete(l);
#else
        uv_loop_close(l);
        delete l;
#endif
    }

    inline void run() {
        uv_run(l, UV_RUN_DEFAULT);
    }

    inline uv_loop_t* operator*() {
        return l;
    }

    inline uv_loop_t* get() {
        return l;
    }

private:
    uv_loop_t *l = nullptr;
};

class async : public mbgl::util::noncopyable {
public:
    inline async(uv_loop_t* loop, std::function<void ()> fn_)
        : a(new uv_async_t)
        , fn(fn_)
    {
        a->data = this;
        if (uv_async_init(loop, a.get(), async_cb) != 0) {
            throw std::runtime_error("failed to initialize async");
        }
    }

    inline ~async() {
        close(std::move(a));
    }

    inline void send() {
        if (uv_async_send(a.get()) != 0) {
            throw std::runtime_error("failed to async send");
        }
    }

    inline void ref() {
        uv_ref(reinterpret_cast<uv_handle_t*>(a.get()));
    }

    inline void unref() {
        uv_unref(reinterpret_cast<uv_handle_t*>(a.get()));
    }

private:
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
    static void async_cb(uv_async_t* a, int) {
#else
    static void async_cb(uv_async_t* a) {
#endif
        reinterpret_cast<async*>(a->data)->fn();
    }

    std::unique_ptr<uv_async_t> a;
    std::function<void ()> fn;
};

class mutex : public mbgl::util::noncopyable {
public:
    inline mutex() {
        if (uv_mutex_init(&mtx) != 0) {
            throw std::runtime_error("failed to initialize mutex lock");
        }
    }
    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 rwlock : public mbgl::util::noncopyable {
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;
};

template <class T>
class tls : public mbgl::util::noncopyable {
public:
    inline tls() {
        if (uv_key_create(&key) != 0) {
            throw std::runtime_error("failed to initialize thread local storage key");
        }
    }
    inline ~tls() { uv_key_delete(&key); }
    inline T* get() { return reinterpret_cast<T*>(uv_key_get(&key)); }
    inline void set(T* val) { uv_key_set(&key, val); }

private:
    uv_key_t key;
};

}

#endif