execution 1.0.2
Portable task execution, timers, and process lifecycle
Loading...
Searching...
No Matches
task.hpp
Go to the documentation of this file.
1/*
2 Copyright 2015 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6
7/**************************************************************************************************/
8
9#ifndef STLAB_CONCURRENCY_TASK_HPP
10#define STLAB_CONCURRENCY_TASK_HPP
11
33
34/**************************************************************************************************/
35
36#include <algorithm>
37#include <array>
38#include <cstddef>
39#include <exception>
40#include <functional>
41#include <memory>
42#include <type_traits>
43#include <utility>
44
45#include <stlab/execution/config.hpp>
46
47/**************************************************************************************************/
48
49namespace stlab {
50STLAB_EXECUTION_VERSION_NAMESPACE_BEGIN()
51
52
60
61/**************************************************************************************************/
62
63
65 using dtor_t = void (*)(void*) noexcept;
66 using move_ctor_t = void (*)(void*, void*) noexcept;
67 using target_type_t = const std::type_info& (*)() noexcept;
68 using pointer_t = void* (*)(void*) noexcept;
69 using const_pointer_t = const void* (*)(const void*) noexcept;
70
71 dtor_t dtor;
72 move_ctor_t move_ctor;
73 target_type_t target_type;
74 pointer_t pointer;
75 const_pointer_t const_pointer;
76};
77
79inline constexpr std::size_t stlab_v2_task_storage_size =
80 std::max(alignof(std::max_align_t) * 2, sizeof(void*) * 8) -
81 std::max(alignof(std::max_align_t), sizeof(void*) * 2);
82
84inline constexpr std::size_t stlab_v2_task_storage_alignment = alignof(std::max_align_t);
85
86namespace execution_detail {
87
89template <std::size_t Size, std::size_t Alignment>
90struct task_storage_abi_guard {
91 STLAB_CORE_API static const unsigned char value;
92};
93
95using current_task_storage_abi_guard =
96 task_storage_abi_guard<stlab_v2_task_storage_size, stlab_v2_task_storage_alignment>;
97
99template <>
100struct task_storage_abi_guard<stlab_v2_task_storage_size, stlab_v2_task_storage_alignment> {
101 STLAB_CORE_API static const unsigned char value;
102};
103
105constexpr auto stlab_v2_align_offset(std::size_t offset, std::size_t alignment) noexcept
106 -> std::size_t {
107 return (offset + alignment - 1) / alignment * alignment;
108}
109
110inline constexpr auto stlab_v2_task_concept_move_ctor_offset = stlab_v2_align_offset(
111 sizeof(stlab_v2_task_concept::dtor_t), alignof(stlab_v2_task_concept::move_ctor_t));
112inline constexpr auto stlab_v2_task_concept_target_type_offset = stlab_v2_align_offset(
113 stlab_v2_task_concept_move_ctor_offset + sizeof(stlab_v2_task_concept::move_ctor_t),
114 alignof(stlab_v2_task_concept::target_type_t));
115inline constexpr auto stlab_v2_task_concept_pointer_offset = stlab_v2_align_offset(
116 stlab_v2_task_concept_target_type_offset + sizeof(stlab_v2_task_concept::target_type_t),
117 alignof(stlab_v2_task_concept::pointer_t));
118inline constexpr auto stlab_v2_task_concept_const_pointer_offset = stlab_v2_align_offset(
119 stlab_v2_task_concept_pointer_offset + sizeof(stlab_v2_task_concept::pointer_t),
120 alignof(stlab_v2_task_concept::const_pointer_t));
121inline constexpr auto stlab_v2_task_concept_alignment = std::max(
122 {alignof(stlab_v2_task_concept::dtor_t), alignof(stlab_v2_task_concept::move_ctor_t),
123 alignof(stlab_v2_task_concept::target_type_t), alignof(stlab_v2_task_concept::pointer_t),
124 alignof(stlab_v2_task_concept::const_pointer_t)});
125
126} // namespace execution_detail
127
128// An incompatible operation-table change requires a new versioned type instead of relaxing the v2
129// canaries below to accept a different layout.
130static_assert(std::is_standard_layout_v<stlab_v2_task_concept>);
131static_assert(alignof(stlab_v2_task_concept) == execution_detail::stlab_v2_task_concept_alignment);
132static_assert(std::is_same_v<decltype(stlab_v2_task_concept::dtor), stlab_v2_task_concept::dtor_t>);
133static_assert(
134 std::is_same_v<decltype(stlab_v2_task_concept::move_ctor), stlab_v2_task_concept::move_ctor_t>);
135static_assert(std::is_same_v<decltype(stlab_v2_task_concept::target_type),
136 stlab_v2_task_concept::target_type_t>);
137static_assert(
138 std::is_same_v<decltype(stlab_v2_task_concept::pointer), stlab_v2_task_concept::pointer_t>);
139static_assert(std::is_same_v<decltype(stlab_v2_task_concept::const_pointer),
140 stlab_v2_task_concept::const_pointer_t>);
141static_assert(offsetof(stlab_v2_task_concept, dtor) == 0);
142static_assert(offsetof(stlab_v2_task_concept, move_ctor) ==
143 execution_detail::stlab_v2_task_concept_move_ctor_offset);
144static_assert(offsetof(stlab_v2_task_concept, target_type) ==
145 execution_detail::stlab_v2_task_concept_target_type_offset);
146static_assert(offsetof(stlab_v2_task_concept, pointer) ==
147 execution_detail::stlab_v2_task_concept_pointer_offset);
148static_assert(offsetof(stlab_v2_task_concept, const_pointer) ==
149 execution_detail::stlab_v2_task_concept_const_pointer_offset);
150static_assert(sizeof(stlab_v2_task_concept) ==
151 execution_detail::stlab_v2_align_offset(
152 execution_detail::stlab_v2_task_concept_const_pointer_offset +
153 sizeof(stlab_v2_task_concept::const_pointer_t),
154 alignof(stlab_v2_task_concept)));
155
156/**************************************************************************************************/
157
163template <bool NoExcept, class R, class... Args>
164class task_ {
165public:
170
172 using invoke_t =
173 std::conditional_t<NoExcept, R (*)(void*, Args...) noexcept, R (*)(void*, Args...)>;
174
175private:
176 template <class F>
177 constexpr static bool maybe_empty =
178 std::is_pointer_v<std::decay_t<F>> || std::is_member_pointer_v<std::decay_t<F>> ||
179 std::is_same_v<std::function<R(Args...)>, std::decay_t<F>>;
180
181 template <class F>
182 constexpr static auto is_empty(const F& f) -> std::enable_if_t<maybe_empty<F>, bool> {
183 return !f;
184 }
185
186 template <class F>
187 constexpr static auto is_empty(const F&) -> std::enable_if_t<!maybe_empty<F>, bool> {
188 return false;
189 }
190
191 template <class F, bool Small>
192 struct model;
193
194 template <class F>
195 struct model<F, true> {
196 template <class G> // for forwarding
197 model(G&& f) : _f(std::forward<G>(f)) {}
198 model(model&&) noexcept = delete;
199
200 static void dtor(void* self) noexcept { static_cast<model*>(self)->~model(); }
201 static void move_ctor(void* self, void* p) noexcept {
202 new (p) model(std::move(static_cast<model*>(self)->_f));
203 }
204
205 /*
206 NOTE (sean-parent): `Args` are _not_ universal references. This is a `concrete`
207 interface for the model. Do not add `&&`, that would make it an rvalue reference.
208 The `forward<Args>` here is correct. We are forwarding from the client defined
209 signature to the actual captured model.
210 */
211
212 // NOLINTNEXTLINE(performance-unnecessary-value-param)
213 static auto invoke(void* self, Args... args) noexcept(NoExcept) -> R {
214 return std::invoke(static_cast<model*>(self)->_f, std::forward<Args>(args)...);
215 }
216
217 static auto target_type() noexcept -> const std::type_info& { return typeid(F); }
218 static auto pointer(void* self) noexcept -> void* { return &static_cast<model*>(self)->_f; }
219 static auto const_pointer(const void* self) noexcept -> const void* {
220 return &static_cast<const model*>(self)->_f;
221 }
222#if defined(__GNUC__) && __GNUC__ < 7 && !defined(__clang__)
223 static const concept_t _vtable;
224 static const invoke_t _invoke;
225#else
226 static constexpr concept_t _vtable = {dtor, move_ctor, target_type, pointer, const_pointer};
227 static constexpr invoke_t _invoke = invoke;
228#endif
229 F _f;
230 };
231
232 template <class F>
233 struct model<F, false> {
234 template <class G> // for forwarding
235 model(G&& f) : _p(std::make_unique<F>(std::forward<G>(f))) {}
236 model(model&&) noexcept = default;
237
238 static void dtor(void* self) noexcept { static_cast<model*>(self)->~model(); }
239 static void move_ctor(void* self, void* p) noexcept {
240 new (p) model(std::move(*static_cast<model*>(self)));
241 }
242
243 /*
244 NOTE (sean-parent): `Args` are _not_ universal references. This is a `concrete`
245 interface for the model. Do not add `&&`, that would make it an rvalue reference.
246 The `forward<Args>` here is correct. We are forwarding from the client defined
247 signature to the actual captured model.
248 */
249
250 static auto invoke(void* self, Args... args) noexcept(NoExcept) -> R {
251 return std::invoke(*static_cast<model*>(self)->_p, std::forward<Args>(args)...);
252 }
253
254 static auto target_type() noexcept -> const std::type_info& { return typeid(F); }
255 static auto pointer(void* self) noexcept -> void* {
256 return static_cast<model*>(self)->_p.get();
257 }
258 static auto const_pointer(const void* self) noexcept -> const void* {
259 return static_cast<const model*>(self)->_p.get();
260 }
261
262#if defined(__GNUC__) && __GNUC__ < 7 && !defined(__clang__)
263 static const concept_t _vtable;
264 static const invoke_t _invoke;
265#else
266 static constexpr concept_t _vtable = {dtor, move_ctor, target_type, pointer, const_pointer};
267 static constexpr invoke_t _invoke = invoke;
268#endif
269
270 std::unique_ptr<F> _p;
271 };
272
273 // empty (default) vtable
274 static void dtor(void*) noexcept {}
275 static void move_ctor(void*, void*) noexcept {}
276 // NOLINTNEXTLINE(performance-unnecessary-value-param)
277 static auto invoke(void*, Args...) noexcept(NoExcept) -> R {
278 if constexpr (NoExcept) {
279 try {
280 throw std::bad_function_call();
281 } catch (...) {
282 std::terminate();
283 }
284 } else {
285 throw std::bad_function_call();
286 }
287 }
288 static auto target_type_() noexcept -> const std::type_info& { return typeid(void); }
289 static auto pointer(void*) noexcept -> void* { return nullptr; }
290 static auto const_pointer(const void*) noexcept -> const void* { return nullptr; }
291
292#if defined(__GNUC__) && __GNUC__ < 7 && !defined(__clang__)
293 static const concept_t _vtable;
294#else
295 static constexpr concept_t _vtable = {dtor, move_ctor, target_type_, pointer, const_pointer};
296#endif
297
298 /*
299 The layout of this object is going to be:
300 _vtable_ptr
301 _invoke
302 _model
303
304 Because the model is going to be max-aligned, it will leave a gap between the vtable_ptr and
305 the _model, we fill that gap by lifting the _invoke pointer into it from the vtable. This means
306 invoke calls require one less indirection.
307
308 The size of the model is big enough for 6 pointers or (2 max aligned object - 2 pointers)
309 (which would typically be 2 pointers). The rational here is that we want the total object size
310 to be a power of 2, for the object but the model store to be large enough to hold 2
311 weak-pointers (which is 4 pointers total), so we give things a little extra room.
312 */
313
314 const concept_t* _vtable_ptr = &_vtable;
315 invoke_t _invoke = invoke;
317 std::array<unsigned char, stlab_v2_task_storage_size> _model;
318
319public:
320 using result_type = R;
321
324 [[nodiscard]] auto relocation_concept() const noexcept -> const concept_t* {
325 return _vtable_ptr;
326 }
327
329 [[nodiscard]] auto relocation_invoke() const noexcept -> invoke_t { return _invoke; }
330
334 [[nodiscard]] auto relocation_source() noexcept -> void* { return &_model; }
335
343 task_(const concept_t* vtable_ptr, invoke_t invoke_fn, void* source) noexcept :
344 _vtable_ptr(vtable_ptr), _invoke(invoke_fn) {
345 vtable_ptr->move_ctor(source, &_model);
346 }
347
348 constexpr task_() noexcept = default;
349 constexpr task_(std::nullptr_t) noexcept : task_() {}
350 task_(const task_&) = delete;
351 task_(const task_&&) = delete;
352 task_(task_&& x) noexcept : _vtable_ptr(x._vtable_ptr), _invoke(x._invoke) {
353 _vtable_ptr->move_ctor(&x._model, &_model);
354 }
355
356 template <class F,
357 std::enable_if_t<!NoExcept || std::is_nothrow_invocable_v<F, Args...>, bool> = true>
358 task_(F&& f) {
359 using small_t = model<std::decay_t<F>, true>;
360 using large_t = model<std::decay_t<F>, false>;
361 using model_t =
362 std::conditional_t<(sizeof(small_t) <= stlab_v2_task_storage_size) &&
363 (alignof(small_t) <= stlab_v2_task_storage_alignment) &&
364 std::is_nothrow_move_constructible_v<std::decay_t<F>>,
365 small_t, large_t>;
366
367 if (is_empty(f)) return;
368
369 new (&_model) model_t(std::forward<F>(f));
370 _vtable_ptr = &model_t::_vtable;
371 _invoke = &model_t::invoke;
372 }
373
374 ~task_() { _vtable_ptr->dtor(&_model); };
375
376 auto operator=(const task_&) -> task_& = delete;
377
378 auto operator=(task_&& x) noexcept -> task_& {
379 if (this == &x) return *this;
380 _vtable_ptr->dtor(&_model);
381 _vtable_ptr = x._vtable_ptr;
382 _invoke = x._invoke;
383 _vtable_ptr->move_ctor(&x._model, &_model);
384 return *this;
385 }
386
387 auto operator=(std::nullptr_t) noexcept -> task_& { return *this = task_(); }
388
389 template <class F>
390 auto operator=(F&& f)
391 -> std::enable_if_t<!NoExcept || std::is_nothrow_invocable_v<decltype(f), Args...>,
392 task_&> {
393 return *this = task_(std::forward<F>(f));
394 }
395
396 void swap(task_& x) noexcept { std::swap(*this, x); }
397
398 explicit operator bool() const { return _vtable_ptr->const_pointer(&_model) != nullptr; }
399
400 [[nodiscard]] auto target_type() const noexcept -> const std::type_info& {
401 return _vtable_ptr->target_type();
402 }
403
404 template <class T>
405 auto target() -> T* {
406 return (target_type() == typeid(T)) ? static_cast<T*>(_vtable_ptr->pointer(&_model)) :
407 nullptr;
408 }
409
410 template <class T>
411 [[nodiscard]] auto target() const -> const T* {
412 return (target_type() == typeid(T)) ?
413 static_cast<const T*>(_vtable_ptr->const_pointer(&_model)) :
414 nullptr;
415 }
416
417 template <class... Brgs>
418 auto operator()(Brgs&&... brgs) noexcept(NoExcept) -> R {
419 return _invoke(&_model, std::forward<Brgs>(brgs)...);
420 }
421
422 friend inline void swap(task_& x, task_& y) noexcept { return x.swap(y); }
423 friend inline auto operator==(const task_& x, std::nullptr_t) -> bool {
424 return !static_cast<bool>(x);
425 }
426 friend inline auto operator==(std::nullptr_t, const task_& x) -> bool {
427 return !static_cast<bool>(x);
428 }
429 friend inline auto operator!=(const task_& x, std::nullptr_t) -> bool {
430 return static_cast<bool>(x);
431 }
432 friend inline auto operator!=(std::nullptr_t, const task_& x) -> bool {
433 return static_cast<bool>(x);
434 }
435};
436
437/**************************************************************************************************/
438
439template <template <bool, class, class...> class T, class F>
441
442template <template <bool, class, class...> class T, class R, class... Args>
443struct noexcept_deducer<T, R(Args...)> {
444 using type = T<false, R, Args...>;
445};
446
447template <template <bool, class, class...> class T, class R, class... Args>
448struct noexcept_deducer<T, R(Args...) noexcept> {
449 using type = T<true, R, Args...>;
450};
451
453template <class F>
455
456/**************************************************************************************************/
457
459
460STLAB_EXECUTION_VERSION_NAMESPACE_END()
461} // namespace stlab
462
463/**************************************************************************************************/
464
465#endif
466
467/**************************************************************************************************/
Type-erased, move-only callable with signature R(Args...) (or noexcept variant).
Definition task.hpp:164
std::conditional_t< NoExcept, R(*)(void *, Args...) noexcept, R(*)(void *, Args...)> invoke_t
Function used to invoke a task's target, valid for as long as the target is live.
Definition task.hpp:172
auto relocation_invoke() const noexcept -> invoke_t
Returns the function used to invoke this task's target.
Definition task.hpp:329
task_(const concept_t *vtable_ptr, invoke_t invoke_fn, void *source) noexcept
Constructs a task by relocating the target described by (vtable_ptr, invoke_fn) out of source,...
Definition task.hpp:343
stlab_v2_task_concept concept_t
Stable vtable describing how to move-construct, destroy, and introspect a task's target,...
Definition task.hpp:169
auto relocation_concept() const noexcept -> const concept_t *
Returns the vtable needed to relocate, invoke, or destroy this task's target without depending on its...
Definition task.hpp:324
auto relocation_source() noexcept -> void *
Returns the address of this task's inline model storage.
Definition task.hpp:334
constexpr std::size_t stlab_v2_task_storage_alignment
Alignment required by a v2 task's relocatable inline model.
Definition task.hpp:84
constexpr std::size_t stlab_v2_task_storage_size
Number of bytes reserved for a v2 task's relocatable inline model.
Definition task.hpp:79
typename noexcept_deducer< task_, F >::type task
task_ with noexcept deduced from the function type F (e.g. void() vs void() noexcept).
Definition task.hpp:454
Definition default_executor.hpp:49
Definition task.hpp:440
Versioned operation table used to relocate a task target across the shared executor ABI.
Definition task.hpp:64