summaryrefslogtreecommitdiff
path: root/src/mbgl/util/worker.hpp
blob: 6630465631a2a4458c320621e67a523bf40ba919 (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
#ifndef MBGL_UTIL_WORKER
#define MBGL_UTIL_WORKER

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/tile/tile_worker.hpp>

#include <functional>
#include <memory>

namespace mbgl {

class AsyncRequest;
class RasterBucket;
class GeometryTileLoader;

using RasterTileParseResult = mapbox::util::variant<
    std::unique_ptr<Bucket>, // success
    std::exception_ptr>;     // error

class Worker : public mbgl::util::noncopyable {
public:
    explicit Worker(std::size_t count);
    ~Worker();

    // Request work be done on a thread pool. Callbacks are executed on the invoking
    // thread, which must have a run loop, after the work is complete.
    //
    // The return value represents the request to perform the work asynchronously.
    // Its destructor guarantees that the work function has finished executing, and
    // that the after function has either finished executing or will not execute.
    // Together, this means that an object may make a work request with lambdas which
    // bind references to itself, and if and when those lambdas execute, the references
    // will still be valid.

    using Request = std::unique_ptr<AsyncRequest>;

    Request parseRasterTile(std::unique_ptr<RasterBucket> bucket,
                            std::shared_ptr<const std::string> data,
                            std::function<void(RasterTileParseResult)> callback);

    Request parseGeometryTile(TileWorker&,
                              std::vector<std::unique_ptr<StyleLayer>>,
                              std::unique_ptr<GeometryTile>,
                              PlacementConfig,
                              std::function<void(TileParseResult)> callback);

    Request parsePendingGeometryTileLayers(TileWorker&,
                                           PlacementConfig config,
                                           std::function<void(TileParseResult)> callback);

    Request redoPlacement(TileWorker&,
                          const std::unordered_map<std::string, std::unique_ptr<Bucket>>&,
                          PlacementConfig config,
                          std::function<void()> callback);

private:
    class Impl;
    std::vector<std::unique_ptr<util::Thread<Impl>>> threads;
    std::size_t current = 0;
};
} // namespace mbgl

#endif