diff --git a/include/boo/BooObject.hpp b/include/boo/BooObject.hpp index 048c3c4..fd818bf 100644 --- a/include/boo/BooObject.hpp +++ b/include/boo/BooObject.hpp @@ -2,6 +2,7 @@ #define BOOOBJECT_HPP #include +#include namespace boo { @@ -9,13 +10,26 @@ namespace boo class IObj { std::atomic_int m_refCount = {0}; +protected: + std::recursive_mutex* m_mutex = nullptr; public: virtual ~IObj() = default; void increment() { m_refCount++; } void decrement() { if (m_refCount.fetch_sub(1) == 1) - delete this; + { + if (std::recursive_mutex* mutex = m_mutex) + { + mutex->lock(); + delete this; + mutex->unlock(); + } + else + { + delete this; + } + } } }; diff --git a/lib/graphicsdev/Common.hpp b/lib/graphicsdev/Common.hpp index 95100b7..8664f4c 100644 --- a/lib/graphicsdev/Common.hpp +++ b/lib/graphicsdev/Common.hpp @@ -21,7 +21,7 @@ struct GraphicsDataNode; /** Inherited by data factory implementations to track the head data and pool nodes */ struct GraphicsDataFactoryHead { - std::mutex m_dataMutex; + std::recursive_mutex m_dataMutex; BaseGraphicsData* m_dataHead = nullptr; BaseGraphicsPool* m_poolHead = nullptr; }; @@ -68,7 +68,8 @@ struct BaseGraphicsData : IObj explicit BaseGraphicsData(GraphicsDataFactoryHead& head) : m_head(head) { - std::lock_guard lk(m_head.m_dataMutex); + IObj::m_mutex = &m_head.m_dataMutex; + std::lock_guard lk(m_head.m_dataMutex); m_next = head.m_dataHead; if (m_next) m_next->m_prev = this; @@ -76,7 +77,6 @@ struct BaseGraphicsData : IObj } ~BaseGraphicsData() { - std::lock_guard lk(m_head.m_dataMutex); if (m_prev) { if (m_next) @@ -131,7 +131,8 @@ struct BaseGraphicsPool : IObj explicit BaseGraphicsPool(GraphicsDataFactoryHead& head) : m_head(head) { - std::lock_guard lk(m_head.m_dataMutex); + IObj::m_mutex = &m_head.m_dataMutex; + std::lock_guard lk(m_head.m_dataMutex); m_next = head.m_poolHead; if (m_next) m_next->m_prev = this; @@ -139,7 +140,6 @@ struct BaseGraphicsPool : IObj } ~BaseGraphicsPool() { - std::lock_guard lk(m_head.m_dataMutex); if (m_prev) { if (m_next) @@ -176,7 +176,8 @@ struct GraphicsDataNode : NodeCls explicit GraphicsDataNode(const ObjToken& data) : m_data(data) { - std::lock_guard lk(m_data->m_head.m_dataMutex); + IObj::m_mutex = &m_data->m_head.m_dataMutex; + std::lock_guard lk(m_data->m_head.m_dataMutex); m_next = data->template getHead(); if (m_next) m_next->m_prev = this; @@ -184,7 +185,6 @@ struct GraphicsDataNode : NodeCls } ~GraphicsDataNode() { - std::lock_guard lk(m_data->m_head.m_dataMutex); if (m_prev) { if (m_next) diff --git a/lib/graphicsdev/D3D11.cpp b/lib/graphicsdev/D3D11.cpp index 4b5d40e..bb75609 100644 --- a/lib/graphicsdev/D3D11.cpp +++ b/lib/graphicsdev/D3D11.cpp @@ -1509,7 +1509,7 @@ void D3D11CommandQueue::ProcessDynamicLoads(ID3D11DeviceContext* ctx) { D3D11DataFactory* gfxF = static_cast(m_parent->getDataFactory()); std::unique_lock lk(m_dynamicLock); - std::unique_lock datalk(gfxF->m_dataMutex); + std::unique_lock datalk(gfxF->m_dataMutex); if (gfxF->m_dataHead) { diff --git a/lib/graphicsdev/D3D12.cpp b/lib/graphicsdev/D3D12.cpp index c364e4d..cae5814 100644 --- a/lib/graphicsdev/D3D12.cpp +++ b/lib/graphicsdev/D3D12.cpp @@ -2078,7 +2078,7 @@ void D3D12CommandQueue::execute() /* Stage dynamic uploads */ D3D12DataFactory* gfxF = static_cast(m_parent->getDataFactory()); - std::unique_lock datalk(gfxF->m_dataMutex); + std::unique_lock datalk(gfxF->m_dataMutex); if (gfxF->m_dataHead) { for (BaseGraphicsData& d : *gfxF->m_dataHead) diff --git a/lib/graphicsdev/GL.cpp b/lib/graphicsdev/GL.cpp index 00f3f42..18b16e0 100644 --- a/lib/graphicsdev/GL.cpp +++ b/lib/graphicsdev/GL.cpp @@ -1457,7 +1457,7 @@ struct GLCommandQueue : IGraphicsCommandQueue /* Update dynamic data here */ GLDataFactoryImpl* gfxF = static_cast(m_parent->getDataFactory()); - std::unique_lock datalk(gfxF->m_dataMutex); + std::unique_lock datalk(gfxF->m_dataMutex); if (gfxF->m_dataHead) { for (BaseGraphicsData& d : *gfxF->m_dataHead) diff --git a/lib/graphicsdev/Metal.mm b/lib/graphicsdev/Metal.mm index 83a0d26..2031053 100644 --- a/lib/graphicsdev/Metal.mm +++ b/lib/graphicsdev/Metal.mm @@ -1231,7 +1231,7 @@ struct MetalCommandQueue : IGraphicsCommandQueue /* Update dynamic data here */ MetalDataFactoryImpl* gfxF = static_cast(m_parent->getDataFactory()); - std::unique_lock datalk(gfxF->m_dataMutex); + std::unique_lock datalk(gfxF->m_dataMutex); if (gfxF->m_dataHead) { for (BaseGraphicsData& d : *gfxF->m_dataHead) diff --git a/lib/graphicsdev/Vulkan.cpp b/lib/graphicsdev/Vulkan.cpp index 2d7fd8a..8ea6670 100644 --- a/lib/graphicsdev/Vulkan.cpp +++ b/lib/graphicsdev/Vulkan.cpp @@ -3558,7 +3558,7 @@ void VulkanCommandQueue::execute() /* Stage dynamic uploads */ VulkanDataFactoryImpl* gfxF = static_cast(m_parent->getDataFactory()); - std::unique_lock datalk(gfxF->m_dataMutex); + std::unique_lock datalk(gfxF->m_dataMutex); if (gfxF->m_dataHead) { for (BaseGraphicsData& d : *gfxF->m_dataHead) diff --git a/lib/mac/ApplicationCocoa.mm b/lib/mac/ApplicationCocoa.mm index f6ed750..96c7f6c 100644 --- a/lib/mac/ApplicationCocoa.mm +++ b/lib/mac/ApplicationCocoa.mm @@ -120,7 +120,8 @@ public: /* Spawn client thread */ m_clientThread = std::thread([&]() { - logvisor::RegisterThreadName("Boo Client Thread"); + std::string thrName = getFriendlyName() + " Client Thread"; + logvisor::RegisterThreadName(thrName.c_str()); /* Run app */ m_clientReturn = m_callback.appMain(this); @@ -205,7 +206,8 @@ int ApplicationRun(IApplication::EPlatformType platform, const std::vector& args, bool singleInstance) { - logvisor::RegisterThreadName("Boo Main Thread"); + std::string thrName = friendlyName + " Main Thread"; + logvisor::RegisterThreadName(thrName.c_str()); @autoreleasepool { if (!APP)