Task handle used by a wait handler
| 31 | |
| 32 | // Task handle used by a wait handler |
| 33 | class task_wait_handle { |
| 34 | detail::task_base* handle; |
| 35 | |
| 36 | // Allow construction in wait_for_task() |
| 37 | friend LIBASYNC_EXPORT void detail::wait_for_task(detail::task_base* t); |
| 38 | task_wait_handle(detail::task_base* t) |
| 39 | : handle(t) {} |
| 40 | |
| 41 | // Execution function for use by wait handlers |
| 42 | template<typename Func> |
| 43 | struct wait_exec_func: private detail::func_base<Func> { |
| 44 | template<typename F> |
| 45 | explicit wait_exec_func(F&& f) |
| 46 | : detail::func_base<Func>(std::forward<F>(f)) {} |
| 47 | void operator()(detail::task_base*) |
| 48 | { |
| 49 | // Just call the function directly, all this wrapper does is remove |
| 50 | // the task_base* parameter. |
| 51 | this->get_func()(); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | public: |
| 56 | task_wait_handle() |
| 57 | : handle(nullptr) {} |
| 58 | |
| 59 | // Check if the handle is valid |
| 60 | explicit operator bool() const |
| 61 | { |
| 62 | return handle != nullptr; |
| 63 | } |
| 64 | |
| 65 | // Check if the task has finished executing |
| 66 | bool ready() const |
| 67 | { |
| 68 | return detail::is_finished(handle->state.load(std::memory_order_acquire)); |
| 69 | } |
| 70 | |
| 71 | // Queue a function to be executed when the task has finished executing. |
| 72 | template<typename Func> |
| 73 | void on_finish(Func&& func) |
| 74 | { |
| 75 | // Make sure the function type is callable |
| 76 | static_assert(detail::is_callable<Func()>::value, "Invalid function type passed to on_finish()"); |
| 77 | |
| 78 | auto cont = new detail::task_func<typename std::remove_reference<decltype(inline_scheduler())>::type, wait_exec_func<typename std::decay<Func>::type>, detail::fake_void>(std::forward<Func>(func)); |
| 79 | cont->sched = std::addressof(inline_scheduler()); |
| 80 | handle->add_continuation(inline_scheduler(), detail::task_ptr(cont)); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | // Wait handler function prototype |
| 85 | typedef void (*wait_handler)(task_wait_handle t); |
no outgoing calls
no test coverage detected