49b3c5d74d7c300d241dd694f175ae0852dfe662
[folly.git] / folly / futures / DrivableExecutor.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <folly/Executor.h>
20
21 namespace folly {
22
23 /*
24  * A DrivableExecutor can be driven via its drive() method
25  * Examples include EventBase (via loopOnce()) and ManualExecutor
26  * (via makeProgress()).
27  *
28  * This interface is most handy in conjunction with
29  * Future<T>::getVia(DrivableExecutor*) and
30  * Future<T>::waitVia(DrivableExecutor*)
31  *
32  * These call drive() * repeatedly until the Future is fulfilled.
33  * getVia() returns the value (or throws the exception) and waitVia() returns
34  * the same Future for chainability.
35  *
36  * These will be most helpful in tests, for instance if you need to pump a mock
37  * EventBase until Futures complete.
38  */
39
40 class DrivableExecutor : public virtual Executor {
41  public:
42   ~DrivableExecutor() override = default;
43
44   // Make progress on this Executor's work.
45   //
46   // Drive *must not* busy wait if there is no work to do.  Instead,
47   // sleep (using a semaphore or similar) until at least one event is
48   // processed.
49   // I.e. make_future().via(foo).then(...).getVia(DrivableExecutor)
50   // must not spin, even though nothing happens on the drivable
51   // executor.
52   virtual void drive() = 0;
53 };
54
55 } // folly