From: Philip Pronin Date: Thu, 28 Aug 2014 11:05:45 +0000 (-0700) Subject: use folly::Baton in waitWithSemaphore X-Git-Tag: v0.22.0~380 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=a9bfb17955960ed3a93eaeb3d12a74ac1af7d8c7;p=folly.git 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 --- diff --git a/folly/wangle/Future-inl.h b/folly/wangle/Future-inl.h index 782afe4b..de8dbf4b 100644 --- a/folly/wangle/Future-inl.h +++ b/folly/wangle/Future-inl.h @@ -16,8 +16,10 @@ #pragma once +#include + #include -#include +#include namespace folly { namespace wangle { @@ -408,61 +410,47 @@ whenN(InputIterator first, InputIterator last, size_t n) { template Future waitWithSemaphore(Future&& f) { - LifoSem sem; + Baton<> baton; auto done = f.then([&](Try &&t) { - sem.post(); + baton.post(); return std::move(t.value()); }); - sem.wait(); + baton.wait(); return done; } template<> inline Future waitWithSemaphore(Future&& f) { - LifoSem sem; + Baton<> baton; auto done = f.then([&](Try &&t) { - sem.post(); + baton.post(); t.value(); }); - sem.wait(); + baton.wait(); return done; } template Future waitWithSemaphore(Future&& f, Duration timeout) { - auto sem = std::make_shared(); - auto done = f.then([sem](Try &&t) { - sem->post(); + auto baton = std::make_shared>(); + auto done = f.then([baton](Try &&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 Future waitWithSemaphore(Future&& f, Duration timeout) { - auto sem = std::make_shared(); - auto done = f.then([sem](Try &&t) { - sem->post(); + auto baton = std::make_shared>(); + auto done = f.then([baton](Try &&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; }