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<std::mutex> lock(lock_);
funcs_.push(std::move(callback));
- sem_post(&sem_);
+ sem_.post();
}
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();
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();
}
}
*/
#pragma once
+#include <folly/LifoSem.h>
#include <folly/futures/DrivableExecutor.h>
#include <folly/futures/ScheduledExecutor.h>
-#include <semaphore.h>
#include <memory>
#include <mutex>
#include <queue>
class ManualExecutor : public DrivableExecutor,
public ScheduledExecutor {
public:
- ManualExecutor();
-
void add(Func) override;
/// Do work. Returns the number of functions that were executed (maybe 0).
virtual void scheduleAt(Func&& f, TimePoint const& t) override {
std::lock_guard<std::mutex> lock(lock_);
scheduledFuncs_.emplace(t, std::move(f));
- sem_post(&sem_);
+ sem_.post();
}
/// Advance the clock. The clock never advances on its own.
private:
std::mutex lock_;
std::queue<Func> funcs_;
- sem_t sem_;
+ LifoSem sem_;
// helper class to enable ordering of scheduled events in the priority
// queue