/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * 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 #include namespace folly { namespace detail { /** * @class SerialExecutor * * @brief Executor that guarantees serial non-concurrent execution of added * tasks * * SerialExecutor is similar to boost asio's strand concept. A SerialExecutor * has a parent executor which is given at construction time (defaults to * folly's global CPUExecutor). Tasks added to SerialExecutor are executed * in the parent executor, however strictly non-concurrently and in the order * they were added. * * When a task is added to the executor while another one is running on the * parent executor, the new task is piggybacked on the running task to save the * cost of scheduling a task on the parent executor. This implies that the * parent executor may observe a smaller number of tasks than those added in the * SerialExecutor. * * The SerialExecutor may be deleted at any time. All tasks that have been * submitted will still be executed with the same guarantees, as long as the * parent executor is executing tasks. * * NOTE: This describes low-level executor tasks. Not coro::Task tasks. * This executor does not guarantee that coro::Task tasks will be completed in * the order that they are added. Rather, a coro::Task task may be suspended at * a co_await/co_yield point and another such task that has been added to this * executor may be resumed at that point. */ template