[Openmp-commits] [openmp] [Libomptarget] Remove global ctor and use reference counting (PR #80499)
Joseph Huber via Openmp-commits
openmp-commits at lists.llvm.org
Tue Feb 6 11:31:36 PST 2024
================
@@ -20,25 +20,36 @@
extern void llvm::omp::target::ompt::connectLibrary();
#endif
-__attribute__((constructor(101))) void init() {
+static std::mutex PluginMtx;
+static uint32_t RefCount = 0;
+
+void initRuntime() {
+ std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
Profiler::get();
TIMESCOPE();
- DP("Init offload library!\n");
-
- PM = new PluginManager();
+ if (PM == nullptr) {
+ DP("Init offload library!\n");
+ PM = new PluginManager();
#ifdef OMPT_SUPPORT
- // Initialize OMPT first
- llvm::omp::target::ompt::connectLibrary();
+ // Initialize OMPT first
+ llvm::omp::target::ompt::connectLibrary();
#endif
- PM->init();
+ PM->init();
+ PM->registerDelayedLibraries();
+ }
- PM->registerDelayedLibraries();
+ RefCount++;
}
-__attribute__((destructor(101))) void deinit() {
- DP("Deinit offload library!\n");
- delete PM;
+void deinitRuntime() {
+ std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
+ assert(PM && "Runtime not initialized");
+
+ if (RefCount-- == 0) {
----------------
jhuber6 wrote:
Good catch, that was originally on the line above but then I moved it in the `if` and forgot to change it.
https://github.com/llvm/llvm-project/pull/80499
More information about the Openmp-commits
mailing list