2017-07-03 17:05:01 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2017-07-03 17:05:01 +10:00
|
|
|
*
|
2018-03-15 15:21:05 +11:00
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
2017-07-03 17:05:01 +10:00
|
|
|
*/
|
|
|
|
|
2018-03-22 16:10:06 +11:00
|
|
|
#include "queue.hpp"
|
2017-07-03 17:05:01 +10:00
|
|
|
|
2018-03-27 16:16:13 +11:00
|
|
|
#include "../parse.hpp"
|
2018-06-13 15:43:01 +10:00
|
|
|
#include "../scoped.hpp"
|
2018-03-15 15:21:05 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::job::queue;
|
2017-07-03 17:05:01 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-03-27 16:16:13 +11:00
|
|
|
static unsigned
|
|
|
|
default_parallelism (void)
|
|
|
|
{
|
|
|
|
if (auto var = getenv ("JOB_THREADS")) {
|
2018-08-05 14:42:02 +10:00
|
|
|
return cruft::parse<unsigned> (var);
|
2018-03-27 16:16:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::thread::hardware_concurrency ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
static unsigned
|
|
|
|
default_depth (void)
|
|
|
|
{
|
|
|
|
if (auto var = getenv ("JOB_DEPTH")) {
|
2018-08-05 14:42:02 +10:00
|
|
|
return cruft::parse<unsigned> (var);
|
2018-03-27 16:16:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
queue::queue ():
|
|
|
|
queue (default_parallelism (), default_depth ())
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-03-22 14:59:03 +11:00
|
|
|
queue::queue (unsigned thread_count, unsigned pool_size):
|
2018-03-15 15:21:05 +11:00
|
|
|
m_tasks {
|
2018-08-05 14:42:02 +10:00
|
|
|
{}, cruft::pool<task> {pool_size}, {}, {}
|
2018-03-15 15:21:05 +11:00
|
|
|
},
|
|
|
|
m_pending (0),
|
2018-03-22 14:59:03 +11:00
|
|
|
m_threads (thread_count),
|
|
|
|
m_doomed (0),
|
|
|
|
m_reaper (&queue::reap, this)
|
2017-07-03 17:05:01 +10:00
|
|
|
{
|
2018-03-22 14:59:03 +11:00
|
|
|
CHECK_GE (m_tasks.finishing.capacity (), thread_count);
|
|
|
|
|
2017-07-03 17:05:01 +10:00
|
|
|
for (auto &t: m_threads)
|
2018-03-15 15:21:05 +11:00
|
|
|
t = std::thread (&queue::loop, this);
|
2017-07-03 17:05:01 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
queue::~queue ()
|
|
|
|
{
|
2018-03-15 15:21:05 +11:00
|
|
|
m_stopping = true;
|
|
|
|
|
|
|
|
// raise the semaphore enough times to resume all the worker threads
|
|
|
|
for (size_t i = 0; i < m_threads.size (); ++i)
|
|
|
|
m_pending.release ();
|
2017-07-03 17:05:01 +10:00
|
|
|
|
|
|
|
// wait for everyone to tidy up. perhaps we'd like to use a timeout, but
|
|
|
|
// if things deadlock then it's the users fault currently.
|
2018-03-15 15:21:05 +11:00
|
|
|
for (auto &t: m_threads)
|
2017-07-03 17:05:01 +10:00
|
|
|
t.join ();
|
2018-03-22 14:59:03 +11:00
|
|
|
|
|
|
|
// wake the reaper up with enough tasks that it's guaranteed to resume.
|
|
|
|
// it will bail early and drain the queue, so the validity of the increment
|
|
|
|
// isn't super important.
|
|
|
|
m_doomed.release (m_tasks.finishing.capacity ());
|
|
|
|
m_reaper.join ();
|
2017-07-03 17:05:01 +10:00
|
|
|
}
|
|
|
|
|
2018-03-15 15:21:05 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
queue::loop ()
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
m_pending.acquire ();
|
|
|
|
if (m_stopping)
|
|
|
|
return;
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::scoped::increment running_count (m_running);
|
2018-03-15 15:21:05 +11:00
|
|
|
|
|
|
|
CHECK (!m_tasks.pending->empty ());
|
|
|
|
|
|
|
|
auto todo = [this] () {
|
|
|
|
auto obj = m_tasks.pending.acquire ();
|
|
|
|
auto res = obj->front ();
|
|
|
|
obj->pop_front ();
|
|
|
|
return res;
|
|
|
|
} ();
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::scoped::function cleanup ([&todo, this] () noexcept {
|
2018-03-22 14:59:03 +11:00
|
|
|
while (!m_tasks.finishing.push (todo))
|
|
|
|
;
|
|
|
|
m_doomed.release ();
|
2018-03-15 15:21:05 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
todo->function (*todo);
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 14:59:03 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
queue::reap ()
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
// wait until we might have something to work with
|
|
|
|
m_doomed.acquire ();
|
|
|
|
if (m_stopping)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// pop and notify as many doomed tasks as we can
|
|
|
|
int count = 0;
|
|
|
|
for (task *item; m_tasks.finishing.pop (item); ++count) {
|
2018-08-13 14:51:33 +10:00
|
|
|
item->done.notify_all ();
|
2018-03-22 14:59:03 +11:00
|
|
|
m_tasks.notified.push_back (item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroy any tasks that have a zero reference count
|
|
|
|
m_tasks.notified.erase (
|
|
|
|
std::remove_if (
|
|
|
|
m_tasks.notified.begin (),
|
|
|
|
m_tasks.notified.end (),
|
|
|
|
[&] (auto i) {
|
|
|
|
if (i->references.value () < 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_tasks.store.destroy (i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
),
|
|
|
|
m_tasks.notified.end ()
|
|
|
|
);
|
|
|
|
|
|
|
|
// subtract the number of tasks we've dealt with (remembering we've
|
|
|
|
// already claimed one in the process of waking).
|
|
|
|
m_doomed.acquire (count - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pre-notify everyone. this lets any observers release the task before
|
|
|
|
// we wait for them at destruction time. otherwise we tend to find
|
|
|
|
// deadlocks amongst remaining tasks.
|
|
|
|
std::vector<task*> doomed;
|
|
|
|
for (auto &i: m_tasks.notified)
|
|
|
|
m_tasks.store.destroy (i);
|
|
|
|
for (auto &i: doomed)
|
2018-08-13 14:51:33 +10:00
|
|
|
i->done.notify_all ();
|
2018-03-22 14:59:03 +11:00
|
|
|
for (auto &i: doomed)
|
|
|
|
m_tasks.store.destroy (i);
|
|
|
|
}
|