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
This commit is contained in:
Lorenzo Blasa
2022-05-12 07:37:11 -07:00
committed by Facebook GitHub Bot
parent 996132afbd
commit afcc695edf
3 changed files with 106 additions and 0 deletions

View File

@@ -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 <folly/futures/Future.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/ScopedEventBaseThread.h>
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

View File

@@ -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 <folly/futures/Future.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/ScopedEventBaseThread.h>
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

View File

@@ -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 <functional>
namespace facebook {
namespace flipper {
using Func = std::function<void()>;
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