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"
@@ -365,40 +366,19 @@ ZeroInitStructsHolder::ZeroInitStructsHolder()
365366}
366367
367368const std::shared_ptr<ZeroInitStructsHolder> ZeroInitStructsHolder::getInstance () {
368- std::lock_guard<std::mutex> lock (getMutex ());
369- auto instance = getInstanceStorage ().lock ();
369+ static std::mutex mutex;
370+ static std::weak_ptr<ZeroInitStructsHolder> weak_instance;
371+
372+ std::lock_guard<std::mutex> lock (mutex);
373+ auto instance = weak_instance.lock ();
370374 if (!instance) {
371375 instance = std::make_shared<ZeroInitStructsHolder>();
372- getInstanceStorage () = instance;
376+ weak_instance = instance;
373377 }
374378 return instance;
375379}
376380
377- void ZeroInitStructsHolder::destroy () {
378- std::lock_guard<std::mutex> lock (getMutex ());
379- auto instance = getInstanceStorage ().lock ();
380- if (instance && instance.use_count () == 2 ) {
381- instance->destroy_context ();
382- // reset weak pointer
383- getInstanceStorage ().reset ();
384- // reset shared pointer
385- instance.reset ();
386- }
387- // don't destroy if ref count is higher than 2.
388- // one is after getInstanceStorage().lock() and another one is hold by the caller
389- }
390-
391- std::weak_ptr<ZeroInitStructsHolder>& ZeroInitStructsHolder::getInstanceStorage () {
392- static std::weak_ptr<ZeroInitStructsHolder> weak_instance;
393- return weak_instance;
394- }
395-
396- std::mutex& ZeroInitStructsHolder::getMutex () {
397- static std::mutex mutex;
398- return mutex;
399- }
400-
401- void ZeroInitStructsHolder::destroy_context () {
381+ ZeroInitStructsHolder::~ZeroInitStructsHolder () {
402382 if (context) {
403383 if (context_npu_dditable_ext_decorator->version () >= ZE_MAKE_VERSION (1 , 0 )) {
404384 context_options &= ~(ZE_NPU_CONTEXT_OPTION_ENABLE_IDLE_OPTIMIZATIONS);
@@ -421,8 +401,4 @@ void ZeroInitStructsHolder::destroy_context() {
421401 }
422402}
423403
424- ZeroInitStructsHolder::~ZeroInitStructsHolder () {
425- destroy_context ();
426- }
427-
428404} // namespace intel_npu
0 commit comments