From afcc695edf6a43b6e037c2856c51d55837da3b74 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Thu, 12 May 2022 07:37:11 -0700 Subject: [PATCH] Scheduler Summary: Introduce a 'Scheduler' interface which will allow to decouple from the existing used Folly scheduler. Reviewed By: fabiomassimo Differential Revision: D36245587 fbshipit-source-id: 2f28bc1612e37ae53060a134d1c8059231fbc8ad --- xplat/Flipper/FlipperFollyScheduler.h | 40 +++++++++++++++++++ .../FlipperFollyScopedThreadScheduler.h | 40 +++++++++++++++++++ xplat/Flipper/FlipperScheduler.h | 26 ++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 xplat/Flipper/FlipperFollyScheduler.h create mode 100644 xplat/Flipper/FlipperFollyScopedThreadScheduler.h create mode 100644 xplat/Flipper/FlipperScheduler.h diff --git a/xplat/Flipper/FlipperFollyScheduler.h b/xplat/Flipper/FlipperFollyScheduler.h new file mode 100644 index 000000000..12620bf67 --- /dev/null +++ b/xplat/Flipper/FlipperFollyScheduler.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include "FlipperScheduler.h" + +#include +#include +#include + +namespace facebook { +namespace flipper { + +struct FollyScheduler : public facebook::flipper::Scheduler { + FollyScheduler(folly::EventBase* eventLoop) : eventLoop_(eventLoop) {} + virtual void schedule(Func&& t) override { + eventLoop_->add(t); + } + virtual void scheduleAfter(Func&& t, unsigned int ms) override { + folly::makeFuture() + .via(eventLoop_) + .delayed(std::chrono::milliseconds(ms)) + .thenValue([t](auto&&) { t(); }); + } + + virtual bool isRunningInOwnThread() override { + return eventLoop_->isInEventBaseThread(); + } + + private: + folly::EventBase* eventLoop_; +}; + +} // namespace flipper +} // namespace facebook diff --git a/xplat/Flipper/FlipperFollyScopedThreadScheduler.h b/xplat/Flipper/FlipperFollyScopedThreadScheduler.h new file mode 100644 index 000000000..7fc8b5b44 --- /dev/null +++ b/xplat/Flipper/FlipperFollyScopedThreadScheduler.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include "FlipperScheduler.h" + +#include +#include +#include + +namespace facebook { +namespace flipper { + +struct FollyScopedThreadScheduler : public Scheduler { + virtual void schedule(Func&& t) override { + thread_.getEventBase()->add(t); + } + + virtual void scheduleAfter(Func&& t, unsigned int ms) override { + folly::makeFuture() + .via(thread_.getEventBase()) + .delayed(std::chrono::milliseconds(ms)) + .thenValue([t](auto&&) { t(); }); + } + + virtual bool isRunningInOwnThread() override { + return thread_.getEventBase()->isInEventBaseThread(); + } + + private: + folly::ScopedEventBaseThread thread_; +}; + +} // namespace flipper +} // namespace facebook diff --git a/xplat/Flipper/FlipperScheduler.h b/xplat/Flipper/FlipperScheduler.h new file mode 100644 index 000000000..a8b2f545a --- /dev/null +++ b/xplat/Flipper/FlipperScheduler.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include + +namespace facebook { +namespace flipper { + +using Func = std::function; + +struct Scheduler { + virtual ~Scheduler() {} + virtual void schedule(Func&& t) = 0; + + virtual void scheduleAfter(Func&& t, unsigned int ms) = 0; + virtual bool isRunningInOwnThread() = 0; +}; + +} // namespace flipper +} // namespace facebook