dispenso
A library for task parallelism
 
Loading...
Searching...
No Matches
task_set.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
14#pragma once
15
16namespace dispenso {
17enum class ParentCascadeCancel { kOff, kOn };
18}
19
20#include <dispenso/detail/task_set_impl.h>
21
22namespace dispenso {
23
24constexpr ssize_t kDefaultStealingMultiplier = 4;
25
37class TaskSet : public TaskSetBase {
38 public:
48 ParentCascadeCancel registerForParentCancel,
49 ssize_t stealingLoadMultiplier = kDefaultStealingMultiplier)
51 token_(makeToken(p.work_)) {}
52
53 TaskSet(ThreadPool& p) : TaskSet(p, ParentCascadeCancel::kOff, kDefaultStealingMultiplier) {}
54 TaskSet(ThreadPool& p, ssize_t stealingLoadMultiplier)
55 : TaskSet(p, ParentCascadeCancel::kOff, stealingLoadMultiplier) {}
56
57 TaskSet(TaskSet&& other) = delete;
58 TaskSet& operator=(TaskSet&& other) = delete;
59
73 template <typename F>
74 void schedule(F&& f) {
75 if (DISPENSO_EXPECT(canceled(), false)) {
76 return;
77 }
78 if (outstandingTaskCount_.load(std::memory_order_relaxed) > taskSetLoadFactor_) {
79 f();
80 } else {
81 pool_.schedule(token_, packageTask(std::forward<F>(f)));
82 }
83 }
84
96 template <typename F>
98 pool_.schedule(token_, packageTask(std::forward<F>(f)), fq);
99 }
100
107 DISPENSO_DLL_ACCESS bool wait();
108
123 DISPENSO_DLL_ACCESS bool tryWait(size_t maxToExecute);
124
130 void cancel() {
131 TaskSetBase::cancel();
132 }
133
139 bool canceled() const {
140 return TaskSetBase::canceled();
141 }
142
148 wait();
149 }
150
151 private:
152 DISPENSO_DLL_ACCESS moodycamel::ProducerToken makeToken(
153 moodycamel::ConcurrentQueue<OnceFunction>& pool);
154
155 moodycamel::ProducerToken token_;
156
157 template <typename Result>
158 friend class detail::FutureBase;
159};
160
173class ConcurrentTaskSet : public TaskSetBase {
174 public:
184 ParentCascadeCancel registerForParentCancel,
185 ssize_t stealingLoadMultiplier = kDefaultStealingMultiplier)
187
189 : ConcurrentTaskSet(p, ParentCascadeCancel::kOff, kDefaultStealingMultiplier) {}
190 ConcurrentTaskSet(ThreadPool& p, ssize_t stealingLoadMultiplier)
191 : ConcurrentTaskSet(p, ParentCascadeCancel::kOff, stealingLoadMultiplier) {}
192
193 ConcurrentTaskSet(ConcurrentTaskSet&& other) = delete;
194 ConcurrentTaskSet& operator=(ConcurrentTaskSet&& other) = delete;
195
212 template <typename F>
213 void schedule(F&& f, bool skipRecheck = false) {
214 if (outstandingTaskCount_.load(std::memory_order_relaxed) > taskSetLoadFactor_ &&
215 DISPENSO_EXPECT(!canceled(), true)) {
216 f();
217 } else if (skipRecheck) {
218 pool_.schedule(packageTask(std::forward<F>(f)), ForceQueuingTag());
219 } else {
220 pool_.schedule(packageTask(std::forward<F>(f)));
221 }
222 }
223
235 template <typename F>
237 pool_.schedule(packageTask(std::forward<F>(f)), fq);
238 }
239
244 DISPENSO_DLL_ACCESS bool wait();
245
259 DISPENSO_DLL_ACCESS bool tryWait(size_t maxToExecute);
260
267 void cancel() {
268 TaskSetBase::cancel();
269 }
270
276 bool canceled() const {
277 return TaskSetBase::canceled();
278 }
279
285 wait();
286 }
287
288 private:
289 bool tryExecuteNext() {
290 return pool_.tryExecuteNext();
291 }
292
293 template <typename Result>
294 friend class detail::FutureBase;
295
296 friend class detail::LimitGatedScheduler;
297};
298
304DISPENSO_DLL_ACCESS TaskSetBase* parentTaskSet();
305
306} // namespace dispenso
void schedule(F &&f, bool skipRecheck=false)
Definition task_set.h:213
void schedule(F &&f, ForceQueuingTag fq)
Definition task_set.h:236
DISPENSO_DLL_ACCESS bool tryWait(size_t maxToExecute)
Definition task_set.cpp:93
ConcurrentTaskSet(ThreadPool &pool, ParentCascadeCancel registerForParentCancel, ssize_t stealingLoadMultiplier=kDefaultStealingMultiplier)
Definition task_set.h:182
DISPENSO_DLL_ACCESS bool wait()
Definition task_set.cpp:76
DISPENSO_DLL_ACCESS bool tryWait(size_t maxToExecute)
Definition task_set.cpp:131
void schedule(F &&f)
Definition task_set.h:74
void schedule(F &&f, ForceQueuingTag fq)
Definition task_set.h:97
bool canceled() const
Definition task_set.h:139
TaskSet(ThreadPool &p, ParentCascadeCancel registerForParentCancel, ssize_t stealingLoadMultiplier=kDefaultStealingMultiplier)
Definition task_set.h:46
DISPENSO_DLL_ACCESS bool wait()
Definition task_set.cpp:114
detail::OpResult< T > OpResult
Definition pipeline.h:29