Commit f58b1182 authored by Hans Fugal's avatar Hans Fugal Committed by Sara Golemon

use LifoSem instead of sem_t for ManualExecutor

Summary: OSX apparently knows better than all of us plebes, and got rid of anonymous semaphores. Rather than try to work around that directly, I just switched to folly::LifoSem, which seems like a not-terrible idea anyway.

Reviewed By: @jsedgwick

Differential Revision: D2143566
parent d167b415
...@@ -24,16 +24,10 @@ ...@@ -24,16 +24,10 @@
namespace folly { 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) { void ManualExecutor::add(Func callback) {
std::lock_guard<std::mutex> lock(lock_); std::lock_guard<std::mutex> lock(lock_);
funcs_.push(std::move(callback)); funcs_.push(std::move(callback));
sem_post(&sem_); sem_.post();
} }
size_t ManualExecutor::run() { size_t ManualExecutor::run() {
...@@ -65,7 +59,7 @@ size_t ManualExecutor::run() { ...@@ -65,7 +59,7 @@ size_t ManualExecutor::run() {
// Balance the semaphore so it doesn't grow without bound // Balance the semaphore so it doesn't grow without bound
// if nobody is calling wait(). // if nobody is calling wait().
// This may fail (with EAGAIN), that's fine. // This may fail (with EAGAIN), that's fine.
sem_trywait(&sem_); sem_.tryWait();
func = std::move(funcs_.front()); func = std::move(funcs_.front());
funcs_.pop(); funcs_.pop();
...@@ -84,13 +78,7 @@ void ManualExecutor::wait() { ...@@ -84,13 +78,7 @@ void ManualExecutor::wait() {
break; break;
} }
auto ret = sem_wait(&sem_); sem_.wait();
if (ret == 0) {
break;
}
if (errno != EINVAL) {
throw std::runtime_error(std::string("sem_wait: ") + strerror(errno));
}
} }
} }
......
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
*/ */
#pragma once #pragma once
#include <folly/LifoSem.h>
#include <folly/futures/DrivableExecutor.h> #include <folly/futures/DrivableExecutor.h>
#include <folly/futures/ScheduledExecutor.h> #include <folly/futures/ScheduledExecutor.h>
#include <semaphore.h>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <queue> #include <queue>
...@@ -35,8 +35,6 @@ namespace folly { ...@@ -35,8 +35,6 @@ namespace folly {
class ManualExecutor : public DrivableExecutor, class ManualExecutor : public DrivableExecutor,
public ScheduledExecutor { public ScheduledExecutor {
public: public:
ManualExecutor();
void add(Func) override; void add(Func) override;
/// Do work. Returns the number of functions that were executed (maybe 0). /// Do work. Returns the number of functions that were executed (maybe 0).
...@@ -78,7 +76,7 @@ namespace folly { ...@@ -78,7 +76,7 @@ namespace folly {
virtual void scheduleAt(Func&& f, TimePoint const& t) override { virtual void scheduleAt(Func&& f, TimePoint const& t) override {
std::lock_guard<std::mutex> lock(lock_); std::lock_guard<std::mutex> lock(lock_);
scheduledFuncs_.emplace(t, std::move(f)); scheduledFuncs_.emplace(t, std::move(f));
sem_post(&sem_); sem_.post();
} }
/// Advance the clock. The clock never advances on its own. /// Advance the clock. The clock never advances on its own.
...@@ -98,7 +96,7 @@ namespace folly { ...@@ -98,7 +96,7 @@ namespace folly {
private: private:
std::mutex lock_; std::mutex lock_;
std::queue<Func> funcs_; std::queue<Func> funcs_;
sem_t sem_; LifoSem sem_;
// helper class to enable ordering of scheduled events in the priority // helper class to enable ordering of scheduled events in the priority
// queue // queue
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment