From 73df97f2d0a2a9b12e469154e6a17aa3870b75e6 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 23 Feb 2016 11:33:01 -0500 Subject: [PATCH] Add JobQueue::rendezvous --- src/ripple/core/JobQueue.h | 9 ++++++++- src/ripple/core/impl/JobQueue.cpp | 16 ++++++++++++++-- src/ripple/test/jtx/impl/Env.cpp | 3 +++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/ripple/core/JobQueue.h b/src/ripple/core/JobQueue.h index 655fd9aa5..7f5f2ddfb 100644 --- a/src/ripple/core/JobQueue.h +++ b/src/ripple/core/JobQueue.h @@ -28,8 +28,9 @@ #include #include #include -#include +#include #include +#include namespace ripple { @@ -95,6 +96,10 @@ public: // Cannot be const because LoadMonitor has no const methods. Json::Value getJson (int c = 0); + /** Block until no tasks running. */ + void + rendezvous(); + private: using JobDataMap = std::map ; @@ -116,6 +121,8 @@ private: beast::insight::Gauge job_count; beast::insight::Hook hook; + std::condition_variable cv_; + static JobTypes const& getJobTypes() { static JobTypes types; diff --git a/src/ripple/core/impl/JobQueue.cpp b/src/ripple/core/impl/JobQueue.cpp index ec1f3f182..ac0bd6d7f 100644 --- a/src/ripple/core/impl/JobQueue.cpp +++ b/src/ripple/core/impl/JobQueue.cpp @@ -305,6 +305,17 @@ JobQueue::getJson (int c) return ret; } +void +JobQueue::rendezvous() +{ + std::unique_lock lock(m_mutex); + cv_.wait(lock, [&] + { + return m_processCount == 0 && + m_jobSet.empty(); + }); +} + JobTypeData& JobQueue::getJobTypeData (JobType type) { @@ -476,11 +487,12 @@ JobQueue::processTask () { std::lock_guard lock (m_mutex); - finishJob (type); - --m_processCount; // Job should be destroyed before calling checkStopped // otherwise destructors with side effects can access // parent objects that are already destroyed. + finishJob (type); + if(--m_processCount == 0 && m_jobSet.empty()) + cv_.notify_all(); checkStopped (lock); } diff --git a/src/ripple/test/jtx/impl/Env.cpp b/src/ripple/test/jtx/impl/Env.cpp index a2ee4c70d..90e5bc3f5 100644 --- a/src/ripple/test/jtx/impl/Env.cpp +++ b/src/ripple/test/jtx/impl/Env.cpp @@ -184,6 +184,9 @@ Env::AppBundle::AppBundle(beast::unit_test::suite& suite, Env::AppBundle::~AppBundle() { client.reset(); + // Make sure all jobs finish, otherwise tests + // might not get the coverage they expect. + app->getJobQueue().rendezvous(); app->signalStop(); thread.join(); }