/* * Copyright (c) Facebook, Inc. and its affiliates. * * Licensed under the Apache License Version 2.0 with LLVM Exceptions * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * https://llvm.org/LICENSE.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #include namespace unifex { namespace _manual_event_loop { class context; struct task_base { using execute_fn = void(task_base*) noexcept; explicit task_base(execute_fn* execute) noexcept : execute_(execute) {} void execute() noexcept { this->execute_(this); } task_base* next_ = nullptr; execute_fn* execute_; }; template struct _op { class type; }; template using operation = typename _op>::type; template class _op::type final : task_base { using stop_token_type = stop_token_type_t; public: template explicit type(Receiver2&& receiver, context* loop) : task_base(&type::execute_impl) , receiver_((Receiver2 &&) receiver) , loop_(loop) {} void start() noexcept; private: static void execute_impl(task_base* t) noexcept { auto& self = *static_cast(t); if constexpr (is_stop_never_possible_v) { unifex::set_value(std::move(self.receiver_)); } else { if (get_stop_token(self.receiver_).stop_requested()) { unifex::set_done(std::move(self.receiver_)); } else { unifex::set_value(std::move(self.receiver_)); } } } UNIFEX_NO_UNIQUE_ADDRESS Receiver receiver_; context* const loop_; }; class context { template friend struct _op; public: class scheduler { class schedule_task { friend constexpr blocking_kind tag_invoke( tag_t, const schedule_task&) noexcept { return blocking_kind::never; } public: template < template class Variant, template class Tuple> using value_types = Variant>; template