Skip to content

Commit 66fb447

Browse files
authored
[NPU] Use weak pointer to safely destroy shared ptr when ref count is 0 (#32929)
### Details: - *Use a weak pointer to safely destroy the shared ptr when the ref count from the application is 0, and not increase it in the static method* ### Tickets: - *CVS-176148* Signed-off-by: Bogdan Pereanu <[email protected]>
1 parent 4bf30cb commit 66fb447

File tree

7 files changed

+30
-9
lines changed

7 files changed

+30
-9
lines changed

src/plugins/intel_npu/src/backend/include/zero_backend.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace intel_npu {
1515
class ZeroEngineBackend final : public IEngineBackend {
1616
public:
1717
ZeroEngineBackend();
18-
virtual ~ZeroEngineBackend();
18+
~ZeroEngineBackend() override = default;
19+
1920
const std::shared_ptr<IDevice> getDevice() const override;
2021
const std::shared_ptr<IDevice> getDevice(const std::string&) const override;
2122
const std::string getName() const override {

src/plugins/intel_npu/src/backend/src/zero_backend.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ bool ZeroEngineBackend::isLUIDExtSupported() const {
3737
return _initStruct->isExtensionSupported(std::string(ZE_DEVICE_LUID_EXT_NAME), ZE_MAKE_VERSION(1, 0));
3838
}
3939

40-
ZeroEngineBackend::~ZeroEngineBackend() = default;
41-
4240
const std::shared_ptr<IDevice> ZeroEngineBackend::getDevice() const {
4341
if (_devices.empty()) {
4442
_logger.debug("ZeroEngineBackend - getDevice() returning empty list");

src/plugins/intel_npu/src/plugin/include/remote_context.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class RemoteContextImpl : public ov::IRemoteContext {
2121
public:
2222
RemoteContextImpl(const ov::SoPtr<IEngineBackend>& engineBackend, const ov::AnyMap& remote_properties = {});
2323

24+
~RemoteContextImpl() override = default;
25+
2426
/**
2527
* @brief Returns name of a device on which underlying object is allocated.
2628
* @return A device name string in fully specified format `<device_name>[.<device_id>[.<tile_id>]]` (e.g. GPU.0.1).

src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_api.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ZeroApi {
7878

7979
~ZeroApi() = default;
8080

81-
static const std::shared_ptr<ZeroApi>& getInstance();
81+
static const std::shared_ptr<ZeroApi> getInstance();
8282

8383
#define symbol_statement(symbol) decltype(&::symbol) symbol;
8484
symbols_list();

src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_init.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ZeroInitStructsHolder final {
7979
return _external_memory_fd_win32_supported;
8080
}
8181

82-
static const std::shared_ptr<ZeroInitStructsHolder>& getInstance();
82+
static const std::shared_ptr<ZeroInitStructsHolder> getInstance();
8383

8484
private:
8585
void initNpuDriver();

src/plugins/intel_npu/src/utils/src/zero/zero_api.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "intel_npu/utils/zero/zero_api.hpp"
66

7+
#include <mutex>
8+
79
#include "openvino/util/file_util.hpp"
810
#include "openvino/util/shared_object.hpp"
911

@@ -49,8 +51,16 @@ ZeroApi::ZeroApi() {
4951
#undef symbol_statement
5052
}
5153

52-
const std::shared_ptr<ZeroApi>& ZeroApi::getInstance() {
53-
static std::shared_ptr<ZeroApi> instance = std::make_shared<ZeroApi>();
54+
const std::shared_ptr<ZeroApi> ZeroApi::getInstance() {
55+
static std::mutex mutex;
56+
static std::weak_ptr<ZeroApi> weak_instance;
57+
58+
std::lock_guard<std::mutex> lock(mutex);
59+
auto instance = weak_instance.lock();
60+
if (!instance) {
61+
instance = std::make_shared<ZeroApi>();
62+
weak_instance = instance;
63+
}
5464
return instance;
5565
}
5666

src/plugins/intel_npu/src/utils/src/zero/zero_init.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <ze_command_queue_npu_ext.h>
88
#include <ze_mem_import_system_memory_ext.h>
99

10+
#include <mutex>
1011
#include <regex>
1112

1213
#include "intel_npu/utils/zero/zero_utils.hpp"
@@ -331,15 +332,24 @@ ZeroInitStructsHolder::ZeroInitStructsHolder()
331332
}
332333
}
333334

334-
const std::shared_ptr<ZeroInitStructsHolder>& ZeroInitStructsHolder::getInstance() {
335-
static std::shared_ptr<ZeroInitStructsHolder> instance = std::make_shared<ZeroInitStructsHolder>();
335+
const std::shared_ptr<ZeroInitStructsHolder> ZeroInitStructsHolder::getInstance() {
336+
static std::mutex mutex;
337+
static std::weak_ptr<ZeroInitStructsHolder> weak_instance;
338+
339+
std::lock_guard<std::mutex> lock(mutex);
340+
auto instance = weak_instance.lock();
341+
if (!instance) {
342+
instance = std::make_shared<ZeroInitStructsHolder>();
343+
weak_instance = instance;
344+
}
336345
return instance;
337346
}
338347

339348
ZeroInitStructsHolder::~ZeroInitStructsHolder() {
340349
if (context) {
341350
log.debug("ZeroInitStructsHolder - performing zeContextDestroy");
342351
auto result = zeContextDestroy(context);
352+
context = nullptr;
343353
if (result != ZE_RESULT_SUCCESS) {
344354
if (result == ZE_RESULT_ERROR_UNINITIALIZED) {
345355
log.warning("zeContextDestroy failed to destroy the context; Level zero context was already destroyed");

0 commit comments

Comments
 (0)