2019-07-10 14:35:10 -07:00
|
|
|
#pragma once
|
|
|
|
|
2019-07-10 15:42:13 -07:00
|
|
|
#include <condition_variable>
|
2019-07-10 14:35:10 -07:00
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
namespace vcpkg
|
|
|
|
{
|
|
|
|
template<class Action, class ThreadLocalData>
|
2019-07-10 15:42:13 -07:00
|
|
|
struct WorkQueue;
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
namespace detail
|
|
|
|
{
|
2019-07-10 15:42:13 -07:00
|
|
|
// for SFINAE purposes, keep out of the class
|
2019-07-26 16:32:33 -07:00
|
|
|
// also this sfinae is so weird because Backwards Compatibility with VS2015
|
|
|
|
template<class Action,
|
|
|
|
class ThreadLocalData,
|
|
|
|
class = decltype(std::declval<Action>()(std::declval<ThreadLocalData&>(),
|
|
|
|
std::declval<const WorkQueue<Action, ThreadLocalData>&>()))>
|
|
|
|
void call_moved_action(Action& action,
|
2019-07-11 18:16:10 -07:00
|
|
|
const WorkQueue<Action, ThreadLocalData>& work_queue,
|
2019-07-26 16:32:33 -07:00
|
|
|
ThreadLocalData& tld)
|
2019-07-10 14:35:10 -07:00
|
|
|
{
|
|
|
|
std::move(action)(tld, work_queue);
|
|
|
|
}
|
|
|
|
|
2019-07-26 16:32:33 -07:00
|
|
|
template<class Action,
|
|
|
|
class ThreadLocalData,
|
|
|
|
class = decltype(std::declval<Action>()(std::declval<ThreadLocalData&>())),
|
|
|
|
class = void>
|
|
|
|
void call_moved_action(Action& action, const WorkQueue<Action, ThreadLocalData>&, ThreadLocalData& tld)
|
2019-07-10 14:35:10 -07:00
|
|
|
{
|
|
|
|
std::move(action)(tld);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
template<class Action, class ThreadLocalData>
|
|
|
|
struct WorkQueue
|
|
|
|
{
|
|
|
|
template<class F>
|
2019-07-26 16:32:33 -07:00
|
|
|
WorkQueue(LineInfo li, std::uint16_t num_threads, const F& tld_init) noexcept
|
2019-07-11 18:16:10 -07:00
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
m_line_info = li;
|
2019-07-10 14:35:10 -07:00
|
|
|
|
2019-07-15 18:51:03 -07:00
|
|
|
set_unjoined_workers(num_threads);
|
2019-07-10 14:35:10 -07:00
|
|
|
m_threads.reserve(num_threads);
|
2019-07-11 18:16:10 -07:00
|
|
|
for (std::size_t i = 0; i < num_threads; ++i)
|
|
|
|
{
|
2019-07-10 15:42:13 -07:00
|
|
|
m_threads.push_back(std::thread(Worker{this, tld_init()}));
|
2019-07-10 14:35:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkQueue(WorkQueue const&) = delete;
|
|
|
|
WorkQueue(WorkQueue&&) = delete;
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
~WorkQueue()
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
auto lck = std::unique_lock<std::mutex>(m_mutex);
|
2019-07-11 18:16:10 -07:00
|
|
|
if (!is_joined(m_state))
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
Checks::exit_with_message(m_line_info, "Failed to call join() on a WorkQueue that was destroyed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// should only be called once; anything else is an error
|
2019-07-11 18:16:10 -07:00
|
|
|
void run(LineInfo li)
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
// this should _not_ be locked before `run()` is called; however, we
|
|
|
|
// want to terminate if someone screws up, rather than cause UB
|
|
|
|
auto lck = std::unique_lock<std::mutex>(m_mutex);
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
if (m_state != State::BeforeRun)
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
Checks::exit_with_message(li, "Attempted to run() twice");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_state = State::Running;
|
|
|
|
}
|
2019-07-10 14:35:10 -07:00
|
|
|
|
|
|
|
// runs all remaining tasks, and blocks on their finishing
|
|
|
|
// if this is called in an existing task, _will block forever_
|
|
|
|
// DO NOT DO THAT
|
|
|
|
// thread-unsafe
|
2019-07-11 18:16:10 -07:00
|
|
|
void join(LineInfo li)
|
|
|
|
{
|
2019-07-10 14:35:10 -07:00
|
|
|
{
|
|
|
|
auto lck = std::unique_lock<std::mutex>(m_mutex);
|
2019-07-11 18:16:10 -07:00
|
|
|
if (is_joined(m_state))
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
Checks::exit_with_message(li, "Attempted to call join() more than once");
|
2019-07-11 18:16:10 -07:00
|
|
|
}
|
|
|
|
else if (m_state == State::Terminated)
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
m_state = State::TerminatedJoined;
|
2019-07-11 18:16:10 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
m_state = State::Joined;
|
2019-07-10 14:35:10 -07:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:16:10 -07:00
|
|
|
|
2019-07-15 18:51:03 -07:00
|
|
|
while (unjoined_workers())
|
2019-07-11 18:16:10 -07:00
|
|
|
{
|
2019-07-15 18:51:03 -07:00
|
|
|
if (!running_workers())
|
2019-07-11 18:16:10 -07:00
|
|
|
{
|
2019-07-15 18:51:03 -07:00
|
|
|
m_cv.notify_one();
|
2019-07-18 16:40:52 -07:00
|
|
|
}
|
2019-07-11 18:16:10 -07:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:51:03 -07:00
|
|
|
// wait for all threads to join
|
2019-07-11 18:16:10 -07:00
|
|
|
for (auto& thrd : m_threads)
|
|
|
|
{
|
2019-07-10 14:35:10 -07:00
|
|
|
thrd.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// useful in the case of errors
|
|
|
|
// doesn't stop any existing running tasks
|
|
|
|
// returns immediately, so that one can call this in a task
|
2019-07-11 18:16:10 -07:00
|
|
|
void terminate() const
|
|
|
|
{
|
2019-07-10 14:35:10 -07:00
|
|
|
{
|
|
|
|
auto lck = std::unique_lock<std::mutex>(m_mutex);
|
2019-07-11 18:16:10 -07:00
|
|
|
if (is_joined(m_state))
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
m_state = State::TerminatedJoined;
|
2019-07-11 18:16:10 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
m_state = State::Terminated;
|
|
|
|
}
|
2019-07-10 14:35:10 -07:00
|
|
|
}
|
|
|
|
m_cv.notify_all();
|
|
|
|
}
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
void enqueue_action(Action a) const
|
|
|
|
{
|
2019-07-10 14:35:10 -07:00
|
|
|
{
|
|
|
|
auto lck = std::unique_lock<std::mutex>(m_mutex);
|
|
|
|
m_actions.push_back(std::move(a));
|
2019-07-11 15:01:29 -07:00
|
|
|
|
|
|
|
if (m_state == State::BeforeRun) return;
|
2019-07-10 14:35:10 -07:00
|
|
|
}
|
|
|
|
m_cv.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-07-11 18:16:10 -07:00
|
|
|
struct Worker
|
|
|
|
{
|
2019-07-10 14:35:10 -07:00
|
|
|
const WorkQueue* work_queue;
|
|
|
|
ThreadLocalData tld;
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
void operator()()
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
// unlocked when waiting, or when in the action
|
2019-07-10 15:42:13 -07:00
|
|
|
// locked otherwise
|
|
|
|
auto lck = std::unique_lock<std::mutex>(work_queue->m_mutex);
|
2019-07-11 15:01:29 -07:00
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
work_queue->m_cv.wait(lck, [&] { return work_queue->m_state != State::BeforeRun; });
|
2019-07-11 15:01:29 -07:00
|
|
|
|
2019-07-15 18:51:03 -07:00
|
|
|
work_queue->increment_running_workers();
|
2019-07-11 18:16:10 -07:00
|
|
|
for (;;)
|
|
|
|
{
|
2019-07-10 14:35:10 -07:00
|
|
|
const auto state = work_queue->m_state;
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
if (is_terminated(state))
|
|
|
|
{
|
|
|
|
break;
|
2019-07-10 14:35:10 -07:00
|
|
|
}
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
if (work_queue->m_actions.empty())
|
|
|
|
{
|
2019-07-15 18:51:03 -07:00
|
|
|
if (state == State::Running || work_queue->running_workers() > 1)
|
2019-07-11 18:16:10 -07:00
|
|
|
{
|
2019-07-15 18:51:03 -07:00
|
|
|
work_queue->decrement_running_workers();
|
2019-07-10 14:35:10 -07:00
|
|
|
work_queue->m_cv.wait(lck);
|
2019-07-15 18:51:03 -07:00
|
|
|
work_queue->increment_running_workers();
|
2019-07-10 14:35:10 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:51:03 -07:00
|
|
|
// the queue is joining, and we are the only worker running
|
2019-07-10 14:35:10 -07:00
|
|
|
// no more work!
|
2019-07-11 18:16:10 -07:00
|
|
|
break;
|
2019-07-10 14:35:10 -07:00
|
|
|
}
|
|
|
|
|
2019-07-10 15:42:13 -07:00
|
|
|
Action action = std::move(work_queue->m_actions.back());
|
|
|
|
work_queue->m_actions.pop_back();
|
2019-07-10 14:35:10 -07:00
|
|
|
|
2019-07-10 15:42:13 -07:00
|
|
|
lck.unlock();
|
2019-07-15 18:51:03 -07:00
|
|
|
work_queue->m_cv.notify_one();
|
2019-07-11 10:53:32 -07:00
|
|
|
detail::call_moved_action(action, *work_queue, tld);
|
2019-07-10 15:42:13 -07:00
|
|
|
lck.lock();
|
2019-07-10 14:35:10 -07:00
|
|
|
}
|
2019-07-11 18:16:10 -07:00
|
|
|
|
2019-07-15 18:51:03 -07:00
|
|
|
work_queue->decrement_running_workers();
|
|
|
|
work_queue->decrement_unjoined_workers();
|
2019-07-10 14:35:10 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
enum class State : std::int16_t
|
|
|
|
{
|
2019-07-11 15:01:29 -07:00
|
|
|
// can only exist upon construction
|
|
|
|
BeforeRun = -1,
|
|
|
|
|
2019-07-10 14:35:10 -07:00
|
|
|
Running,
|
2019-07-11 15:01:29 -07:00
|
|
|
Joined,
|
2019-07-10 14:35:10 -07:00
|
|
|
Terminated,
|
2019-07-11 15:01:29 -07:00
|
|
|
TerminatedJoined,
|
2019-07-10 14:35:10 -07:00
|
|
|
};
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
static bool is_terminated(State st) { return st == State::Terminated || st == State::TerminatedJoined; }
|
2019-07-11 15:01:29 -07:00
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
static bool is_joined(State st) { return st == State::Joined || st == State::TerminatedJoined; }
|
2019-07-11 15:01:29 -07:00
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
mutable std::mutex m_mutex{};
|
|
|
|
// these are all under m_mutex
|
|
|
|
mutable State m_state = State::BeforeRun;
|
|
|
|
mutable std::vector<Action> m_actions{};
|
|
|
|
mutable std::condition_variable m_cv{};
|
2019-07-10 14:35:10 -07:00
|
|
|
|
2019-07-15 18:51:03 -07:00
|
|
|
mutable std::atomic<std::uint32_t> m_workers;
|
|
|
|
// = unjoined_workers << 16 | running_workers
|
|
|
|
|
|
|
|
void set_unjoined_workers(std::uint16_t threads) { m_workers = std::uint32_t(threads) << 16; }
|
|
|
|
void decrement_unjoined_workers() const { m_workers -= 1 << 16; }
|
|
|
|
|
|
|
|
std::uint16_t unjoined_workers() const { return std::uint16_t(m_workers >> 16); }
|
|
|
|
|
|
|
|
void increment_running_workers() const { ++m_workers; }
|
|
|
|
void decrement_running_workers() const { --m_workers; }
|
|
|
|
std::uint16_t running_workers() const { return std::uint16_t(m_workers); }
|
|
|
|
|
2019-07-11 18:16:10 -07:00
|
|
|
std::vector<std::thread> m_threads{};
|
2019-07-11 15:01:29 -07:00
|
|
|
LineInfo m_line_info;
|
2019-07-10 14:35:10 -07:00
|
|
|
};
|
|
|
|
}
|