Introduce uv::async
This commit is contained in:
@@ -15,6 +15,7 @@ std::string cwd();
|
||||
class thread;
|
||||
class rwlock;
|
||||
class loop;
|
||||
class async;
|
||||
class worker;
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +13,11 @@
|
||||
|
||||
namespace uv {
|
||||
|
||||
template <class T>
|
||||
void close(T* handle) {
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(handle), nullptr);
|
||||
}
|
||||
|
||||
class thread : public mbgl::util::noncopyable {
|
||||
public:
|
||||
inline operator uv_thread_t *() { return &t; }
|
||||
@@ -51,6 +56,39 @@ private:
|
||||
uv_loop_t *l = nullptr;
|
||||
};
|
||||
|
||||
class async : public mbgl::util::noncopyable {
|
||||
public:
|
||||
inline async(uv_loop_t* loop, std::function<void ()> fn_)
|
||||
: fn(fn_) {
|
||||
a.data = this;
|
||||
if (uv_async_init(loop, &a, async_cb) != 0) {
|
||||
throw std::runtime_error("failed to initialize async");
|
||||
}
|
||||
}
|
||||
|
||||
inline ~async() {
|
||||
close(&a);
|
||||
}
|
||||
|
||||
inline void send() {
|
||||
if (uv_async_send(&a) != 0) {
|
||||
throw std::runtime_error("failed to async send");
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
uv_async_t a;
|
||||
std::function<void ()> fn;
|
||||
};
|
||||
|
||||
class mutex : public mbgl::util::noncopyable {
|
||||
public:
|
||||
inline mutex() {
|
||||
|
||||
Reference in New Issue
Block a user