From 52188192b99ee4f3e7848ac1690bc1ccae338c56 Mon Sep 17 00:00:00 2001 From: wwuoneway Date: Fri, 16 Oct 2026 13:30:57 -0500 Subject: [PATCH 1/2] Enable ROOT thread safety during FORM initialization --- form/root_storage/CMakeLists.txt | 1 + form/root_storage/root_thread_safety.cpp | 17 +++++++++++++++++ form/root_storage/root_thread_safety.hpp | 13 +++++++++++++ form/storage/factories.cpp | 8 ++++++++ form/storage/factories.hpp | 2 ++ form/storage/storage_reader.cpp | 1 + form/storage/storage_writer.cpp | 1 + 7 files changed, 43 insertions(+) create mode 100644 form/root_storage/root_thread_safety.cpp create mode 100644 form/root_storage/root_thread_safety.hpp diff --git a/form/root_storage/CMakeLists.txt b/form/root_storage/CMakeLists.txt index 4cc5e46f9..b1473e51c 100644 --- a/form/root_storage/CMakeLists.txt +++ b/form/root_storage/CMakeLists.txt @@ -7,6 +7,7 @@ set(FORM_ROOT_STORAGE_SOURCES) list( APPEND FORM_ROOT_STORAGE_SOURCES root_tfile.cpp + root_thread_safety.cpp root_ttree_write_container.cpp root_tbranch_read_container.cpp root_tbranch_write_container.cpp diff --git a/form/root_storage/root_thread_safety.cpp b/form/root_storage/root_thread_safety.cpp new file mode 100644 index 000000000..f0decc803 --- /dev/null +++ b/form/root_storage/root_thread_safety.cpp @@ -0,0 +1,17 @@ +// Copyright (C) 2025 ... + +#include "root_thread_safety.hpp" + +#include + +#include + +namespace form::detail::experimental { + + void ensure_root_thread_safety() + { + static std::once_flag enabled; + std::call_once(enabled, [] { ROOT::EnableThreadSafety(); }); + } + +} // namespace form::detail::experimental diff --git a/form/root_storage/root_thread_safety.hpp b/form/root_storage/root_thread_safety.hpp new file mode 100644 index 000000000..b092c918b --- /dev/null +++ b/form/root_storage/root_thread_safety.hpp @@ -0,0 +1,13 @@ +// Copyright (C) 2025 ... + +#ifndef FORM_ROOT_STORAGE_ROOT_THREAD_SAFETY_HPP +#define FORM_ROOT_STORAGE_ROOT_THREAD_SAFETY_HPP + +namespace form::detail::experimental { + + /// Enable ROOT thread safety. + void ensure_root_thread_safety(); + +} // namespace form::detail::experimental + +#endif // FORM_ROOT_STORAGE_ROOT_THREAD_SAFETY_HPP diff --git a/form/storage/factories.cpp b/form/storage/factories.cpp index 8276567c1..33a223974 100644 --- a/form/storage/factories.cpp +++ b/form/storage/factories.cpp @@ -16,6 +16,7 @@ #include "root_storage/root_tbranch_read_container.hpp" #include "root_storage/root_tbranch_write_container.hpp" #include "root_storage/root_tfile.hpp" +#include "root_storage/root_thread_safety.hpp" #include "root_storage/root_ttree_write_container.hpp" #endif @@ -31,6 +32,13 @@ namespace form::detail::experimental { using major = form::technology::major; + void ensure_backend_thread_safety() + { +#ifdef USE_ROOT_STORAGE + ensure_root_thread_safety(); +#endif + } + std::shared_ptr create_file(form::technology::id tech, std::string const& name, char mode) diff --git a/form/storage/factories.hpp b/form/storage/factories.hpp index 2a4bcda38..9eacc270e 100644 --- a/form/storage/factories.hpp +++ b/form/storage/factories.hpp @@ -11,6 +11,8 @@ namespace form::detail::experimental { + void ensure_backend_thread_safety(); + std::shared_ptr create_file(form::technology::id tech, std::string const& name, char mode); diff --git a/form/storage/storage_reader.cpp b/form/storage/storage_reader.cpp index 43f02bfdb..5d22b4ad1 100644 --- a/form/storage/storage_reader.cpp +++ b/form/storage/storage_reader.cpp @@ -190,6 +190,7 @@ namespace { namespace form::detail::experimental { std::unique_ptr create_storage_reader() { + ensure_backend_thread_safety(); return std::unique_ptr(new storage_reader()); } } diff --git a/form/storage/storage_writer.cpp b/form/storage/storage_writer.cpp index 1d6076bd7..d10aab24e 100644 --- a/form/storage/storage_writer.cpp +++ b/form/storage/storage_writer.cpp @@ -58,6 +58,7 @@ namespace { namespace form::detail::experimental { std::unique_ptr create_storage_writer() { + ensure_backend_thread_safety(); return std::unique_ptr(new storage_writer()); } } From 12795e05ae734bcc2acd0c07978793f6c2809395 Mon Sep 17 00:00:00 2001 From: wwuoneway Date: Fri, 23 Oct 2026 13:38:35 -0500 Subject: [PATCH 2/2] Remove call_once from ROOT thread safety initialization; Rely on ROOT::EnableThreadSafety() to handle repeated calls instead of enforcing one-time initialization in FORM. --- form/root_storage/root_thread_safety.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/form/root_storage/root_thread_safety.cpp b/form/root_storage/root_thread_safety.cpp index f0decc803..8eccf96d0 100644 --- a/form/root_storage/root_thread_safety.cpp +++ b/form/root_storage/root_thread_safety.cpp @@ -4,14 +4,8 @@ #include -#include - namespace form::detail::experimental { - void ensure_root_thread_safety() - { - static std::once_flag enabled; - std::call_once(enabled, [] { ROOT::EnableThreadSafety(); }); - } + void ensure_root_thread_safety() { ROOT::EnableThreadSafety(); } } // namespace form::detail::experimental