Fix destructor race in Job

This commit is contained in:
Vinnie Falco
2016-02-19 19:08:24 -05:00
parent 92391332d7
commit 9ab5611c65
3 changed files with 40 additions and 30 deletions

View File

@@ -180,7 +180,7 @@ private:
//
// Invariants:
// <none>
void finishJob (Job const& job);
void finishJob (JobType type);
template <class Rep, class Period>
void on_dequeue (JobType type,

View File

@@ -80,6 +80,10 @@ void Job::doJob ()
m_loadEvent->reName (mName);
mJob (*this);
// Destroy the lambda, otherwise we won't include
// its duration in the time measurement
mJob = std::function<void(Job&)>();
}
void Job::rename (std::string const& newName)

View File

@@ -407,14 +407,11 @@ JobQueue::getNextJob (Job& job)
}
void
JobQueue::finishJob (Job const& job)
JobQueue::finishJob (JobType type)
{
JobType const type = job.getType ();
assert (m_jobSet.find (job) == m_jobSet.end ());
assert(type != jtINVALID);
JobTypeData& data (getJobTypeData (type));
JobTypeData& data = getJobTypeData (type);
// Queue a deferred task if possible
if (data.deferred > 0)
@@ -456,6 +453,9 @@ void JobQueue::on_execute (JobType type,
void
JobQueue::processTask ()
{
JobType type;
{
Job job;
@@ -487,10 +487,16 @@ JobQueue::processTask ()
JLOG(m_journal.trace) << "Skipping processTask ('" << data.name () << "')";
}
type = job.getType();
}
{
std::lock_guard <std::mutex> lock (m_mutex);
finishJob (job);
finishJob (type);
--m_processCount;
// Job should be destroyed before calling checkStopped
// otherwise destructors with side effects can access
// parent objects that are already destroyed.
checkStopped (lock);
}