void condvar_wait(Guard& guard, CondVar& condvar, bool has_timeout, boost::system_time timeout_time = boost::system_time()) { if (has_timeout) { condvar.timed_wait(guard, timeout_time); } else { condvar.wait(guard); } }
virtual void run() { CondVar childCreated; CondVar parentReady; AliveTestThreadPtr t = new AliveTestThread(childCreated, parentReady); t->start(); childCreated.waitForSignal(); test(t->isAlive()); parentReady.signal(); t->join(); test(!t->isAlive()); }
void synchronized_await(Mutex& mtx, CondVar& cv) { CPPA_REQUIRE(!closed()); if (try_block()) { std::unique_lock<Mutex> guard(mtx); while (blocked()) cv.wait(guard); } }
void AliveTest::run() { // // Check that calling isAlive() returns the correct result for alive and // and dead threads. // CondVar childCreated; CondVar parentReady; AliveTestThreadPtr t = new AliveTestThread(childCreated, parentReady); IceUtil::ThreadControl c = t->start(); childCreated.waitForSignal(); test(t->isAlive()); parentReady.signal(); c.join(); test(!t->isAlive()); }
void synchronized_incr(Mutex& mtx, CondVar& cv) { incr( [&](self_t& seq) { std::unique_lock<Mutex> guard{mtx}; cv.notify_one(); }); }
void synchronized_await(Mutex& mtx, CondVar& cv) { CAF_ASSERT(! closed()); if (! can_fetch_more() && try_block()) { std::unique_lock<Mutex> guard(mtx); while (blocked()) { cv.wait(guard); } } }
static void test_cond_var() { CondVar cv; NaCs::tic(); for (int i = 0;i < N;i++) { cv.notify_one(); } tocPerCycle(); NaCs::tic(); for (int i = 0;i < N;i++) { cv.notify_all(); } tocPerCycle(); std::thread thread([&] { Locker locker; for (int i = 0;i < N * 2;i++) { cv.wait(locker); std::this_thread::yield(); } }); thread.detach(); NaCs::tic(); for (int i = 0;i < N;i++) { cv.notify_one(); } tocPerCycle(); NaCs::tic(); for (int i = 0;i < N;i++) { cv.notify_all(); } tocPerCycle(); }
bool synchronized_await(Mutex& mtx, CondVar& cv, const TimePoint& timeout) { CAF_ASSERT(!closed()); if (try_block()) { std::unique_lock<Mutex> guard(mtx); while (blocked()) { if (cv.wait_until(guard, timeout) == std::cv_status::timeout) { // if we're unable to set the queue from blocked to empty, // than there's a new element in the list return !try_unblock(); } } } return true; }
bool synchronized_push_front(Mutex& mtx, CondVar& cv, pointer ptr) { switch (push_front(ptr)) { default: // enqueued message to a running actor's mailbox return true; case inbox_result::unblocked_reader: { std::unique_lock<Mutex> guard(mtx); cv.notify_one(); return true; } case inbox_result::queue_closed: // actor no longer alive return false; } }
bool synchronized_enqueue(Mutex& mtx, CondVar& cv, pointer new_element) { switch (enqueue(new_element)) { case enqueue_result::unblocked_reader: { std::unique_lock<Mutex> guard(mtx); cv.notify_one(); return true; } case enqueue_result::success: // enqueued message to a running actor's mailbox return true; case enqueue_result::queue_closed: // actor no longer alive return false; } // should be unreachable CPPA_CRITICAL("invalid result of enqueue()"); }
size_t pri_synchronized_reset(Mutex& mtx, CondVar& cv) { ACTX_ASSERTS(!blocked()); auto c = reset(); if (c != 0) { return c; } std::unique_lock<Mutex> guard{mtx}; block(); auto _ = gsl::finally([this]() {unblock(); }); c = reset(); while (c == 0) { cv.wait(guard); c = reset(); } return c; }
std::pair<size_t, duration_t> synchronized_reset(Mutex& mtx, CondVar& cv, TimeDuration const& timeout) { ACTX_ASSERTS(!blocked()); if (timeout <= TimeDuration::zero()) { auto c = reset(); return std::make_pair(c, TimeDuration::zero()); } if (timeout >= (TimeDuration::max)()) { auto nw = eclipse_clock_t::now(); auto c = pri_synchronized_reset(mtx, cv); return std::make_pair(c, eclipse_clock_t::now() - nw); } auto c = reset(); if (c != 0) { return std::make_pair(c, TimeDuration::zero()); } std::unique_lock<Mutex> guard{mtx}; block(); auto _ = gsl::finally([this]() {unblock(); }); c = reset(); if (c != 0) { return std::make_pair(c, TimeDuration::zero()); } auto const timeout_dur = std::chrono::duration_cast<duration_t>(timeout); auto curr_timeout = timeout_dur; auto final_eclipse = TimeDuration::zero(); while (c == 0) { auto bt = eclipse_clock_t::now(); auto status = cv.wait_for(guard, curr_timeout); if (status == std::cv_status::timeout) { c = reset(); final_eclipse = timeout_dur; break; } else { auto eclipse = eclipse_clock_t::now() - bt; c = reset(); final_eclipse += eclipse; if (eclipse >= curr_timeout) { break; } curr_timeout -= eclipse; } } return std::make_pair(c, final_eclipse); }