错误处理统一模式

This commit is contained in:
2026-08-16 12:31:28 +08:00
parent 5e57528ecc
commit 00a3c54820
6 changed files with 93 additions and 12 deletions
@@ -1,4 +1,5 @@
#include "Gpu_Completion_Service.h"
#include "renderive/error/Error_Policy.hpp"
#include <algorithm>
#include <stdexcept>
#include <utility>
@@ -27,13 +28,13 @@ Gpu_Completion_Service::Reservation::Reservation(Reservation&& other) noexcept
void Gpu_Completion_Service::Reservation::watch(VkDevice device,
VkFence fence) {
if (!pending_ || device == VK_NULL_HANDLE || fence == VK_NULL_HANDLE)
throw std::logic_error("GPU completion reservation or fence is invalid");
renderive::error::unexpected<std::logic_error>("GPU completion reservation or fence is invalid");
auto pending = std::exchange(pending_, {});
auto* const service = pending->service;
{
std::lock_guard lock(pending->mutex);
if (pending->status != Pending_Fence::Status::reserved)
throw std::logic_error("GPU completion reservation is not reserved");
renderive::error::unexpected<std::logic_error>("GPU completion reservation is not reserved");
pending->device = device;
pending->fence = fence;
// This timestamp is part of correctness, not only observability: it
@@ -92,7 +93,7 @@ void Gpu_Completion_Service::release_slot() noexcept {
Gpu_Completion_Service::Prepare_Result Gpu_Completion_Service::prepare(
Completion completion, bool observe) {
if (!completion)
throw std::invalid_argument("GPU completion callback is empty");
renderive::error::unexpected<std::invalid_argument>("GPU completion callback is empty");
if (stopping_.load(std::memory_order_acquire))
return {{}, Error::stopping};
auto pending = std::make_shared<Pending_Fence>();
@@ -106,7 +107,7 @@ Gpu_Completion_Service::Prepare_Result Gpu_Completion_Service::prepare(
}
if (!pending_.try_push(pending)) {
release_slot();
throw std::logic_error("GPU completion admission invariant violated");
renderive::error::unexpected<std::logic_error>("GPU completion admission invariant violated");
}
wake();
return {Reservation(std::move(pending)), Error::none};
+5 -4
View File
@@ -1,4 +1,5 @@
#include "Render_Domain.h"
#include "renderive/error/Error_Policy.hpp"
#include <mutex>
#include <unordered_map>
namespace renderive::render_3d::detail {
@@ -69,7 +70,7 @@ void Render_Domain::request_stop() noexcept {
return;
slots_.acquire();
if (!tasks_.try_push(std::unique_ptr<Task>{}))
std::terminate();
renderive::error::fast_fail("render domain stop marker queue rejected admitted task");
}
void Render_Domain::update_peak(std::atomic_size_t& peak, std::size_t value) noexcept {
std::size_t current = peak.load(std::memory_order_relaxed);
@@ -94,7 +95,7 @@ void Render_Domain::release_admission() noexcept {
}
Render_Domain::Prepare_Result Render_Domain::prepare(std::function<void()> function) {
if (!function)
throw std::invalid_argument("render domain task is empty");
renderive::error::unexpected<std::invalid_argument>("render domain task is empty");
if (stopping_.load(std::memory_order_acquire))
return {{}, Error::stopping};
acquire_admission();
@@ -117,11 +118,11 @@ Render_Domain::Error Render_Domain::post(std::function<void()> function) {
}
Render_Domain::Error Render_Domain::post(Prepared_Task task) {
if (!task.task_ || task.domain_.get() != this)
throw std::logic_error("render domain prepared task is invalid");
renderive::error::unexpected<std::logic_error>("render domain prepared task is invalid");
if (stopping_.load(std::memory_order_acquire))
return Error::stopping;
if (!tasks_.try_push(std::move(task.task_)))
throw std::logic_error("render domain admission invariant violated");
renderive::error::unexpected<std::logic_error>("render domain admission invariant violated");
const std::size_t queued = queued_.fetch_add(1, std::memory_order_relaxed) + 1;
update_peak(peak_queued_, queued);
task.domain_.reset();