blob: 9198b0fd1cf7353de320599f93116db7d636df33 (
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
|
#ifndef MBGL_UTIL_UV
#define MBGL_UTIL_UV
#include <string>
#include <memory>
typedef struct uv_handle_s uv_handle_t;
typedef struct uv_async_s uv_async_t;
typedef struct uv_timer_s uv_timer_t;
typedef struct uv_loop_s uv_loop_t;
typedef struct uv_fs_s uv_fs_t;
namespace uv {
class rwlock;
class loop;
class async;
class mutex;
class cond;
class lock {
public:
lock(mutex &);
lock(const std::unique_ptr<mutex> &);
lock(const lock &) = delete;
lock(lock &&lock);
lock &operator=(const lock &lock) = delete;
lock &operator=(lock &&lock);
~lock();
private:
mutex *mtx = nullptr;
};
class readlock {
public:
readlock(rwlock &);
readlock(const std::unique_ptr<rwlock> &);
readlock(const readlock &) = delete;
readlock(readlock &&lock);
readlock &operator=(const readlock &lock) = delete;
readlock &operator=(readlock &&lock);
~readlock();
private:
rwlock *mtx = nullptr;
};
class writelock {
public:
writelock(rwlock &);
writelock(const std::unique_ptr<rwlock> &);
writelock(const writelock &) = delete;
writelock(writelock &&lock);
writelock &operator=(const writelock &lock) = delete;
writelock &operator=(writelock &&lock);
~writelock();
private:
rwlock *mtx = nullptr;
};
template <class T>
class exclusive {
public:
exclusive(T& val, mutex &mtx) : ptr(&val), lock(mtx) {}
exclusive(T *val, mutex &mtx) : ptr(val), lock(mtx) {}
exclusive(mutex &mtx) : lock(mtx) {}
exclusive(const std::unique_ptr<mutex> &mtx) : lock(mtx) {}
exclusive(const exclusive &) = delete;
exclusive(exclusive &&) = default;
exclusive &operator=(const exclusive &) = delete;
exclusive &operator=(exclusive &&) = default;
T *operator->() { return ptr; }
const T *operator->() const { return ptr; }
T *operator*() { return ptr; }
const T *operator*() const { return ptr; }
operator T&() { return *ptr; }
operator const T&() const { return *ptr; }
void operator<<(T& val) { operator<<(&val); }
void operator<<(T *val) {
if (ptr) {
throw std::runtime_error("exclusive<> was assigned before");
}
ptr = val;
}
private:
T *ptr = nullptr;
class lock lock;
};
const char *getFileRequestError(uv_fs_t *req);
template <typename T>
void close(T *specific) {
uv_close(reinterpret_cast<uv_handle_t *>(specific), [](uv_handle_t *generic) {
delete reinterpret_cast<T *>(generic);
});
}
}
#endif
|