From 75847c67d38b535ee48588c8a262626123351e16 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 24 Dec 2016 15:35:30 -1000 Subject: [PATCH] Better thread contention handling for BlenderConnection --- hecl/blender/hecl/hmdl/HMDLMesh.py | 3 ++- .../hecl/Blender/BlenderConnection.hpp | 27 ++++++++++++------- hecl/lib/ClientProcess.cpp | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/hecl/blender/hecl/hmdl/HMDLMesh.py b/hecl/blender/hecl/hmdl/HMDLMesh.py index cb15ca065..95c3fe2c4 100644 --- a/hecl/blender/hecl/hmdl/HMDLMesh.py +++ b/hecl/blender/hecl/hmdl/HMDLMesh.py @@ -81,7 +81,8 @@ class VertPool: writebuf(struct.pack('I', len(vert_groups))) for vgrp in vert_groups: - writebuf((vgrp.name + '\n').encode()) + writebuf(struct.pack('I', len(vgrp.name))) + writebuf(vgrp.name.encode()) writebuf(struct.pack('I', len(self.skin))) for s in sorted(self.skin.items(), key=operator.itemgetter(1)): diff --git a/hecl/include/hecl/Blender/BlenderConnection.hpp b/hecl/include/hecl/Blender/BlenderConnection.hpp index 0e90ead54..ff4840449 100644 --- a/hecl/include/hecl/Blender/BlenderConnection.hpp +++ b/hecl/include/hecl/Blender/BlenderConnection.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "hecl/hecl.hpp" #include "hecl/HMDLMeta.hpp" @@ -49,7 +50,7 @@ public: Frame }; private: - bool m_lock = false; + std::atomic_bool m_lock; #if _WIN32 HANDLE m_blenderProc = 0; PROCESS_INFORMATION m_pinfo = {}; @@ -81,8 +82,8 @@ public: BlenderConnection(const BlenderConnection&)=delete; BlenderConnection& operator=(const BlenderConnection&)=delete; - BlenderConnection(BlenderConnection&&)=default; - BlenderConnection& operator=(BlenderConnection&&)=default; + BlenderConnection(BlenderConnection&&)=delete; + BlenderConnection& operator=(BlenderConnection&&)=delete; bool hasSLERP() const {return m_hasSlerp;} @@ -122,7 +123,6 @@ public: m_deleteOnError(deleteOnError), m_sbuf(*this, deleteOnError) { - m_parent->m_lock = true; m_parent->_writeStr("PYBEGIN"); char readBuf[16]; m_parent->_readStr(readBuf, 16); @@ -296,7 +296,8 @@ public: }; PyOutStream beginPythonOut(bool deleteOnError=false) { - if (m_lock) + bool expect = false; + if (!m_lock.compare_exchange_strong(expect, true)) BlenderLog.report(logvisor::Fatal, "lock already held for BlenderConnection::beginPythonOut()"); return PyOutStream(this, deleteOnError); } @@ -308,7 +309,6 @@ public: DataStream(BlenderConnection* parent) : m_parent(parent) { - m_parent->m_lock = true; m_parent->_writeStr("DATABEGIN"); char readBuf[16]; m_parent->_readStr(readBuf, 16); @@ -767,7 +767,8 @@ public: }; DataStream beginData() { - if (m_lock) + bool expect = false; + if (!m_lock.compare_exchange_strong(expect, true)) BlenderLog.report(logvisor::Fatal, "lock already held for BlenderConnection::beginDataIn()"); return DataStream(this); } @@ -786,12 +787,12 @@ public: class BlenderToken { - std::experimental::optional m_conn; + std::unique_ptr m_conn; public: BlenderConnection& getBlenderConnection() { if (!m_conn) - m_conn.emplace(hecl::VerbosityLevel); + m_conn = std::make_unique(hecl::VerbosityLevel); return *m_conn; } void shutdown() @@ -799,10 +800,16 @@ public: if (m_conn) { m_conn->quitBlender(); - m_conn = std::experimental::nullopt; + m_conn.reset(); BlenderLog.report(logvisor::Info, "BlenderConnection Shutdown Successful"); } } + + BlenderToken() = default; + BlenderToken(const BlenderToken&)=delete; + BlenderToken& operator=(const BlenderToken&)=delete; + BlenderToken(BlenderToken&&)=default; + BlenderToken& operator=(BlenderToken&&)=default; }; class HMDLBuffers diff --git a/hecl/lib/ClientProcess.cpp b/hecl/lib/ClientProcess.cpp index 6cf64b24f..2add2f011 100644 --- a/hecl/lib/ClientProcess.cpp +++ b/hecl/lib/ClientProcess.cpp @@ -91,7 +91,7 @@ void ClientProcess::Worker::proc() ClientProcess::ClientProcess(int verbosityLevel) : m_verbosity(verbosityLevel) { -#ifdef NDEBUG +#if defined(NDEBUG) int cpuCount = GetCPUCount(); #else constexpr int cpuCount = 1;