summaryrefslogtreecommitdiff
path: root/src/mbgl/util/run_loop.hpp
blob: ed2b364cef43c1a45d7295deced833b8a0eb1434 (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
#ifndef MBGL_UTIL_RUN_LOOP
#define MBGL_UTIL_RUN_LOOP

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/uv_detail.hpp>

#include <functional>
#include <queue>
#include <mutex>

namespace mbgl {
namespace util {

class RunLoop : private util::noncopyable {
public:
    RunLoop(uv_loop_t*);
    ~RunLoop();

    void stop();

    // Invoke fn() in the runloop thread.
    template <class Fn>
    void invoke(Fn&& fn) {
        auto invokable = util::make_unique<Invoker<Fn>>(std::move(fn));
        withMutex([&] { queue.push(std::move(invokable)); });
        async.send();
    }

    // Invoke fn() in the runloop thread, then invoke callback(result) in the current thread.
    template <class Fn, class R>
    void invokeWithResult(Fn&& fn, std::function<void (R)>&& callback) {
        RunLoop* outer = current.get();
        assert(outer);

        invoke([fn = std::move(fn), callback = std::move(callback), outer] () mutable {
            outer->invoke([callback = std::move(callback), result = std::move(fn())] () mutable {
                callback(std::move(result));
            });
        });
    }

    // Invoke fn() in the runloop thread, then invoke callback() in the current thread.
    template <class Fn>
    void invokeWithResult(Fn&& fn, std::function<void ()>&& callback) {
        RunLoop* outer = current.get();
        assert(outer);

        invoke([fn = std::move(fn), callback = std::move(callback), outer] () mutable {
            fn();
            outer->invoke(std::move(callback));
        });
    }

    uv_loop_t* get() { return async.get()->loop; }

    static uv::tls<RunLoop> current;

private:
    // A movable type-erasing invokable entity wrapper. This allows to store arbitrary invokable
    // things (like std::function<>, or the result of a movable-only std::bind()) in the queue.
    // Source: http://stackoverflow.com/a/29642072/331379
    struct Message {
        virtual void operator()() = 0;
        virtual ~Message() = default;
    };

    template <class F>
    struct Invoker : Message {
        Invoker(F&& f) : func(std::move(f)) {}
        void operator()() override { func(); }
        F func;
    };

    using Queue = std::queue<std::unique_ptr<Message>>;

    void withMutex(std::function<void()>&&);
    void process();

    Queue queue;
    std::mutex mutex;
    uv::async async;
};

}
}

#endif