2 * Copyright 2016 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.
17 #include <folly/futures/ManualExecutor.h>
27 void ManualExecutor::add(Func callback) {
28 std::lock_guard<std::mutex> lock(lock_);
29 funcs_.push(std::move(callback));
33 size_t ManualExecutor::run() {
39 std::lock_guard<std::mutex> lock(lock_);
41 while (!scheduledFuncs_.empty()) {
42 auto& sf = scheduledFuncs_.top();
46 scheduledFuncs_.pop();
52 for (count = 0; count < n; count++) {
54 std::lock_guard<std::mutex> lock(lock_);
59 // Balance the semaphore so it doesn't grow without bound
60 // if nobody is calling wait().
61 // This may fail (with EAGAIN), that's fine.
64 func = std::move(funcs_.front());
73 void ManualExecutor::wait() {
76 std::lock_guard<std::mutex> lock(lock_);
85 void ManualExecutor::advanceTo(TimePoint const& t) {