renderer: implemented function to wait for workers in the main thread
The workers run in separate threads meaning if any significant change is made (e.g. parameters changing, or even closing the program), these workers may still be mid-render. This is particularly problematic when closing since the nodes a worker is rendering may be deleted mid-render. Render backends now have a function that pauses the main thread (but starts a second event loop so the UI isn't frozen) until the worker threads are all finished. This way, massive changes can be made safely without race conditions.
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
#include <QDateTime>
|
||||
#include <QThread>
|
||||
|
||||
#include "core.h"
|
||||
#include "window/mainwindow/mainwindow.h"
|
||||
|
||||
RenderBackend::RenderBackend(QObject *parent) :
|
||||
QObject(parent),
|
||||
compiled_(false),
|
||||
@@ -12,6 +15,8 @@ RenderBackend::RenderBackend(QObject *parent) :
|
||||
recompile_queued_(false),
|
||||
input_update_queued_(false)
|
||||
{
|
||||
// FIXME: Don't create in CLI mode
|
||||
cancel_dialog_ = new RenderCancelDialog(Core::instance()->main_window());
|
||||
}
|
||||
|
||||
bool RenderBackend::Init()
|
||||
@@ -30,6 +35,8 @@ bool RenderBackend::Init()
|
||||
thread->start(QThread::LowPriority);
|
||||
}
|
||||
|
||||
cancel_dialog_->SetWorkerCount(threads_.size());
|
||||
|
||||
started_ = InitInternal();
|
||||
|
||||
// Connects workers and moves them to their respective threads
|
||||
@@ -50,6 +57,8 @@ void RenderBackend::Close()
|
||||
|
||||
started_ = false;
|
||||
|
||||
CancelQueue();
|
||||
|
||||
Decompile();
|
||||
|
||||
CloseInternal();
|
||||
@@ -81,6 +90,8 @@ const QString &RenderBackend::GetError() const
|
||||
void RenderBackend::SetViewerNode(ViewerOutput *viewer_node)
|
||||
{
|
||||
if (viewer_node_ != nullptr) {
|
||||
CancelQueue();
|
||||
|
||||
DisconnectViewer(viewer_node_);
|
||||
|
||||
Decompile();
|
||||
@@ -297,6 +308,7 @@ void RenderBackend::CacheNext()
|
||||
render_job_info_.insert(cache_frame, job_time);
|
||||
|
||||
SetWorkerBusyState(worker, true);
|
||||
cancel_dialog_->WorkerStarted();
|
||||
|
||||
QMetaObject::invokeMethod(worker,
|
||||
"Render",
|
||||
@@ -312,6 +324,20 @@ ViewerOutput *RenderBackend::viewer_node() const
|
||||
return copied_viewer_node_;
|
||||
}
|
||||
|
||||
void RenderBackend::CancelQueue()
|
||||
{
|
||||
cache_queue_.clear();
|
||||
|
||||
int busy = 0;
|
||||
for (int i=0;i<processor_busy_state_.size();i++) {
|
||||
if (processor_busy_state_.at(i))
|
||||
busy++;
|
||||
}
|
||||
qDebug() << this << "has" << busy << "busy workers";
|
||||
|
||||
cancel_dialog_->RunIfWorkersAreBusy();
|
||||
}
|
||||
|
||||
bool RenderBackend::ViewerIsConnected() const
|
||||
{
|
||||
return viewer_node_ != nullptr;
|
||||
@@ -375,6 +401,9 @@ void RenderBackend::InitWorkers()
|
||||
// Connect to it
|
||||
ConnectWorkerToThis(processor);
|
||||
|
||||
// Connect cancel dialog to it
|
||||
connect(processor, &RenderWorker::CompletedCache, cancel_dialog_, &RenderCancelDialog::WorkerDone, Qt::QueuedConnection);
|
||||
|
||||
// Finally, we can move it to its own thread
|
||||
processor->moveToThread(thread);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user