X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ffutures%2FManualExecutor.cpp;h=50941d02567f59717a5b3b5cf75830de5f2d637a;hb=7ebe7c2a249de44209e8bcc453e1a70bafd4ed19;hp=8dcd5f12649f0622ab54e6ad6e04a5dac892e781;hpb=275ca94d04e44f28cfa411668eb1c1dd8db90b80;p=folly.git diff --git a/folly/futures/ManualExecutor.cpp b/folly/futures/ManualExecutor.cpp index 8dcd5f12..50941d02 100644 --- a/folly/futures/ManualExecutor.cpp +++ b/folly/futures/ManualExecutor.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2015 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,20 +20,12 @@ #include #include -#include - namespace folly { -ManualExecutor::ManualExecutor() { - if (sem_init(&sem_, 0, 0) == -1) { - throw std::runtime_error(std::string("sem_init: ") + strerror(errno)); - } -} - void ManualExecutor::add(Func callback) { std::lock_guard lock(lock_); - funcs_.push(std::move(callback)); - sem_post(&sem_); + funcs_.emplace(std::move(callback)); + sem_.post(); } size_t ManualExecutor::run() { @@ -46,9 +38,10 @@ size_t ManualExecutor::run() { while (!scheduledFuncs_.empty()) { auto& sf = scheduledFuncs_.top(); - if (sf.time > now_) + if (sf.time > now_) { break; - funcs_.push(sf.func); + } + funcs_.emplace(sf.moveOutFunc()); scheduledFuncs_.pop(); } @@ -65,7 +58,7 @@ size_t ManualExecutor::run() { // Balance the semaphore so it doesn't grow without bound // if nobody is calling wait(). // This may fail (with EAGAIN), that's fine. - sem_trywait(&sem_); + sem_.tryWait(); func = std::move(funcs_.front()); funcs_.pop(); @@ -80,17 +73,12 @@ void ManualExecutor::wait() { while (true) { { std::lock_guard lock(lock_); - if (!funcs_.empty()) + if (!funcs_.empty()) { break; + } } - auto ret = sem_wait(&sem_); - if (ret == 0) { - break; - } - if (errno != EINVAL) { - throw std::runtime_error(std::string("sem_wait: ") + strerror(errno)); - } + sem_.wait(); } } @@ -101,4 +89,4 @@ void ManualExecutor::advanceTo(TimePoint const& t) { run(); } -} // folly +} // namespace folly