mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-04-14 08:23:52 +00:00
fix: data race on collision query profiling counters
queryTimeMs and queryCallCount on WMORenderer and M2Renderer were plain mutable doubles/uint32s written by getFloorHeight (dispatched on async threads from CameraController) and read by the main thread. This is undefined behavior per C++ — thread sanitizer would flag it. Changed to std::atomic with relaxed ordering (adequate for diagnostics) and updated QueryTimer to use atomic fetch_add/compare_exchange.
This commit is contained in:
parent
3dd1128ecf
commit
bbb560f93c
3 changed files with 23 additions and 12 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
|
||||
namespace wowee {
|
||||
|
|
@ -42,19 +43,25 @@ struct ShadowParamsUBO {
|
|||
float foliageMotionDamp;
|
||||
};
|
||||
|
||||
// Timer utility for performance profiling queries
|
||||
// Timer utility for performance profiling queries.
|
||||
// Uses atomics because floor-height queries are dispatched on async threads
|
||||
// from CameraController while the main thread may read the counters.
|
||||
struct QueryTimer {
|
||||
double* totalMs = nullptr;
|
||||
uint32_t* callCount = nullptr;
|
||||
std::atomic<double>* totalMs = nullptr;
|
||||
std::atomic<uint32_t>* callCount = nullptr;
|
||||
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
||||
QueryTimer(double* total, uint32_t* calls) : totalMs(total), callCount(calls) {}
|
||||
QueryTimer(std::atomic<double>* total, std::atomic<uint32_t>* calls)
|
||||
: totalMs(total), callCount(calls) {}
|
||||
~QueryTimer() {
|
||||
if (callCount) {
|
||||
(*callCount)++;
|
||||
callCount->fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
if (totalMs) {
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
*totalMs += std::chrono::duration<double, std::milli>(end - start).count();
|
||||
double ms = std::chrono::duration<double, std::milli>(end - start).count();
|
||||
// Relaxed is fine for diagnostics — exact ordering doesn't matter.
|
||||
double old = totalMs->load(std::memory_order_relaxed);
|
||||
while (!totalMs->compare_exchange_weak(old, old + ms, std::memory_order_relaxed)) {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue