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

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

namespace uv {
class async;
class loop;
}

namespace mbgl {
namespace util {

template <typename T> class Thread;

class RunLoop {
    friend Thread<RunLoop>;

protected:
    // These are called by the Thread<> wrapper.
    RunLoop();
    ~RunLoop();

    // Called by the Thread<> wrapper to start the loop. When you implement this
    // method in a child class, you *must* call this function as the last action.
    void start();

protected:
    // Called by the Thread<> wrapper to terminate this loop.
    void stop();

    // Obtain the underlying loop object in case you want to attach additional listeners.
    uv::loop& loop() { return *runloop; };

private:
    // Invokes function in the run loop.
    void process();

public:
    // Schedules a function to be executed as part of this run loop.
    void invoke(std::function<void()>&& fn);

private:
    const std::unique_ptr<uv::loop> runloop;
    const std::unique_ptr<uv::async> runloopAsync;
    std::mutex runloopMutex;
    std::queue<std::function<void()>> runloopQueue;
};

}
}

#endif