6#include "detail/function_result.hpp"
7#include "detail/pending_task_queue.hpp"
8#include "detail/promise.hpp"
9#include "detail/ranges.hpp"
10#include "detail/worker_pool.hpp"
15#ifndef DISPATCH_QUEUE_DEFAULT_BATCH_SIZE
16 #define DISPATCH_QUEUE_DEFAULT_BATCH_SIZE 64
52 worker_pool = std::make_unique<detail::worker_pool>(task_queue,
thread_count, std::move(worker_init));
71 template<
typename F,
typename... Args,
typename Ret = detail::function_result<F, Args...>>
73 return dispatch_internal(detail::task_type::background, NULL_TAG, std::forward<F>(f), std::forward<Args>(args)...);
84 template<
typename F,
typename... Args,
typename Ret = detail::function_result<F, Args...>>
86 return dispatch_internal(detail::task_type::main, NULL_TAG, std::forward<F>(f), std::forward<Args>(args)...);
99 template<
typename F,
typename... Args,
typename Ret = detail::function_result<F, Args...>>
101 return dispatch_internal(detail::task_type::tagged, tag, std::forward<F>(f), std::forward<Args>(args)...);
110 template<
typename F,
typename It>
112 std::vector<task<void>> tasks;
113 detail::apply_batches([&](
auto&& batch_begin,
auto&& batch_end) {
115 for (
auto it = batch_begin; it != batch_end; ++it) {
119 }, begin, end, batch_size);
129 template<
typename F,
typename R>
131 std::vector<task<void>> tasks;
132 detail::apply_batches([&](
auto&& batch_begin,
auto&& batch_end) {
134 for (
auto it = batch_begin; it != batch_end; ++it) {
138 }, range, batch_size);
145 bool is_threaded()
const;
151 int thread_count()
const;
185 template<
class Rep,
class Period>
186 bool wait_for(
const std::chrono::duration<Rep, Period>& timeout_duration) {
188 return worker_pool->wait_for(timeout_duration);
200 template<
class Clock,
class Duration>
201 bool wait_until(
const std::chrono::time_point<Clock, Duration>& timeout_time) {
203 return worker_pool->wait_until(timeout_time);
217#ifdef __cpp_lib_coroutine
219 struct dispatch_awaiter {
222 bool await_ready() const noexcept {
return false; }
223 void await_suspend(std::coroutine_handle<> cont)
const {
231 void await_resume() {}
234 struct dispatch_main_awaiter {
235 dispatch_queue& dispatch_queue;
237 bool await_ready() const noexcept {
return false; }
238 void await_suspend(std::coroutine_handle<> cont)
const {
246 void await_resume() {}
249 struct dispatch_tagged_awaiter {
250 dispatch_queue& dispatch_queue;
253 bool await_ready() const noexcept {
return false; }
254 void await_suspend(std::coroutine_handle<> cont)
const {
255 dispatch_queue.dispatch_tagged(tag, [cont]{
262 void await_resume() {}
276 return dispatch_awaiter(*
this);
290 return dispatch_main_awaiter(*
this);
304 return dispatch_tagged_awaiter(*
this, tag);
309 std::unique_ptr<detail::worker_pool> worker_pool;
310 detail::pending_task_queue task_queue;
312 template<
typename F,
typename... Args,
typename Ret = detail::function_result<F, Args...>>
313 task<Ret> dispatch_internal(detail::task_type type,
task_tag tag, F&& f, Args&&... args) {
314 auto work = std::bind(std::move(f), std::forward<Args>(args)...);
316 auto future = detail::task_future<Ret>::create_pending();
317 worker_pool->enqueue_task(type, { future->wrap(work) }, tag);
318 return task<Ret>(future);
320 else if (type == detail::task_type::main) {
321 auto future = detail::task_future<Ret>::create_pending();
322 task_queue.push(type, { future->wrap(work) });
326 auto future = detail::task_future<Ret>::create(work);
dispatch_tagged_awaiter dispatch_tagged(task_tag tag)
Definition dispatch_queue.hpp:303
task< Ret > dispatch_main(F &&f, Args &&... args)
Definition dispatch_queue.hpp:85
dispatch_main_awaiter dispatch_main()
Definition dispatch_queue.hpp:289
bool wait_for(const std::chrono::duration< Rep, Period > &timeout_duration)
Definition dispatch_queue.hpp:186
dispatch_queue & operator=(const dispatch_queue &)=delete
dispatch_awaiter dispatch()
Definition dispatch_queue.hpp:275
task< void > parallel_for(F &&f, const It &begin, const It &end, size_t batch_size=DISPATCH_QUEUE_DEFAULT_BATCH_SIZE)
Definition dispatch_queue.hpp:111
dispatch_queue(const dispatch_queue &)=delete
int thread_count() const
Definition dispatch_queue.cpp:22
task< void > parallel_for(F &&f, R &&range, size_t batch_size=DISPATCH_QUEUE_DEFAULT_BATCH_SIZE)
Definition dispatch_queue.hpp:130
task< Ret > dispatch(F &&f, Args &&... args)
Definition dispatch_queue.hpp:72
task< Ret > dispatch_tagged(task_tag tag, F &&f, Args &&... args)
Definition dispatch_queue.hpp:100
bool wait_until(const std::chrono::time_point< Clock, Duration > &timeout_time)
Definition dispatch_queue.hpp:201
dispatch_queue(int thread_count, Fn &&worker_init)
Definition dispatch_queue.hpp:47
#define DISPATCH_QUEUE_DEFAULT_BATCH_SIZE
Definition dispatch_queue.hpp:16
Definition dispatch_queue.hpp:19
int task_tag
Definition task_tag.hpp:12
task< void > when_all(std::initializer_list< task< T > > tasks)
Definition when_all.hpp:66