2 * Copyright 2014 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.
19 #include <boost/noncopyable.hpp>
23 namespace folly { namespace wangle {
24 // Like an Rx Scheduler. We should probably rename it to match now that it
25 // has scheduling semantics too, but that's a codemod for another lazy
27 class Executor : boost::noncopyable {
29 typedef std::function<void()> Action;
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 virtual ~Executor() = default;
37 /// Enqueue an action to be performed by this executor. This and all
38 /// schedule variants must be threadsafe.
39 virtual void add(Action&&) = 0;
41 /// Alias for add() (for Rx consistency)
42 void schedule(Action&& a) { add(std::move(a)); }
44 /// Schedule an action to be executed after dur time has elapsed
45 /// Expect millisecond resolution at best.
46 void schedule(Action&& a, Duration const& dur) {
47 scheduleAt(std::move(a), now() + dur);
50 /// Schedule an action to be executed at time t, or as soon afterward as
51 /// possible. Expect millisecond resolution at best. Must be threadsafe.
52 virtual void scheduleAt(Action&& a, TimePoint const& t) {
53 throw std::logic_error("unimplemented");
56 /// Get this executor's notion of time. Must be threadsafe.
57 virtual TimePoint now() {
58 return std::chrono::steady_clock::now();