5#include <condition_variable>
24 std::condition_variable ready;
26 bool stopping =
false;
28 std::vector<std::thread> workers;
29 std::atomic<size_t> running{0}, peak{0};
30 void stop()
noexcept {
32 std::lock_guard lock(
mutex);
36 for (
auto& worker : workers)
37 if (worker.joinable()) worker.join();
44 for (uint32_t i = 0; i <
count; ++i)
45 workers.emplace_back([
this] {
50 std::unique_lock lock(mutex);
51 ready.wait(lock, [this] { return stopping || !jobs.empty(); });
52 if (jobs.empty()) return;
53 job = std::move(jobs.front());
57 const auto n = ++running;
59 while (p < n && !peak.compare_exchange_weak(p, n)) {
66 std::lock_guard lock(mutex);
86 const auto start = std::chrono::steady_clock::now();
88 if (!result.ok())
throw std::runtime_error(result.error()->message() +
"\nCompute source:\n" +
source);
91 std::move(result.value()),
92 std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() -
start).count()};
94 auto future = job.get_future();
97 std::lock_guard lock(
mutex);
98 if (stopping || jobs.size() >= 48)
throw std::runtime_error(
"ONNX compiler queue unavailable");
99 jobs.push_back(std::move(job));
109 std::unique_lock lock(
mutex);
110 ready.wait(lock, [
this] {
return jobs.empty() && inFlight == 0; });
const UnitySourceAsset & source
CompilerQueue public API.
size_t peakWorkers() const noexcept
Peak workers.
~CompilerQueue()
Releases CompilerQueue resources.
void waitIdle() noexcept
Waits idle.
std::future< CompiledProgram > enqueue(std::string source)
Enqueue.
void resetPeak() noexcept
Resets peak.
CompilerQueue(uint32_t count)
Constructs a CompilerQueue.
EVENGINE_API_WORLD Result< std::vector< uint32_t > > compileComputeSpirv(const std::string &source)
Compile GLSL compute source to owning SPIR-V without accessing Graphics or a GPU.
CompiledProgram public API.
std::vector< uint32_t > words
std::future< vkb::Instance > future