Commit a9bfb179 authored by Philip Pronin's avatar Philip Pronin Committed by Sara Golemon

use folly::Baton in waitWithSemaphore

Test Plan:
fbconfig -r folly/wangle && fbmake runtests -j32

Reviewed By: lucian@fb.com

Subscribers: sdoroshenko, fugalh, njormrod

FB internal diff: D1525044

Tasks: 2680246

Blame Revision: D1358230
parent 924520ac
......@@ -16,8 +16,10 @@
#pragma once
#include <chrono>
#include <folly/wangle/detail/State.h>
#include <folly/LifoSem.h>
#include <folly/Baton.h>
namespace folly { namespace wangle {
......@@ -408,61 +410,47 @@ whenN(InputIterator first, InputIterator last, size_t n) {
template <typename T>
Future<T>
waitWithSemaphore(Future<T>&& f) {
LifoSem sem;
Baton<> baton;
auto done = f.then([&](Try<T> &&t) {
sem.post();
baton.post();
return std::move(t.value());
});
sem.wait();
baton.wait();
return done;
}
template<>
inline Future<void> waitWithSemaphore<void>(Future<void>&& f) {
LifoSem sem;
Baton<> baton;
auto done = f.then([&](Try<void> &&t) {
sem.post();
baton.post();
t.value();
});
sem.wait();
baton.wait();
return done;
}
template <typename T, class Duration>
Future<T>
waitWithSemaphore(Future<T>&& f, Duration timeout) {
auto sem = std::make_shared<LifoSem>();
auto done = f.then([sem](Try<T> &&t) {
sem->post();
auto baton = std::make_shared<Baton<>>();
auto done = f.then([baton](Try<T> &&t) {
baton->post();
return std::move(t.value());
});
std::thread t([sem, timeout](){
std::this_thread::sleep_for(timeout);
sem->shutdown();
});
t.detach();
try {
sem->wait();
} catch (ShutdownSemError & ign) { }
baton->timed_wait(std::chrono::system_clock::now() + timeout);
return done;
}
template <class Duration>
Future<void>
waitWithSemaphore(Future<void>&& f, Duration timeout) {
auto sem = std::make_shared<LifoSem>();
auto done = f.then([sem](Try<void> &&t) {
sem->post();
auto baton = std::make_shared<Baton<>>();
auto done = f.then([baton](Try<void> &&t) {
baton->post();
t.value();
});
std::thread t([sem, timeout](){
std::this_thread::sleep_for(timeout);
sem->shutdown();
});
t.detach();
try {
sem->wait();
} catch (ShutdownSemError & ign) { }
baton->timed_wait(std::chrono::system_clock::now() + timeout);
return done;
}
......
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