summaryrefslogtreecommitdiff
path: root/platform/default/async_task.cpp
blob: 064e8dad064b6da5308c51a19dfb8e9f3cca4db0 (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
#include <mbgl/util/async_task.hpp>

#include <mbgl/util/run_loop.hpp>

#include <atomic>
#include <functional>

#include <uv.h>

#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
#define UV_ASYNC_PARAMS(handle) uv_async_t *handle, int
#else
#define UV_ASYNC_PARAMS(handle) uv_async_t *handle
#endif

namespace mbgl {
namespace util {

class AsyncTask::Impl {
public:
    Impl(std::function<void()>&& fn)
        : async(new uv_async_t),
          task(std::move(fn)) {

        uv_loop_t* loop = reinterpret_cast<uv_loop_t*>(RunLoop::getLoopHandle());
        if (uv_async_init(loop, async, asyncCallback) != 0) {
            throw std::runtime_error("Failed to initialize async.");
        }

        handle()->data = this;
    }

    ~Impl() {
        uv_close(handle(), [](uv_handle_t* h) {
            delete reinterpret_cast<uv_async_t*>(h);
        });
    }

    void maySend() {
        // uv_async_send will do the call coalescing for us.
        if (uv_async_send(async) != 0) {
            throw std::runtime_error("Failed to async send.");
        }
    }

    void unref() {
        uv_unref(handle());
    }

private:
    static void asyncCallback(UV_ASYNC_PARAMS(handle)) {
        reinterpret_cast<Impl*>(handle->data)->task();
    }

    uv_handle_t* handle() {
        return reinterpret_cast<uv_handle_t*>(async);
    }

    uv_async_t* async;

    std::function<void()> task;
};

AsyncTask::AsyncTask(std::function<void()>&& fn)
    : impl(std::make_unique<Impl>(std::move(fn))) {
}

AsyncTask::~AsyncTask() {
}

void AsyncTask::send() {
    impl->maySend();
}

void AsyncTask::unref() {
    impl->unref();
}

}
}