feat(host): vendor PyroWave + minimal Granite subset as crates/pyrowave-sys
Phase 0 of design/pyrowave-codec-plan.md — the opt-in wired-LAN ultra-low- latency codec. Vendored at upstream 509e4f88 (API 0.4.0, Granite 44362775, volk + vulkan-headers pins in PUNKTFUNK-VENDOR.txt), pruned to the 6.6 MB the standalone no-renderer build needs; scripts/vendor-pyrowave.sh reproduces the tree (a pin bump is protocol-affecting, plan §4.2). build.rs drives the wrapper CMakeLists (static archives incl. a static C-API lib upstream only ships shared) + bindgen over pyrowave.h; Linux and Windows only, empty stub elsewhere (Apple gets a native Metal port, §4.7). Offline-safe by construction: no network, no system lib, vendored Vulkan headers — same model as the opus dep (flatpak builder has no network). Phase-0 validation on .21 (RTX 5070 Ti, driver 610.43.03): - upstream pyrowave-c-test + interop test (incl. dmabuf/DRM-modifier Vulkan<->Vulkan) pass, from the pristine AND the pruned tree - GPU kernel times at ~1.6 bpp noise: encode/decode 0.090/0.042 ms @800p, 0.146/0.067 @1080p, 0.226/0.103 @1440p, 0.477/0.201 @4K — order of magnitude under NVENC's 1-2 ms retrieve, CBR lands within ~100 B of target - cargo test -p pyrowave-sys green (static link + API-version pin check) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
add_granite_internal_lib(granite-threading
|
||||
thread_group.cpp thread_group.hpp
|
||||
thread_latch.cpp thread_latch.hpp
|
||||
task_composer.cpp task_composer.hpp)
|
||||
|
||||
target_include_directories(granite-threading PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(granite-threading PUBLIC granite-util granite-application-global)
|
||||
@@ -0,0 +1,91 @@
|
||||
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "task_composer.hpp"
|
||||
|
||||
namespace Granite
|
||||
{
|
||||
TaskComposer::TaskComposer(ThreadGroup &group_)
|
||||
: group(group_)
|
||||
{
|
||||
}
|
||||
|
||||
void TaskComposer::set_incoming_task(TaskGroupHandle group_)
|
||||
{
|
||||
current = std::move(group_);
|
||||
}
|
||||
|
||||
TaskGroup &TaskComposer::begin_pipeline_stage()
|
||||
{
|
||||
auto new_group = group.create_task();
|
||||
auto new_deps = group.create_task();
|
||||
if (current)
|
||||
group.add_dependency(*new_deps, *current);
|
||||
if (next_stage_deps)
|
||||
group.add_dependency(*new_deps, *next_stage_deps);
|
||||
next_stage_deps.reset();
|
||||
group.add_dependency(*new_group, *new_deps);
|
||||
|
||||
current = std::move(new_group);
|
||||
incoming_deps = std::move(new_deps);
|
||||
return *current;
|
||||
}
|
||||
|
||||
TaskGroup &TaskComposer::get_group()
|
||||
{
|
||||
if (!current)
|
||||
return begin_pipeline_stage();
|
||||
else
|
||||
return *current;
|
||||
}
|
||||
|
||||
TaskGroupHandle TaskComposer::get_outgoing_task()
|
||||
{
|
||||
begin_pipeline_stage();
|
||||
auto ret = std::move(incoming_deps);
|
||||
incoming_deps = {};
|
||||
current = {};
|
||||
return ret;
|
||||
}
|
||||
|
||||
TaskGroupHandle TaskComposer::get_pipeline_stage_dependency()
|
||||
{
|
||||
return incoming_deps;
|
||||
}
|
||||
|
||||
TaskGroupHandle TaskComposer::get_deferred_enqueue_handle()
|
||||
{
|
||||
if (!next_stage_deps)
|
||||
next_stage_deps = group.create_task();
|
||||
return next_stage_deps;
|
||||
}
|
||||
|
||||
ThreadGroup &TaskComposer::get_thread_group()
|
||||
{
|
||||
return group;
|
||||
}
|
||||
|
||||
void TaskComposer::add_outgoing_dependency(TaskGroup &task)
|
||||
{
|
||||
group.add_dependency(task, *get_outgoing_task());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "thread_group.hpp"
|
||||
|
||||
// Designed to compose a series of pipelined tasks.
|
||||
|
||||
namespace Granite
|
||||
{
|
||||
class TaskComposer
|
||||
{
|
||||
public:
|
||||
explicit TaskComposer(ThreadGroup &group);
|
||||
void set_incoming_task(TaskGroupHandle group);
|
||||
TaskGroup &begin_pipeline_stage();
|
||||
TaskGroup &get_group();
|
||||
|
||||
// Returns a waitable handle that can be waited on.
|
||||
TaskGroupHandle get_outgoing_task();
|
||||
TaskGroupHandle get_pipeline_stage_dependency();
|
||||
ThreadGroup &get_thread_group();
|
||||
|
||||
// If this is called, the next pipeline stage will implicitly depend
|
||||
// on the task returned. This is useful if a pipeline stage will spawn tasks on its own,
|
||||
// which can only be known at the time of task execution.
|
||||
// As long as the enqueue handle is kept alive in child tasks,
|
||||
// the next pipeline stage will not begin.
|
||||
TaskGroupHandle get_deferred_enqueue_handle();
|
||||
|
||||
void add_outgoing_dependency(TaskGroup &task);
|
||||
|
||||
private:
|
||||
ThreadGroup &group;
|
||||
TaskGroupHandle current;
|
||||
TaskGroupHandle incoming_deps;
|
||||
TaskGroupHandle next_stage_deps;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,447 @@
|
||||
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "thread_group.hpp"
|
||||
#include <assert.h>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include "logging.hpp"
|
||||
#include "thread_id.hpp"
|
||||
#include "thread_priority.hpp"
|
||||
#include "string_helpers.hpp"
|
||||
#include "timeline_trace_file.hpp"
|
||||
#include "thread_name.hpp"
|
||||
#include "environment.hpp"
|
||||
|
||||
namespace Granite
|
||||
{
|
||||
namespace Internal
|
||||
{
|
||||
void TaskDeps::notify_dependees()
|
||||
{
|
||||
if (signal)
|
||||
signal->signal_increment();
|
||||
|
||||
for (auto &dep : pending)
|
||||
dep->dependency_satisfied();
|
||||
pending.clear();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> holder{cond_lock};
|
||||
done = true;
|
||||
cond.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
void TaskDeps::task_completed()
|
||||
{
|
||||
auto old_tasks = count.fetch_sub(1, std::memory_order_acq_rel);
|
||||
assert(old_tasks > 0);
|
||||
if (old_tasks == 1)
|
||||
notify_dependees();
|
||||
}
|
||||
|
||||
void TaskDeps::dependency_satisfied()
|
||||
{
|
||||
auto old_deps = dependency_count.fetch_sub(1, std::memory_order_acq_rel);
|
||||
assert(old_deps > 0);
|
||||
|
||||
if (old_deps == 1)
|
||||
{
|
||||
if (pending_tasks.empty())
|
||||
notify_dependees();
|
||||
else
|
||||
{
|
||||
group->move_to_ready_tasks(pending_tasks);
|
||||
pending_tasks.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TaskGroup::TaskGroup(ThreadGroup *group_)
|
||||
: group(group_)
|
||||
{
|
||||
}
|
||||
|
||||
void TaskGroup::flush()
|
||||
{
|
||||
if (flushed)
|
||||
throw std::logic_error("Cannot flush more than once.");
|
||||
|
||||
flushed = true;
|
||||
deps->dependency_satisfied();
|
||||
}
|
||||
|
||||
void TaskGroup::wait()
|
||||
{
|
||||
if (!flushed)
|
||||
flush();
|
||||
|
||||
std::unique_lock<std::mutex> holder{deps->cond_lock};
|
||||
deps->cond.wait(holder, [this]() {
|
||||
return deps->done;
|
||||
});
|
||||
}
|
||||
|
||||
bool TaskGroup::poll()
|
||||
{
|
||||
if (!flushed)
|
||||
flush();
|
||||
return deps->count.load(std::memory_order_acquire) == 0;
|
||||
}
|
||||
|
||||
TaskGroup::~TaskGroup()
|
||||
{
|
||||
if (!flushed)
|
||||
flush();
|
||||
}
|
||||
|
||||
void ThreadGroup::set_async_main_thread()
|
||||
{
|
||||
Util::set_current_thread_name("MainAsyncThread");
|
||||
Util::TimelineTraceFile::set_tid("main-async");
|
||||
// Seems reasonable to make sure main thread is making forward progress when it has something useful to do.
|
||||
Util::set_current_thread_priority(Util::ThreadPriority::High);
|
||||
}
|
||||
|
||||
static void set_main_thread_name()
|
||||
{
|
||||
Util::set_current_thread_name("MainThread");
|
||||
Util::TimelineTraceFile::set_tid("main");
|
||||
// Seems reasonable to make sure main thread is making forward progress when it has something useful to do.
|
||||
Util::set_current_thread_priority(Util::ThreadPriority::High);
|
||||
}
|
||||
|
||||
static void set_worker_thread_name_and_prio(unsigned index, TaskClass task_class)
|
||||
{
|
||||
auto name = Util::join(task_class == TaskClass::Foreground ? "FG-" : "BG-", index);
|
||||
Util::set_current_thread_name(name.c_str());
|
||||
Util::TimelineTraceFile::set_tid(name.c_str());
|
||||
Util::set_current_thread_priority(task_class == TaskClass::Foreground ?
|
||||
Util::ThreadPriority::Default : Util::ThreadPriority::Low);
|
||||
}
|
||||
|
||||
void ThreadGroup::refresh_global_timeline_trace_file()
|
||||
{
|
||||
Util::TimelineTraceFile::set_per_thread(timeline_trace_file.get());
|
||||
}
|
||||
|
||||
void ThreadGroup::set_thread_context()
|
||||
{
|
||||
refresh_global_timeline_trace_file();
|
||||
}
|
||||
|
||||
Util::TimelineTraceFile *ThreadGroup::get_timeline_trace_file()
|
||||
{
|
||||
return timeline_trace_file.get();
|
||||
}
|
||||
|
||||
void ThreadGroup::start(unsigned num_threads_foreground,
|
||||
unsigned num_threads_background,
|
||||
const std::function<void ()> &on_thread_begin)
|
||||
{
|
||||
if (active)
|
||||
throw std::logic_error("Cannot start a thread group which has already started.");
|
||||
|
||||
dead = false;
|
||||
active = true;
|
||||
|
||||
fg.thread_group.resize(num_threads_foreground);
|
||||
bg.thread_group.resize(num_threads_background);
|
||||
|
||||
#ifndef GRANITE_SHIPPING
|
||||
std::string path;
|
||||
if (Util::get_environment("GRANITE_TIMELINE_TRACE", path))
|
||||
{
|
||||
LOGI("Enabling JSON timeline tracing to %s.\n", path.c_str());
|
||||
timeline_trace_file = std::make_unique<Util::TimelineTraceFile>(path);
|
||||
}
|
||||
#endif
|
||||
|
||||
refresh_global_timeline_trace_file();
|
||||
set_main_thread_name();
|
||||
|
||||
unsigned self_index = 1;
|
||||
for (auto &t : fg.thread_group)
|
||||
{
|
||||
t = std::make_unique<std::thread>([this, on_thread_begin, self_index]() {
|
||||
refresh_global_timeline_trace_file();
|
||||
set_worker_thread_name_and_prio(self_index - 1, TaskClass::Foreground);
|
||||
if (on_thread_begin)
|
||||
on_thread_begin();
|
||||
thread_looper(self_index, TaskClass::Foreground);
|
||||
});
|
||||
self_index++;
|
||||
}
|
||||
|
||||
for (auto &t : bg.thread_group)
|
||||
{
|
||||
t = std::make_unique<std::thread>([this, on_thread_begin, self_index]() {
|
||||
refresh_global_timeline_trace_file();
|
||||
set_worker_thread_name_and_prio(self_index - 1, TaskClass::Background);
|
||||
if (on_thread_begin)
|
||||
on_thread_begin();
|
||||
thread_looper(self_index, TaskClass::Background);
|
||||
});
|
||||
self_index++;
|
||||
}
|
||||
}
|
||||
|
||||
void ThreadGroup::submit(TaskGroupHandle &group)
|
||||
{
|
||||
group->flush();
|
||||
group.reset();
|
||||
}
|
||||
|
||||
void ThreadGroup::add_dependency(TaskGroup &dependee, TaskGroup &dependency)
|
||||
{
|
||||
if (dependency.flushed)
|
||||
throw std::logic_error("Cannot wait for task group which has been flushed.");
|
||||
if (dependee.flushed)
|
||||
throw std::logic_error("Cannot add dependency to task group which has been flushed.");
|
||||
|
||||
dependency.deps->pending.push_back(dependee.deps);
|
||||
dependee.deps->dependency_count.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void ThreadGroup::move_to_ready_tasks(const Util::SmallVector<Internal::Task *> &list)
|
||||
{
|
||||
unsigned fg_task_count = 0;
|
||||
unsigned bg_task_count = 0;
|
||||
for (auto *t : list)
|
||||
{
|
||||
if (t->deps->task_class == TaskClass::Foreground)
|
||||
fg_task_count++;
|
||||
else
|
||||
bg_task_count++;
|
||||
}
|
||||
|
||||
total_tasks.fetch_add(list.size(), std::memory_order_relaxed);
|
||||
|
||||
if (fg_task_count)
|
||||
{
|
||||
std::lock_guard<std::mutex> holder{fg.cond_lock};
|
||||
|
||||
for (auto &t : list)
|
||||
fg.ready_tasks.push(t);
|
||||
|
||||
if (fg_task_count >= fg.thread_group.size())
|
||||
fg.cond.notify_all();
|
||||
else
|
||||
{
|
||||
for (unsigned i = 0; i < fg_task_count; i++)
|
||||
fg.cond.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
if (bg_task_count)
|
||||
{
|
||||
std::lock_guard<std::mutex> holder{bg.cond_lock};
|
||||
|
||||
for (auto &t : list)
|
||||
bg.ready_tasks.push(t);
|
||||
|
||||
if (bg_task_count >= bg.thread_group.size())
|
||||
bg.cond.notify_all();
|
||||
else
|
||||
{
|
||||
for (unsigned i = 0; i < bg_task_count; i++)
|
||||
bg.cond.notify_one();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::TaskGroupDeleter::operator()(TaskGroup *group)
|
||||
{
|
||||
group->group->free_task_group(group);
|
||||
}
|
||||
|
||||
void Internal::TaskDepsDeleter::operator()(Internal::TaskDeps *deps)
|
||||
{
|
||||
deps->group->free_task_deps(deps);
|
||||
}
|
||||
|
||||
void ThreadGroup::free_task_group(TaskGroup *group)
|
||||
{
|
||||
task_group_pool.free(group);
|
||||
}
|
||||
|
||||
void ThreadGroup::free_task_deps(Internal::TaskDeps *deps)
|
||||
{
|
||||
task_deps_pool.free(deps);
|
||||
}
|
||||
|
||||
void TaskSignal::signal_increment()
|
||||
{
|
||||
std::lock_guard<std::mutex> holder{lock};
|
||||
counter++;
|
||||
cond.notify_all();
|
||||
}
|
||||
|
||||
void TaskSignal::wait_until_at_least(uint64_t count)
|
||||
{
|
||||
std::unique_lock<std::mutex> holder{lock};
|
||||
cond.wait(holder, [&]() -> bool {
|
||||
return counter >= count;
|
||||
});
|
||||
}
|
||||
|
||||
uint64_t TaskSignal::get_count()
|
||||
{
|
||||
std::lock_guard<std::mutex> holder{lock};
|
||||
return counter;
|
||||
}
|
||||
|
||||
TaskGroupHandle ThreadGroup::create_task()
|
||||
{
|
||||
TaskGroupHandle group(task_group_pool.allocate(this));
|
||||
group->deps = Internal::TaskDepsHandle(task_deps_pool.allocate(this));
|
||||
group->deps->count.store(0, std::memory_order_relaxed);
|
||||
return group;
|
||||
}
|
||||
|
||||
void TaskGroup::set_fence_counter_signal(TaskSignal *signal)
|
||||
{
|
||||
deps->signal = signal;
|
||||
}
|
||||
|
||||
ThreadGroup *TaskGroup::get_thread_group() const
|
||||
{
|
||||
return group;
|
||||
}
|
||||
|
||||
void TaskGroup::set_desc(const char *desc)
|
||||
{
|
||||
snprintf(deps->desc, sizeof(deps->desc), "%s", desc);
|
||||
}
|
||||
|
||||
void TaskGroup::set_task_class(TaskClass task_class)
|
||||
{
|
||||
deps->task_class = task_class;
|
||||
}
|
||||
|
||||
void ThreadGroup::wait_idle()
|
||||
{
|
||||
std::unique_lock<std::mutex> holder{wait_cond_lock};
|
||||
wait_cond.wait(holder, [&]() {
|
||||
return total_tasks.load(std::memory_order_relaxed) == completed_tasks.load(std::memory_order_relaxed);
|
||||
});
|
||||
}
|
||||
|
||||
bool ThreadGroup::is_idle()
|
||||
{
|
||||
return total_tasks.load(std::memory_order_acquire) == completed_tasks.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
void ThreadGroup::thread_looper(unsigned index, TaskClass task_class)
|
||||
{
|
||||
Util::register_thread_index(index);
|
||||
auto &ctx = task_class == TaskClass::Foreground ? fg : bg;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
Internal::Task *task = nullptr;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> holder{ctx.cond_lock};
|
||||
ctx.cond.wait(holder, [&]() {
|
||||
return dead || !ctx.ready_tasks.empty();
|
||||
});
|
||||
|
||||
if (dead && ctx.ready_tasks.empty())
|
||||
break;
|
||||
|
||||
task = ctx.ready_tasks.front();
|
||||
ctx.ready_tasks.pop();
|
||||
}
|
||||
|
||||
if (task->callable)
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT_FILE(timeline_trace_file.get(), task->deps->desc);
|
||||
task->callable.call();
|
||||
}
|
||||
|
||||
task->deps->task_completed();
|
||||
task_pool.free(task);
|
||||
|
||||
{
|
||||
auto completed = completed_tasks.fetch_add(1, std::memory_order_relaxed) + 1;
|
||||
//LOGI("Task completed (%u / %u)!\n", completed, total_tasks.load(memory_order_relaxed));
|
||||
|
||||
if (completed == total_tasks.load(std::memory_order_relaxed))
|
||||
{
|
||||
std::lock_guard<std::mutex> holder{wait_cond_lock};
|
||||
wait_cond.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ThreadGroup::ThreadGroup()
|
||||
{
|
||||
total_tasks.store(0);
|
||||
completed_tasks.store(0);
|
||||
}
|
||||
|
||||
ThreadGroup::~ThreadGroup()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
void ThreadGroup::stop()
|
||||
{
|
||||
if (!active)
|
||||
return;
|
||||
|
||||
wait_idle();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> holder_fg{fg.cond_lock};
|
||||
std::lock_guard<std::mutex> holder_bg{bg.cond_lock};
|
||||
dead = true;
|
||||
fg.cond.notify_all();
|
||||
bg.cond.notify_all();
|
||||
}
|
||||
|
||||
for (auto &t : fg.thread_group)
|
||||
{
|
||||
if (t && t->joinable())
|
||||
{
|
||||
t->join();
|
||||
t.reset();
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &t : bg.thread_group)
|
||||
{
|
||||
if (t && t->joinable())
|
||||
{
|
||||
t->join();
|
||||
t.reset();
|
||||
}
|
||||
}
|
||||
|
||||
active = false;
|
||||
dead = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include "object_pool.hpp"
|
||||
#include "variant.hpp"
|
||||
#include "intrusive.hpp"
|
||||
#include "timeline_trace_file.hpp"
|
||||
#include "global_managers.hpp"
|
||||
#include "small_vector.hpp"
|
||||
#include "small_callable.hpp"
|
||||
|
||||
namespace Granite
|
||||
{
|
||||
class ThreadGroup;
|
||||
|
||||
struct TaskSignal
|
||||
{
|
||||
std::condition_variable cond;
|
||||
std::mutex lock;
|
||||
uint64_t counter = 0;
|
||||
|
||||
void signal_increment();
|
||||
void wait_until_at_least(uint64_t count);
|
||||
uint64_t get_count();
|
||||
};
|
||||
|
||||
enum class TaskClass : uint8_t
|
||||
{
|
||||
Foreground,
|
||||
Background
|
||||
};
|
||||
|
||||
struct TaskGroup;
|
||||
namespace Internal
|
||||
{
|
||||
struct TaskDeps;
|
||||
struct Task;
|
||||
|
||||
struct TaskDepsDeleter
|
||||
{
|
||||
void operator()(TaskDeps *deps);
|
||||
};
|
||||
|
||||
struct TaskGroupDeleter
|
||||
{
|
||||
void operator()(TaskGroup *group);
|
||||
};
|
||||
|
||||
struct TaskDeps : Util::IntrusivePtrEnabled<TaskDeps, TaskDepsDeleter, Util::MultiThreadCounter>
|
||||
{
|
||||
explicit TaskDeps(ThreadGroup *group_)
|
||||
: group(group_)
|
||||
{
|
||||
count.store(0, std::memory_order_relaxed);
|
||||
// One implicit dependency is the flush() happening.
|
||||
dependency_count.store(1, std::memory_order_relaxed);
|
||||
desc[0] = '\0';
|
||||
}
|
||||
|
||||
ThreadGroup *group;
|
||||
Util::SmallVector<Util::IntrusivePtr<TaskDeps>> pending;
|
||||
std::atomic_uint count;
|
||||
|
||||
Util::SmallVector<Task *> pending_tasks;
|
||||
TaskSignal *signal = nullptr;
|
||||
std::atomic_uint dependency_count;
|
||||
|
||||
void task_completed();
|
||||
void dependency_satisfied();
|
||||
void notify_dependees();
|
||||
|
||||
std::condition_variable cond;
|
||||
std::mutex cond_lock;
|
||||
bool done = false;
|
||||
TaskClass task_class = TaskClass::Foreground;
|
||||
|
||||
char desc[64];
|
||||
};
|
||||
using TaskDepsHandle = Util::IntrusivePtr<TaskDeps>;
|
||||
|
||||
struct Task
|
||||
{
|
||||
template <typename Func>
|
||||
Task(TaskDepsHandle deps_, Func&& func)
|
||||
: callable(std::forward<Func>(func)), deps(std::move(deps_))
|
||||
{
|
||||
}
|
||||
|
||||
Task() = default;
|
||||
|
||||
Util::SmallCallable<void (), 64 - sizeof(TaskDepsHandle), alignof(TaskDepsHandle)> callable;
|
||||
TaskDepsHandle deps;
|
||||
};
|
||||
|
||||
static_assert(sizeof(Task) == 64, "sizeof(Task) is unexpected.");
|
||||
}
|
||||
|
||||
struct TaskGroup : Util::IntrusivePtrEnabled<TaskGroup, Internal::TaskGroupDeleter, Util::MultiThreadCounter>
|
||||
{
|
||||
explicit TaskGroup(ThreadGroup *group);
|
||||
~TaskGroup();
|
||||
void flush();
|
||||
void wait();
|
||||
bool poll();
|
||||
|
||||
ThreadGroup *group;
|
||||
Internal::TaskDepsHandle deps;
|
||||
|
||||
template <typename Func>
|
||||
void enqueue_task(Func&& func);
|
||||
|
||||
void set_fence_counter_signal(TaskSignal *signal);
|
||||
ThreadGroup *get_thread_group() const;
|
||||
|
||||
void set_desc(const char *desc);
|
||||
void set_task_class(TaskClass task_class);
|
||||
|
||||
unsigned id = 0;
|
||||
bool flushed = false;
|
||||
};
|
||||
|
||||
using TaskGroupHandle = Util::IntrusivePtr<TaskGroup>;
|
||||
|
||||
class ThreadGroup final : public ThreadGroupInterface
|
||||
{
|
||||
public:
|
||||
ThreadGroup();
|
||||
~ThreadGroup();
|
||||
ThreadGroup(ThreadGroup &&) = delete;
|
||||
void operator=(ThreadGroup &&) = delete;
|
||||
|
||||
void start(unsigned num_threads_foreground,
|
||||
unsigned num_threads_background,
|
||||
const std::function<void ()> &on_thread_begin) override;
|
||||
|
||||
unsigned get_num_threads() const
|
||||
{
|
||||
return unsigned(fg.thread_group.size() + bg.thread_group.size());
|
||||
}
|
||||
|
||||
void stop();
|
||||
|
||||
template <typename Func>
|
||||
void enqueue_task(TaskGroup &group, Func&& func);
|
||||
template <typename Func>
|
||||
TaskGroupHandle create_task(Func&& func);
|
||||
TaskGroupHandle create_task();
|
||||
|
||||
void move_to_ready_tasks(const Util::SmallVector<Internal::Task *> &list);
|
||||
|
||||
void add_dependency(TaskGroup &dependee, TaskGroup &dependency);
|
||||
|
||||
void free_task_group(TaskGroup *group);
|
||||
void free_task_deps(Internal::TaskDeps *deps);
|
||||
|
||||
void submit(TaskGroupHandle &group);
|
||||
void wait_idle();
|
||||
bool is_idle();
|
||||
|
||||
Util::TimelineTraceFile *get_timeline_trace_file();
|
||||
void refresh_global_timeline_trace_file();
|
||||
|
||||
static void set_async_main_thread();
|
||||
|
||||
private:
|
||||
Util::ThreadSafeObjectPool<Internal::Task> task_pool;
|
||||
Util::ThreadSafeObjectPool<TaskGroup> task_group_pool;
|
||||
Util::ThreadSafeObjectPool<Internal::TaskDeps> task_deps_pool;
|
||||
|
||||
struct
|
||||
{
|
||||
std::vector<std::unique_ptr<std::thread>> thread_group;
|
||||
std::queue<Internal::Task *> ready_tasks;
|
||||
std::mutex cond_lock;
|
||||
std::condition_variable cond;
|
||||
} fg, bg;
|
||||
|
||||
void thread_looper(unsigned self_index, TaskClass task_class);
|
||||
|
||||
bool active = false;
|
||||
bool dead = false;
|
||||
|
||||
std::condition_variable wait_cond;
|
||||
std::mutex wait_cond_lock;
|
||||
std::atomic_uint total_tasks;
|
||||
std::atomic_uint completed_tasks;
|
||||
|
||||
std::unique_ptr<Util::TimelineTraceFile> timeline_trace_file;
|
||||
void set_thread_context() override;
|
||||
};
|
||||
|
||||
template <typename Func>
|
||||
TaskGroupHandle ThreadGroup::create_task(Func&& func)
|
||||
{
|
||||
TaskGroupHandle group(task_group_pool.allocate(this));
|
||||
group->deps = Internal::TaskDepsHandle(task_deps_pool.allocate(this));
|
||||
group->deps->pending_tasks.push_back(task_pool.allocate(group->deps, std::forward<Func>(func)));
|
||||
group->deps->count.store(1, std::memory_order_relaxed);
|
||||
return group;
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
void ThreadGroup::enqueue_task(TaskGroup &group, Func&& func)
|
||||
{
|
||||
if (group.flushed)
|
||||
throw std::logic_error("Cannot enqueue work to a flushed task group.");
|
||||
group.deps->pending_tasks.push_back(task_pool.allocate(group.deps, std::forward<Func>(func)));
|
||||
group.deps->count.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
void TaskGroup::enqueue_task(Func&& func)
|
||||
{
|
||||
group->enqueue_task(*this, std::forward<Func>(func));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "thread_latch.hpp"
|
||||
#include <assert.h>
|
||||
|
||||
namespace Granite
|
||||
{
|
||||
void ThreadLatch::set_latch()
|
||||
{
|
||||
std::lock_guard<std::mutex> holder{lock};
|
||||
assert(!latch);
|
||||
latch = true;
|
||||
cond.notify_one();
|
||||
}
|
||||
|
||||
void ThreadLatch::clear_latch()
|
||||
{
|
||||
std::lock_guard<std::mutex> holder{lock};
|
||||
assert(latch);
|
||||
latch = false;
|
||||
cond.notify_one();
|
||||
}
|
||||
|
||||
bool ThreadLatch::wait_latch_set()
|
||||
{
|
||||
std::unique_lock<std::mutex> holder{lock};
|
||||
cond.wait(holder, [this]() {
|
||||
return latch || dead;
|
||||
});
|
||||
return !dead;
|
||||
}
|
||||
|
||||
bool ThreadLatch::wait_latch_cleared()
|
||||
{
|
||||
std::unique_lock<std::mutex> holder{lock};
|
||||
cond.wait(holder, [this]() {
|
||||
return !latch || dead;
|
||||
});
|
||||
return !dead;
|
||||
}
|
||||
|
||||
void ThreadLatch::kill_latch()
|
||||
{
|
||||
std::lock_guard<std::mutex> holder{lock};
|
||||
dead = true;
|
||||
cond.notify_one();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
namespace Granite
|
||||
{
|
||||
class ThreadLatch
|
||||
{
|
||||
public:
|
||||
void set_latch();
|
||||
void clear_latch();
|
||||
bool wait_latch_cleared();
|
||||
bool wait_latch_set();
|
||||
void kill_latch();
|
||||
|
||||
private:
|
||||
std::condition_variable cond;
|
||||
std::mutex lock;
|
||||
bool latch = false;
|
||||
bool dead = false;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user