folly::Unit::Drop.
[folly.git] / folly / futures / ManualExecutor.cpp
index c18476e58d92f30a04d4595e0aba04dc52513a32..9a0d1e7ba47ae79063adf142f7cf862ff80ba45f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 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() {
@@ -65,7 +59,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();
@@ -84,13 +78,7 @@ void ManualExecutor::wait() {
         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();
   }
 }