EventBase::runInEventLoopThreadAndWait.
[folly.git] / folly / io / async / EventBase.cpp
index e956ca1881623e245369394c6e297243072956ae..00aa9d80d0a0b7ea63ba31453de1afda67c4bb1f 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <folly/io/async/EventBase.h>
 
+#include <folly/Baton.h>
 #include <folly/ThreadName.h>
 #include <folly/io/async/NotificationQueue.h>
 
@@ -562,6 +563,40 @@ bool EventBase::runInEventBaseThread(const Cob& fn) {
   return true;
 }
 
+bool EventBase::runInEventBaseThreadAndWait(void (*fn)(void*), void* arg) {
+  if (inRunningEventBaseThread()) {
+    LOG(ERROR) << "EventBase " << this << ": Waiting in the event loop is not "
+               << "allowed";
+    return false;
+  }
+
+  Baton<> ready;
+  runInEventBaseThread([&] {
+      fn(arg);
+      ready.post();
+  });
+  ready.wait();
+
+  return true;
+}
+
+bool EventBase::runInEventBaseThreadAndWait(const Cob& fn) {
+  if (inRunningEventBaseThread()) {
+    LOG(ERROR) << "EventBase " << this << ": Waiting in the event loop is not "
+               << "allowed";
+    return false;
+  }
+
+  Baton<> ready;
+  runInEventBaseThread([&] {
+      fn();
+      ready.post();
+  });
+  ready.wait();
+
+  return true;
+}
+
 bool EventBase::runAfterDelay(const Cob& cob,
                                int milliseconds,
                                TimeoutManager::InternalEnum in) {