From 2ebf71976e629bb65862033808d37d867aac0981 Mon Sep 17 00:00:00 2001 From: Bill Liu Date: Thu, 14 Aug 2025 07:52:24 -0500 Subject: [PATCH] [SWDEV-548260] Enable Support for Multiple init() and shutdown() Implemented reference counting to manage init and shutdown processes, allowing for multiple initializations and shutdowns. [ROCm/amdsmi commit: c45a53d751b8638256e9409c564703c68e851a5b] --- projects/amdsmi/src/amd_smi/amd_smi.cc | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/projects/amdsmi/src/amd_smi/amd_smi.cc b/projects/amdsmi/src/amd_smi/amd_smi.cc index bbd860ae29..03c99d4ee8 100644 --- a/projects/amdsmi/src/amd_smi/amd_smi.cc +++ b/projects/amdsmi/src/amd_smi/amd_smi.cc @@ -65,13 +65,16 @@ // a global instance of std::mutex to protect data passed during threads std::mutex myMutex; -static bool initialized_lib = false; + +// To enable multiple init and shutdown calls, the reference count is used +// to track the number of times the library has been initialized. +static int init_ref_count = 0; #define SIZE 10 char proc_id[SIZE] = "\0"; #define AMDSMI_CHECK_INIT() do { \ - if (!initialized_lib) { \ + if (init_ref_count == 0) { \ return AMDSMI_STATUS_NOT_INIT; \ } \ } while (0) @@ -178,25 +181,31 @@ amdsmi_status_t rsmi_wrapper(F && f, amdsmi_status_t amdsmi_init(uint64_t flags) { - if (initialized_lib) { + if (init_ref_count > 0 ) { + init_ref_count++; return AMDSMI_STATUS_SUCCESS; } amdsmi_status_t status = amd::smi::AMDSmiSystem::getInstance().init(flags); if (status == AMDSMI_STATUS_SUCCESS) { - initialized_lib = true; + init_ref_count++; } return status; } amdsmi_status_t amdsmi_shut_down() { - if (!initialized_lib) + if (init_ref_count == 0) { return AMDSMI_STATUS_SUCCESS; - amdsmi_status_t status = amd::smi::AMDSmiSystem::getInstance().cleanup(); - if (status == AMDSMI_STATUS_SUCCESS) { - initialized_lib = false; } + // Decrement the reference count + init_ref_count--; + // If the reference count is still greater than 0, return success + if (init_ref_count > 0) { + return AMDSMI_STATUS_SUCCESS; + } + amdsmi_status_t status = amd::smi::AMDSmiSystem::getInstance().cleanup(); + return status; }