Add JobQueue::rendezvous

This commit is contained in:
Vinnie Falco
2016-02-23 11:33:01 -05:00
parent e8b75b80c2
commit 73df97f2d0
3 changed files with 25 additions and 3 deletions

View File

@@ -28,8 +28,9 @@
#include <beast/threads/Stoppable.h>
#include <beast/module/core/thread/Workers.h>
#include <boost/function.hpp>
#include <thread>
#include <condition_variable>
#include <set>
#include <thread>
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 <JobType, JobTypeData>;
@@ -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;

View File

@@ -305,6 +305,17 @@ JobQueue::getJson (int c)
return ret;
}
void
JobQueue::rendezvous()
{
std::unique_lock<std::mutex> 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 <std::mutex> 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);
}

View File

@@ -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();
}