From c173a0ea8844a752b665208430e7d7b19d0ce40d Mon Sep 17 00:00:00 2001 From: Le Hoang Quyen Date: Thu, 6 Apr 2023 08:08:00 +0000 Subject: [PATCH] Fix TRACE_EVENT wasn't thread safe. TRACE_EVENT uses INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO and INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO reads and writes to a static variable on multiple threads. Fix by using std::atomic for the static variable. Bug: dawn:1700 Change-Id: I914bb73352e400f0adeafb64518d61099276270d Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/123680 Reviewed-by: Corentin Wallez Kokoro: Kokoro Reviewed-by: Austin Eng Commit-Queue: Quyen Le --- src/dawn/platform/tracing/TraceEvent.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/dawn/platform/tracing/TraceEvent.h b/src/dawn/platform/tracing/TraceEvent.h index 76cbc56037..e207b96025 100644 --- a/src/dawn/platform/tracing/TraceEvent.h +++ b/src/dawn/platform/tracing/TraceEvent.h @@ -152,6 +152,7 @@ #ifndef SRC_DAWN_PLATFORM_TRACING_TRACEEVENT_H_ #define SRC_DAWN_PLATFORM_TRACING_TRACEEVENT_H_ +#include #include #include "dawn/platform/tracing/EventTracer.h" @@ -670,10 +671,16 @@ #define INTERNALTRACEEVENTUID(name_prefix) INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) // Implementation detail: internal macro to create static category. -#define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(platform, category) \ - static const unsigned char* INTERNALTRACEEVENTUID(catstatic) = 0; \ - if (!INTERNALTRACEEVENTUID(catstatic)) \ - INTERNALTRACEEVENTUID(catstatic) = TRACE_EVENT_API_GET_CATEGORY_ENABLED(platform, category); +#define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(platform, category) \ + static std::atomic INTERNALTRACEEVENTUID(atomicCatstatic)(nullptr); \ + const unsigned char* INTERNALTRACEEVENTUID(catstatic) = \ + INTERNALTRACEEVENTUID(atomicCatstatic).load(std::memory_order_acquire); \ + if (!INTERNALTRACEEVENTUID(catstatic)) { \ + INTERNALTRACEEVENTUID(catstatic) = \ + TRACE_EVENT_API_GET_CATEGORY_ENABLED(platform, category); \ + INTERNALTRACEEVENTUID(atomicCatstatic) \ + .store(INTERNALTRACEEVENTUID(catstatic), std::memory_order_release); \ + } // Implementation detail: internal macro to create static category and add // event if the category is enabled.