修复webserver死锁
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
#include "common/Gallery_Scene_Interface.h"
|
||||
#include "render_2D/Gallery_Scene2D.h"
|
||||
#include "render_3D/Gallery_Scene3D.h"
|
||||
#include <renderive/scheduling/Scheduler.hpp>
|
||||
#include <trantor/net/EventLoopThread.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <algorithm>
|
||||
@@ -204,24 +203,31 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
scene->set_client_metrics(performance);
|
||||
return true;
|
||||
}
|
||||
[[nodiscard]] std::unique_lock<std::mutex> acquire_foreground_lock() {
|
||||
return std::unique_lock<std::mutex>(mutex);
|
||||
[[nodiscard]] std::unique_lock<std::mutex> acquire_scene_lock() {
|
||||
return std::unique_lock<std::mutex>(scene_mutex);
|
||||
}
|
||||
void disarm_automatic_render() {
|
||||
void disarm_automatic_render_locked() {
|
||||
++automatic_timer_revision;
|
||||
if (automatic_timer == trantor::InvalidTimerId)
|
||||
return;
|
||||
automatic_render_loop()->invalidateTimer(automatic_timer);
|
||||
automatic_timer = trantor::InvalidTimerId;
|
||||
}
|
||||
void arm_automatic_render_locked() {
|
||||
void disarm_automatic_render() {
|
||||
std::lock_guard lock(state_mutex);
|
||||
disarm_automatic_render_locked();
|
||||
}
|
||||
void arm_automatic_render() {
|
||||
if (!automatic_low_latency)
|
||||
return;
|
||||
disarm_automatic_render();
|
||||
if (!scene || !scene->can_render_automatically() || render_task_pending)
|
||||
const bool can_render = scene && scene->can_render_automatically();
|
||||
const auto interval = can_render
|
||||
? std::chrono::nanoseconds(std::max<std::uint64_t>(1, scene->kernel_refresh_interval_ns()))
|
||||
: std::chrono::nanoseconds::zero();
|
||||
std::lock_guard lock(state_mutex);
|
||||
disarm_automatic_render_locked();
|
||||
if (!can_render || automatic_render_running)
|
||||
return;
|
||||
const auto interval = std::chrono::nanoseconds(
|
||||
std::max<std::uint64_t>(1, scene->kernel_refresh_interval_ns()));
|
||||
const auto deadline = last_render_started ? *last_render_started + interval : Clock::now();
|
||||
const auto delay = std::max(Clock::duration::zero(), deadline - Clock::now());
|
||||
const std::uint64_t revision = automatic_timer_revision;
|
||||
@@ -235,46 +241,46 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
void automatic_render_due(std::uint64_t revision) {
|
||||
std::uint64_t generation{};
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
std::lock_guard lock(state_mutex);
|
||||
if (revision != automatic_timer_revision)
|
||||
return;
|
||||
automatic_timer = trantor::InvalidTimerId;
|
||||
++automatic_timer_revision;
|
||||
if (!automatic_low_latency || !scene || !scene->can_render_automatically() || render_task_pending)
|
||||
if (!automatic_low_latency || automatic_render_running)
|
||||
return;
|
||||
render_task_pending = true;
|
||||
automatic_render_running = true;
|
||||
generation = scene_generation;
|
||||
}
|
||||
auto self = shared_from_this();
|
||||
try {
|
||||
renderive::scheduling::enqueue_task([self = std::move(self), generation] {
|
||||
self->run_automatic_render(generation);
|
||||
});
|
||||
} catch (...) {
|
||||
std::lock_guard lock(mutex);
|
||||
render_task_pending = false;
|
||||
automatic_render_exception = ::renderive::error::capture(
|
||||
"arming automatic gallery render", std::current_exception());
|
||||
disarm_automatic_render();
|
||||
}
|
||||
run_automatic_render(generation);
|
||||
}
|
||||
void run_automatic_render(std::uint64_t generation) {
|
||||
const auto started = Clock::now();
|
||||
try {
|
||||
std::lock_guard lock(mutex);
|
||||
if (generation == scene_generation && scene && scene->can_render_automatically()) {
|
||||
const auto error = scene->render_latest_frame();
|
||||
if (error == Gallery_Render_Result::none)
|
||||
last_render_started = started;
|
||||
auto scene_lock = acquire_scene_lock();
|
||||
bool current_generation{};
|
||||
{
|
||||
std::lock_guard lock(state_mutex);
|
||||
current_generation = generation == scene_generation;
|
||||
}
|
||||
render_task_pending = false;
|
||||
arm_automatic_render_locked();
|
||||
if (current_generation && scene && scene->can_render_automatically()) {
|
||||
const auto error = scene->render_latest_frame();
|
||||
if (error == Gallery_Render_Result::none) {
|
||||
std::lock_guard lock(state_mutex);
|
||||
if (generation == scene_generation)
|
||||
last_render_started = started;
|
||||
}
|
||||
}
|
||||
{
|
||||
std::lock_guard lock(state_mutex);
|
||||
automatic_render_running = false;
|
||||
}
|
||||
arm_automatic_render();
|
||||
} catch (...) {
|
||||
std::lock_guard lock(mutex);
|
||||
render_task_pending = false;
|
||||
std::lock_guard lock(state_mutex);
|
||||
automatic_render_running = false;
|
||||
automatic_render_exception = ::renderive::error::capture(
|
||||
"running automatic gallery render", std::current_exception());
|
||||
disarm_automatic_render();
|
||||
disarm_automatic_render_locked();
|
||||
}
|
||||
}
|
||||
static bool affects_render_schedule(const Web_Event& event) {
|
||||
@@ -297,11 +303,12 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
event);
|
||||
}
|
||||
std::unique_ptr<Gallery_Scene_Interface> scene;
|
||||
std::mutex mutex;
|
||||
std::mutex scene_mutex;
|
||||
std::mutex state_mutex;
|
||||
std::optional<Clock::time_point> last_render_started;
|
||||
std::uint64_t scene_generation{};
|
||||
bool automatic_low_latency{};
|
||||
bool render_task_pending{};
|
||||
bool automatic_render_running{};
|
||||
std::exception_ptr automatic_render_exception;
|
||||
std::uint64_t session_id{};
|
||||
trantor::TimerId automatic_timer{trantor::InvalidTimerId};
|
||||
@@ -317,9 +324,12 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
Gallery_Protocol::error_json("未知的 gallery case 或 frame_mode", "case")
|
||||
};
|
||||
scene = make_gallery_scene(session_id, open->case_id, open->frame_mode, automatic_low_latency);
|
||||
++scene_generation;
|
||||
last_render_started.reset();
|
||||
arm_automatic_render_locked();
|
||||
{
|
||||
std::lock_guard lock(state_mutex);
|
||||
++scene_generation;
|
||||
last_render_started.reset();
|
||||
}
|
||||
arm_automatic_render();
|
||||
return Web_Response{
|
||||
Web_Response_Type::Json,
|
||||
Gallery_Protocol::case_json_from_controls(scene->case_id(), scene->controls().dump(),
|
||||
@@ -335,7 +345,7 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
};
|
||||
if (request.kind == Gallery_Request_Kind::Observe) {
|
||||
if (update_client_metrics(request.message))
|
||||
arm_automatic_render_locked();
|
||||
arm_automatic_render();
|
||||
return Web_Response{
|
||||
Web_Response_Type::Json,
|
||||
Gallery_Protocol::observer_json(
|
||||
@@ -344,7 +354,7 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
}
|
||||
if (request.kind == Gallery_Request_Kind::Refresh) {
|
||||
if (update_client_metrics(request.message))
|
||||
arm_automatic_render_locked();
|
||||
arm_automatic_render();
|
||||
return Web_Response{
|
||||
Web_Response_Type::Json,
|
||||
Gallery_Protocol::case_json_from_controls(
|
||||
@@ -398,9 +408,12 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
const std::string id = scene->case_id();
|
||||
const auto mode = scene->frame_mode();
|
||||
scene = make_gallery_scene(session_id, id, mode, automatic_low_latency);
|
||||
++scene_generation;
|
||||
last_render_started.reset();
|
||||
arm_automatic_render_locked();
|
||||
{
|
||||
std::lock_guard lock(state_mutex);
|
||||
++scene_generation;
|
||||
last_render_started.reset();
|
||||
}
|
||||
arm_automatic_render();
|
||||
return Web_Response{
|
||||
Web_Response_Type::Json,
|
||||
Gallery_Protocol::case_json_from_controls(id, scene->controls().dump(),
|
||||
@@ -428,7 +441,7 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
const auto request_started = std::chrono::steady_clock::now();
|
||||
std::optional<std::string> pixels;
|
||||
{
|
||||
auto lock = acquire_foreground_lock();
|
||||
auto lock = acquire_scene_lock();
|
||||
if (!scene)
|
||||
return std::nullopt;
|
||||
const auto encode_started = std::chrono::steady_clock::now();
|
||||
@@ -446,7 +459,7 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
{
|
||||
std::exception_ptr exception;
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
std::lock_guard lock(state_mutex);
|
||||
exception = std::exchange(automatic_render_exception, {});
|
||||
}
|
||||
if (exception)
|
||||
@@ -458,7 +471,7 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
const bool reschedule = affects_render_schedule(event);
|
||||
std::optional<Web_Response> response;
|
||||
{
|
||||
auto lock = acquire_foreground_lock();
|
||||
auto lock = acquire_scene_lock();
|
||||
response = std::visit(
|
||||
[this](const auto& value) -> std::optional<Web_Response> {
|
||||
using T = std::decay_t<decltype(value)>;
|
||||
@@ -484,7 +497,7 @@ struct Gallery_Plot_Session::Impl : std::enable_shared_from_this<Gallery_Plot_Se
|
||||
},
|
||||
event);
|
||||
if (reschedule)
|
||||
arm_automatic_render_locked();
|
||||
arm_automatic_render();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user