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

#include <uv.h>

namespace mbgl {
namespace util {

uv::tls<RunLoop> RunLoop::current;

class RunLoop::Impl {
public:
    Impl() = default;

    uv_loop_t *loop;
    RunLoop::Type type;
};

RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) {
    switch (type) {
    case Type::New:
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
        impl->loop = uv_loop_new();
        if (impl->loop == nullptr) {
#else
        impl->loop = new uv_loop_t;
        if (uv_loop_init(impl->loop) != 0) {
#endif
            throw std::runtime_error("Failed to initialize loop.");
        }
        break;
    case Type::Default:
        impl->loop = uv_default_loop();
        break;
    }

    impl->type = type;

    current.set(this);
    async = std::make_unique<AsyncTask>(std::bind(&RunLoop::process, this));
}

RunLoop::~RunLoop() {
    current.set(nullptr);

    if (impl->type == Type::Default) {
        return;
    }

    // Run the loop again to ensure that async
    // close callbacks have been called. Not needed
    // for the default main loop because it is only
    // closed when the application exits.
    async.reset();
    runOnce();

#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
    uv_loop_delete(impl->loop);
#else
    if (uv_loop_close(impl->loop) == UV_EBUSY) {
        throw std::runtime_error("Failed to close loop.");
    }
    delete impl->loop;
#endif
}

LOOP_HANDLE RunLoop::getLoopHandle() {
    return current.get()->impl->loop;
}

void RunLoop::run() {
    uv_run(impl->loop, UV_RUN_DEFAULT);
}

void RunLoop::runOnce() {
    uv_run(impl->loop, UV_RUN_ONCE);
}

void RunLoop::stop() {
    invoke([&] { async->unref(); });
}

}
}