Introduce uv::async

This commit is contained in:
John Firebaugh
2014-11-26 17:03:40 -08:00
parent bcc0db3742
commit 703a325518
2 changed files with 39 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ std::string cwd();
class thread;
class rwlock;
class loop;
class async;
class worker;
}

View File

@@ -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() {