From: James Sedgwick Date: Wed, 18 Oct 2017 22:01:33 +0000 (-0700) Subject: move futures/ScheduledExecutor to executors/ScheduledExecutor X-Git-Tag: v2017.10.23.00~24 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=dbf0c41058bf6f45ed016856515700842ae73f1a move futures/ScheduledExecutor to executors/ScheduledExecutor Summary: see title Reviewed By: yfeldblum Differential Revision: D6062601 fbshipit-source-id: edd9a5e85f4ebecd1a6f1004a4d3b8b43b935c2b --- diff --git a/folly/Makefile.am b/folly/Makefile.am index bb2d3e14..b9adb7f6 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -100,6 +100,7 @@ nobase_follyinclude_HEADERS = \ executors/NotificationQueueExecutor.h \ executors/PriorityLifoSemMPMCQueue.h \ executors/PriorityThreadFactory.h \ + executors/ScheduledExecutor.h \ executors/SerialExecutor.h \ executors/ThreadFactory.h \ executors/ThreadPoolExecutor.h \ @@ -218,8 +219,6 @@ nobase_follyinclude_HEADERS = \ futures/Promise-inl.h \ futures/Promise.h \ futures/QueuedImmediateExecutor.h \ - futures/Retrying.h \ - futures/ScheduledExecutor.h \ futures/SharedPromise.h \ futures/SharedPromise-inl.h \ futures/ThreadWheelTimekeeper.h \ diff --git a/folly/executors/ScheduledExecutor.h b/folly/executors/ScheduledExecutor.h new file mode 100644 index 00000000..ac77de9d --- /dev/null +++ b/folly/executors/ScheduledExecutor.h @@ -0,0 +1,59 @@ +/* + * Copyright 2017 Facebook, Inc. + * + * 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 + +namespace folly { + // An executor that supports timed scheduling. Like RxScheduler. + class ScheduledExecutor : public virtual Executor { + public: + // Reality is that better than millisecond resolution is very hard to + // achieve. However, we reserve the right to be incredible. + typedef std::chrono::microseconds Duration; + typedef std::chrono::steady_clock::time_point TimePoint; + + ~ScheduledExecutor() override = default; + + void add(Func) override = 0; + + /// Alias for add() (for Rx consistency) + void schedule(Func&& a) { add(std::move(a)); } + + /// Schedule a Func to be executed after dur time has elapsed + /// Expect millisecond resolution at best. + void schedule(Func&& a, Duration const& dur) { + scheduleAt(std::move(a), now() + dur); + } + + /// Schedule a Func to be executed at time t, or as soon afterward as + /// possible. Expect millisecond resolution at best. Must be threadsafe. + virtual void scheduleAt(Func&& /* a */, TimePoint const& /* t */) { + std::__throw_logic_error("unimplemented"); + } + + /// Get this executor's notion of time. Must be threadsafe. + virtual TimePoint now() { + return std::chrono::steady_clock::now(); + } + }; +} diff --git a/folly/futures/ManualExecutor.h b/folly/futures/ManualExecutor.h index 373e4ac7..a45de257 100644 --- a/folly/futures/ManualExecutor.h +++ b/folly/futures/ManualExecutor.h @@ -23,7 +23,7 @@ #include #include -#include +#include namespace folly { /// A ManualExecutor only does work when you turn the crank, by calling diff --git a/folly/futures/README.md b/folly/futures/README.md index e4bce679..b839056d 100644 --- a/folly/futures/README.md +++ b/folly/futures/README.md @@ -730,7 +730,7 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac
  • ManualExecutor only executes work when manually cranked. This is useful for testing.
  • InlineExecutor executes work immediately inline
  • QueuedImmediateExecutor is similar to InlineExecutor, but work added during callback execution will be queued instead of immediately executed
  • -
  • ScheduledExecutor is a subinterface of Executor that supports scheduled (i.e. delayed) execution. There aren't many implementations yet, see T5924392
  • +
  • ScheduledExecutor is a subinterface of Executor that supports scheduled (i.e. delayed) execution. There aren't many implementations yet, see T5924392
  • Thrift's ThreadManager is an Executor but we aim to deprecate it in favor of the aforementioned CPUThreadPoolExecutor
  • FutureExecutor wraps another Executor and provides Future<T> addFuture(F func) which returns a Future representing the result of func. This is equivalent to futures::async(executor, func) and the latter should probably be preferred.
  • Timeouts and related features

    Futures provide a number of timing-related features. Here's an overview.

    Timing implementation #

    diff --git a/folly/futures/ScheduledExecutor.h b/folly/futures/ScheduledExecutor.h deleted file mode 100644 index ac77de9d..00000000 --- a/folly/futures/ScheduledExecutor.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2017 Facebook, Inc. - * - * 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 - -namespace folly { - // An executor that supports timed scheduling. Like RxScheduler. - class ScheduledExecutor : public virtual Executor { - public: - // Reality is that better than millisecond resolution is very hard to - // achieve. However, we reserve the right to be incredible. - typedef std::chrono::microseconds Duration; - typedef std::chrono::steady_clock::time_point TimePoint; - - ~ScheduledExecutor() override = default; - - void add(Func) override = 0; - - /// Alias for add() (for Rx consistency) - void schedule(Func&& a) { add(std::move(a)); } - - /// Schedule a Func to be executed after dur time has elapsed - /// Expect millisecond resolution at best. - void schedule(Func&& a, Duration const& dur) { - scheduleAt(std::move(a), now() + dur); - } - - /// Schedule a Func to be executed at time t, or as soon afterward as - /// possible. Expect millisecond resolution at best. Must be threadsafe. - virtual void scheduleAt(Func&& /* a */, TimePoint const& /* t */) { - std::__throw_logic_error("unimplemented"); - } - - /// Get this executor's notion of time. Must be threadsafe. - virtual TimePoint now() { - return std::chrono::steady_clock::now(); - } - }; -}