/* * Copyright (c) Facebook, Inc. and its 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 namespace folly { namespace fibers { template auto FiberManager::addTaskFuture(F&& func) -> folly::Future>> { using T = invoke_result_t; using FutureT = folly::lift_unit_t; folly::Promise p; auto f = p.getFuture(); addTaskFinally( [func = std::forward(func)]() mutable { return func(); }, [p = std::move(p)](folly::Try&& t) mutable { p.setTry(std::move(t)); }); return f; } template auto FiberManager::addTaskEagerFuture(F&& func) -> folly::Future>> { using T = invoke_result_t; using FutureT = typename folly::lift_unit::type; folly::Promise p; auto f = p.getFuture(); addTaskFinallyEager( [func = std::forward(func)]() mutable { return func(); }, [p = std::move(p)](folly::Try&& t) mutable { p.setTry(std::move(t)); }); return f; } template auto FiberManager::addTaskRemoteFuture(F&& func) -> folly::Future>> { folly::Promise>> p; auto f = p.getFuture(); addTaskRemote( [p = std::move(p), func = std::forward(func), this]() mutable { auto t = folly::makeTryWithNoUnwrap(std::forward(func)); runInMainContext([&]() { p.setTry(std::move(t)); }); }); return f; } } // namespace fibers } // namespace folly