summaryrefslogtreecommitdiff
path: root/src/mbgl/util/work_queue.hpp
blob: 6694352918d0c9fc1f9343cca9d4a9a88e0cf648 (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
#ifndef MBGL_UTIL_WORK_QUEUE
#define MBGL_UTIL_WORK_QUEUE

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/work_request.hpp>

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

namespace mbgl {
namespace util {

class RunLoop;

// The WorkQueue will manage a queue of closures
// and it will make sure they get executed on the
// thread that created the WorkQueue. All pending
// works are canceled when the queue gets destructed.
class WorkQueue : private util::noncopyable {
public:
    WorkQueue();
    ~WorkQueue();

    // Push a closure to the queue. It is advised to
    // only push tasks calling functions on the object
    // that owns the queue to avoid use after free errors.
    void push(std::function<void()>&&);

private:
    void pop(const std::function<void()>&);

    std::queue<std::unique_ptr<WorkRequest>> queue;
    std::mutex queueMutex;

    RunLoop* runLoop;
};

} // namespace util
} // namespace mbgl

#endif