Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
dispatch-pool.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Author: Liam White
4 * Copyright (C) 2024 Authors
5 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
6 */
7
8#ifndef INKSCAPE_DISPLAY_DISPATCH_POOL_H
9#define INKSCAPE_DISPLAY_DISPATCH_POOL_H
10
11#include <condition_variable>
12#include <functional>
13#include <mutex>
14#include <thread>
15#include <vector>
16
17namespace Inkscape {
18
65{
66public:
67 using global_id = int;
68 using local_id = int;
69 using dispatch_func = std::function<void(global_id, local_id)>;
70
71 explicit dispatch_pool(int size);
73
74 void dispatch(int count, dispatch_func function);
75
76 template <typename F>
77 void dispatch_threshold(int count, bool threshold, F &&function)
78 {
79 if (threshold) {
80 dispatch(count, std::forward<F>(function));
81 } else {
82 for (auto i = global_id{}; i < global_id{count}; i++) {
83 function(i, local_id{});
84 }
85 }
86 }
87
88 int size() const
89 {
90 // The calling thread participates in the dispatch
91 return _threads.size() + 1;
92 }
93
94private:
95 void thread_func(local_id id);
96 void execute_batch(std::unique_lock<std::mutex> &lk, local_id id, int thread_count);
97
98private:
102 bool _shutdown{};
103
104 std::mutex _dispatch_lock;
105 std::mutex _lock;
106 std::condition_variable _available_cv;
107 std::condition_variable _completed_cv;
109 std::vector<std::thread> _threads;
110};
111
112} // namespace Inkscape
113
114#endif // INKSCAPE_DISPLAY_DISPATCH_POOL_H
115
116/*
117 Local Variables:
118 mode:c++
119 c-file-style:"stroustrup"
120 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
121 indent-tabs-mode:nil
122 fill-column:99
123 End:
124*/
125// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
General-purpose, parallel thread dispatch mechanism.
std::function< void(global_id, local_id)> dispatch_func
void execute_batch(std::unique_lock< std::mutex > &lk, local_id id, int thread_count)
std::condition_variable _completed_cv
void thread_func(local_id id)
void dispatch(int count, dispatch_func function)
std::condition_variable _available_cv
std::vector< std::thread > _threads
void dispatch_threshold(int count, bool threshold, F &&function)
Helper class to stream background task notifications as a series of messages.