From 5c2d1bd151151d75b17ea51904080174131754e5 Mon Sep 17 00:00:00 2001 From: Sheil Kumar Date: Tue, 30 Jul 2024 15:59:01 -0700 Subject: [PATCH] change initialization vars to enum, and handle the failed case to avoid reinitializing logger. --- .../core/platform/windows/logging/etw_sink.cc | 27 +++++++++---------- .../core/platform/windows/logging/etw_sink.h | 5 ++-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/onnxruntime/core/platform/windows/logging/etw_sink.cc b/onnxruntime/core/platform/windows/logging/etw_sink.cc index c9d908949df4c..cc12514bd3c30 100644 --- a/onnxruntime/core/platform/windows/logging/etw_sink.cc +++ b/onnxruntime/core/platform/windows/logging/etw_sink.cc @@ -137,13 +137,14 @@ void NTAPI EtwRegistrationManager::ORT_TL_EtwEnableCallback( EtwRegistrationManager::~EtwRegistrationManager() { std::lock_guard lock(callbacks_mutex_); callbacks_.clear(); - if (initialized_ || initializing_) { + if (initialization_status_ == InitializationStatus::Intialized || + initialization_status_ == InitializationStatus::Initializing) { std::lock_guard init_lock(init_mutex_); - assert(!initializing_); - if (initialized_) { - ::TraceLoggingUnregister(etw_provider_handle); - initialized_ = false; - } + assert(initialization_status_ != InitializationStatus::Initializing); + if (initialization_status_ == InitializationStatus::Intialized) { + ::TraceLoggingUnregister(etw_provider_handle); + initialization_status_ = InitializationStatus::NotInitialized; + } } } @@ -151,29 +152,27 @@ EtwRegistrationManager::EtwRegistrationManager() { } void EtwRegistrationManager::LazyInitialize() try { - if (!initialized_ && !initializing_) { + if (initialization_status_ == InitializationStatus::NoitIntialized) { std::lock_guard lock(init_mutex_); - if (!initialized_ && !initializing_) { // Double-check locking pattern - initializing_ = true; + if (initialization_status_ == InitializationStatus::NotInitialized) { // Double-check locking pattern + initialization_status_ == InitializationStatus::Initializing; etw_status_ = ::TraceLoggingRegisterEx(etw_provider_handle, ORT_TL_EtwEnableCallback, nullptr); if (FAILED(etw_status_)) { ORT_THROW("ETW registration failed. Logging will be broken: " + std::to_string(etw_status_)); } - initialized_ = true; - initializing_ = false; + initialization_status_ == InitializationStatus::Initialized; } } } catch (...) { - initialized_ = false; - initializing_ = false; + initialization_status_ == InitializationStatus::Failed; throw; } void EtwRegistrationManager::InvokeCallbacks(LPCGUID SourceId, ULONG IsEnabled, UCHAR Level, ULONGLONG MatchAnyKeyword, ULONGLONG MatchAllKeyword, PEVENT_FILTER_DESCRIPTOR FilterData, PVOID CallbackContext) { - if (!initialized_) { + if (initialization_status_ != InitializationStatus::Initialized) { // Drop messages until manager is fully initialized. return; } diff --git a/onnxruntime/core/platform/windows/logging/etw_sink.h b/onnxruntime/core/platform/windows/logging/etw_sink.h index 19af3e565e095..acd9f6b0892f8 100644 --- a/onnxruntime/core/platform/windows/logging/etw_sink.h +++ b/onnxruntime/core/platform/windows/logging/etw_sink.h @@ -47,6 +47,8 @@ class EtwSink : public ISink { }; class EtwRegistrationManager { + enum class InitializationStatus { NotInitialized, Initializing, Initialized, Failed}; + public: using EtwInternalCallback = std::function