summaryrefslogtreecommitdiff
path: root/src/mbgl/util/thread_pool.hpp
blob: 8ec51e3118b6e78facfecae14c4c008f437de828 (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
#pragma once

#include <mbgl/actor/scheduler.hpp>
#include <mbgl/util/optional.hpp>

#include <memory>
#include <thread>
#include <vector>

namespace mbgl {

// `SingleThreadScheduler` implements `Scheduler` interface using
// a lightweight event loop on a single thread. Therefore, all scheduled
// tasks are guaranteed to execute consequently.
class SingleThreadScheduler final : public Scheduler {
public:
    explicit SingleThreadScheduler(optional<std::string> name = nullopt);
    ~SingleThreadScheduler() override;
    SingleThreadScheduler(SingleThreadScheduler&&) = default;

    void schedule(std::function<void()>) override;

    class Impl;
    SingleThreadScheduler(std::shared_ptr<Impl>, optional<std::string> name);

private:
    std::shared_ptr<Impl> impl;
    std::thread thread;
};

// `ThreadPoolScheduler` schedules tasks on an arbitrary thread from the contained
// thread pool. Some of the scheduled tasks might be executed in parallel.
class ThreadPoolScheduler final : public Scheduler {
public:
    explicit ThreadPoolScheduler(std::size_t count);
    ~ThreadPoolScheduler() override;

    void schedule(std::function<void()>) override;

private:
    std::shared_ptr<SingleThreadScheduler::Impl> sharedImpl;
    std::vector<SingleThreadScheduler> threads;
};

using ThreadPool = ThreadPoolScheduler;

} // namespace mbgl