From e58be26be828aa02d3595f9531e48275d44dfb2b Mon Sep 17 00:00:00 2001 From: darkf Date: Fri, 5 Aug 2016 09:00:40 -0700 Subject: [PATCH] Fix the construction of WorkContext Previously the worker thread was constructed before the queue was, leading to unfortunate race conditions. This fixes that, along with unrelated minor cleanup. --- rwgame/benchmarkstate.cpp | 1 + rwlib/source/job/WorkContext.hpp | 23 +++++++---------------- rwviewer/views/ModelViewer.cpp | 1 + 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/rwgame/benchmarkstate.cpp b/rwgame/benchmarkstate.cpp index 7023022c..c576877b 100644 --- a/rwgame/benchmarkstate.cpp +++ b/rwgame/benchmarkstate.cpp @@ -1,6 +1,7 @@ #include "benchmarkstate.hpp" #include "RWGame.hpp" #include +#include BenchmarkState::BenchmarkState(RWGame* game, const std::string& benchfile) : State(game) diff --git a/rwlib/source/job/WorkContext.hpp b/rwlib/source/job/WorkContext.hpp index c5f0756a..cc4d5815 100644 --- a/rwlib/source/job/WorkContext.hpp +++ b/rwlib/source/job/WorkContext.hpp @@ -1,6 +1,4 @@ #pragma once -#ifndef _LOADCONTEXT_HPP_ -#define _LOADCONTEXT_HPP_ #include #include @@ -8,7 +6,6 @@ #include #include #include -#include #include class WorkContext; @@ -58,9 +55,6 @@ public: virtual void complete() {} }; -// TODO: refactor everything to remove this. -class GameWorld; - /** * @brief A worker queue that runs work in the background. * @@ -69,13 +63,15 @@ class GameWorld; */ class WorkContext { - std::unique_ptr _worker; + std::mutex _inMutex; + std::mutex _outMutex; std::queue _workQueue; std::queue _completeQueue; - std::mutex _inMutex; - std::mutex _outMutex; + // Construct the worker last, so that it may use the queues + // immediately after initialization. + std::unique_ptr _worker; public: @@ -97,17 +93,12 @@ public: // Called by the worker thread - don't touch void workNext(); - const std::queue getWorkQueue() const { return _workQueue; } - const std::queue getCompleteQueue() const { return _completeQueue; } - bool isEmpty() { std::lock_guard guardIn( _inMutex ); - std::lock_guard guardOu( _outMutex ); + std::lock_guard guardOut( _outMutex ); - return (getWorkQueue().size() + getCompleteQueue().size()) == 0; + return (_workQueue.size() + _completeQueue.size()) == 0; } void update(); }; - -#endif diff --git a/rwviewer/views/ModelViewer.cpp b/rwviewer/views/ModelViewer.cpp index 90c9886d..7d26cdd7 100644 --- a/rwviewer/views/ModelViewer.cpp +++ b/rwviewer/views/ModelViewer.cpp @@ -5,6 +5,7 @@ #include #include #include "ViewerWidget.hpp" +#include ModelViewer::ModelViewer(ViewerWidget* viewer, QWidget* parent, Qt::WindowFlags f) : ViewerInterface(parent, f), viewing(nullptr), skeleton(nullptr)