2017-07-03 17:05:01 +10:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
2018-03-15 15:21:05 +11:00
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
2017-07-03 17:05:01 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "./queue.hpp"
|
|
|
|
|
2018-03-15 15:21:05 +11:00
|
|
|
#include "../raii.hpp"
|
|
|
|
|
2017-07-03 17:05:01 +10:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using util::job::queue;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
queue::queue ():
|
|
|
|
queue (std::thread::hardware_concurrency () ?: 1)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
queue::queue (unsigned thread_count):
|
2018-03-15 15:21:05 +11:00
|
|
|
m_tasks {
|
|
|
|
{}, util::pool<task> {4096}
|
|
|
|
},
|
|
|
|
m_pending (0),
|
2017-07-03 17:05:01 +10:00
|
|
|
m_threads (thread_count)
|
|
|
|
{
|
|
|
|
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-15 15:21:05 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
queue::loop ()
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
m_pending.acquire ();
|
|
|
|
if (m_stopping)
|
|
|
|
return;
|
|
|
|
|
|
|
|
util::scoped_counter running_count (m_running);
|
|
|
|
|
|
|
|
CHECK (!m_tasks.pending->empty ());
|
|
|
|
|
|
|
|
auto todo = [this] () {
|
|
|
|
auto obj = m_tasks.pending.acquire ();
|
|
|
|
auto res = obj->front ();
|
|
|
|
obj->pop_front ();
|
|
|
|
return res;
|
|
|
|
} ();
|
|
|
|
|
|
|
|
util::scoped_function cleanup ([&, this] () {
|
|
|
|
m_tasks.store.destroy (todo);
|
|
|
|
});
|
|
|
|
|
|
|
|
todo->function (*todo);
|
|
|
|
}
|
|
|
|
}
|