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