2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <folly/Executor.h>
24 #include <folly/portability/BitsFunctexcept.h>
27 // An executor that supports timed scheduling. Like RxScheduler.
28 class ScheduledExecutor : public virtual Executor {
30 // Reality is that better than millisecond resolution is very hard to
31 // achieve. However, we reserve the right to be incredible.
32 typedef std::chrono::microseconds Duration;
33 typedef std::chrono::steady_clock::time_point TimePoint;
35 ~ScheduledExecutor() override = default;
37 void add(Func) override = 0;
39 /// Alias for add() (for Rx consistency)
40 void schedule(Func&& a) { add(std::move(a)); }
42 /// Schedule a Func to be executed after dur time has elapsed
43 /// Expect millisecond resolution at best.
44 void schedule(Func&& a, Duration const& dur) {
45 scheduleAt(std::move(a), now() + dur);
48 /// Schedule a Func to be executed at time t, or as soon afterward as
49 /// possible. Expect millisecond resolution at best. Must be threadsafe.
50 virtual void scheduleAt(Func&& /* a */, TimePoint const& /* t */) {
51 std::__throw_logic_error("unimplemented");
54 /// Get this executor's notion of time. Must be threadsafe.
55 virtual TimePoint now() {
56 return std::chrono::steady_clock::now();