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 <cwallez@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Quyen Le <lehoangquyen@chromium.org>
This commit is contained in:
Le Hoang Quyen 2023-04-06 08:08:00 +00:00 committed by Dawn LUCI CQ
parent 655c6c9b40
commit c173a0ea88
1 changed files with 11 additions and 4 deletions

View File

@ -152,6 +152,7 @@
#ifndef SRC_DAWN_PLATFORM_TRACING_TRACEEVENT_H_
#define SRC_DAWN_PLATFORM_TRACING_TRACEEVENT_H_
#include <atomic>
#include <string>
#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<const unsigned char*> 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.