2 * Copyright 2015 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.
26 typedef std::function<void()> Func;
28 /// An Executor accepts units of work with add(), which should be
32 virtual ~Executor() = default;
34 /// Enqueue a function to executed by this executor. This and all
35 /// variants must be threadsafe.
36 virtual void add(Func) = 0;
38 /// Enqueue a function with a given priority, where 0 is the medium priority
39 /// This is up to the implementation to enforce
40 virtual void addWithPriority(Func, int8_t /*priority*/) {
41 throw std::runtime_error(
42 "addWithPriority() is not implemented for this Executor");
45 virtual uint8_t getNumPriorities() const {
49 static const int8_t LO_PRI = SCHAR_MIN;
50 static const int8_t MID_PRI = 0;
51 static const int8_t HI_PRI = SCHAR_MAX;
53 /// A convenience function for shared_ptr to legacy functors.
55 /// Sometimes you have a functor that is move-only, and therefore can't be
56 /// converted to a std::function (e.g. std::packaged_task). In that case,
57 /// wrap it in a shared_ptr (or maybe folly::MoveWrapper) and use this.
60 this->add([fn]() mutable { (*fn)(); });