From 9f11f51ae82e9f1d046f2c6666fe72c768c4c872 Mon Sep 17 00:00:00 2001 From: Frank Winklmeier Date: Thu, 25 Aug 2022 10:11:12 +0200 Subject: [PATCH] G4ExternalDecay: thread-checker fixes Cleanup the `Pythia8ForDecays` singleton (no need for complicated mutex logic, static initialization is thread-safe) and mark is as not thread-safe because it exposes a non-const pointer to a static. --- .../G4Extensions/G4ExternalDecay/CMakeLists.txt | 6 ++---- .../G4ExternalDecay/ATLAS_CHECK_THREAD_SAFETY | 1 + .../G4ExternalDecay/Pythia8ForDecays.h | 13 +++++-------- .../G4ExternalDecay/src/Pythia8ForDecays.cxx | 13 ++++--------- 4 files changed, 12 insertions(+), 21 deletions(-) create mode 100644 Simulation/G4Extensions/G4ExternalDecay/G4ExternalDecay/ATLAS_CHECK_THREAD_SAFETY diff --git a/Simulation/G4Extensions/G4ExternalDecay/CMakeLists.txt b/Simulation/G4Extensions/G4ExternalDecay/CMakeLists.txt index 181c1a740d5..619384a27e8 100644 --- a/Simulation/G4Extensions/G4ExternalDecay/CMakeLists.txt +++ b/Simulation/G4Extensions/G4ExternalDecay/CMakeLists.txt @@ -1,6 +1,4 @@ -################################################################################ -# Package: G4ExternalDecay -################################################################################ +# Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration # Declare the package name: atlas_subdir( G4ExternalDecay ) @@ -37,5 +35,5 @@ atlas_add_library( G4ExternalDecay INCLUDE_DIRS ${XERCESC_INCLUDE_DIRS} ${CLHEP_INCLUDE_DIRS} ${LHAPDF_INCLUDE_DIRS} ${GEANT4_INCLUDE_DIRS} ${PYTHIA8_INCLUDE_DIRS} ${extra_includes} PRIVATE_INCLUDE_DIRS DEFINITIONS ${CLHEP_DEFINITIONS} - LINK_LIBRARIES ${XERCESC_LIBRARIES} ${CLHEP_LIBRARIES} ${PYTHIA8_LIBRARIES} ${LHAPDF_LIBRARIES} ${GEANT4_LIBRARIES} Pythia8_iLib ${extra_libs} + LINK_LIBRARIES ${XERCESC_LIBRARIES} ${CLHEP_LIBRARIES} ${PYTHIA8_LIBRARIES} ${LHAPDF_LIBRARIES} ${GEANT4_LIBRARIES} CxxUtils Pythia8_iLib ${extra_libs} PRIVATE_LINK_LIBRARIES AtlasHepMCLib ) diff --git a/Simulation/G4Extensions/G4ExternalDecay/G4ExternalDecay/ATLAS_CHECK_THREAD_SAFETY b/Simulation/G4Extensions/G4ExternalDecay/G4ExternalDecay/ATLAS_CHECK_THREAD_SAFETY new file mode 100644 index 00000000000..9271bccd5d6 --- /dev/null +++ b/Simulation/G4Extensions/G4ExternalDecay/G4ExternalDecay/ATLAS_CHECK_THREAD_SAFETY @@ -0,0 +1 @@ +Simulation/G4Extensions/G4ExternalDecay diff --git a/Simulation/G4Extensions/G4ExternalDecay/G4ExternalDecay/Pythia8ForDecays.h b/Simulation/G4Extensions/G4ExternalDecay/G4ExternalDecay/Pythia8ForDecays.h index a782ed4ef76..d9e5c6148e0 100644 --- a/Simulation/G4Extensions/G4ExternalDecay/G4ExternalDecay/Pythia8ForDecays.h +++ b/Simulation/G4Extensions/G4ExternalDecay/G4ExternalDecay/Pythia8ForDecays.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration */ // Abused from Geant4 version of Pythai6.hh from extended examples @@ -15,9 +15,10 @@ #include // For all the various Pythia8 classes used here #include "Pythia8_i/Pythia8_i.h" -// For unique_ptr and once flag +// For unique_ptr #include -#include +// For ATLAS THREAD macros +#include "CxxUtils/checker_macros.h" class G4DynamicParticle; class G4ParticleDefinition; @@ -28,7 +29,7 @@ class Pythia8ForDecays virtual ~Pythia8ForDecays() = default; - static Pythia8ForDecays *Instance(); + static Pythia8ForDecays *Instance ATLAS_NOT_THREAD_SAFE (); /// Function that decays the RHadron; returns products in G4 format void Py1ent(const G4Track&, std::vector &); @@ -49,10 +50,6 @@ class Pythia8ForDecays std::pair fromIdWithSquark( int idRHad) const; bool isGluinoRHadron(int pdgId) const; - /// My own class; singleton pattern; thread safe for future-proofing - static std::unique_ptr s_instance; - static std::once_flag m_onceFlag; - /// The instance of Pythia8 that will do the work std::unique_ptr m_pythia; }; diff --git a/Simulation/G4Extensions/G4ExternalDecay/src/Pythia8ForDecays.cxx b/Simulation/G4Extensions/G4ExternalDecay/src/Pythia8ForDecays.cxx index c5173dcb754..fa7eba76cb6 100644 --- a/Simulation/G4Extensions/G4ExternalDecay/src/Pythia8ForDecays.cxx +++ b/Simulation/G4Extensions/G4ExternalDecay/src/Pythia8ForDecays.cxx @@ -1,5 +1,5 @@ /* - Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration */ // Abused from Pythia6.cc in Geant4 extended examples @@ -30,15 +30,10 @@ #include #include -std::unique_ptr Pythia8ForDecays::s_instance(nullptr); -std::once_flag Pythia8ForDecays::m_onceFlag; -Pythia8ForDecays* Pythia8ForDecays::Instance() { - std::call_once(m_onceFlag, - [] { - s_instance.reset(new Pythia8ForDecays); - }); - return s_instance.get(); +Pythia8ForDecays* Pythia8ForDecays::Instance ATLAS_NOT_THREAD_SAFE () { + static Pythia8ForDecays instance; + return &instance; } Pythia8ForDecays::Pythia8ForDecays() -- GitLab