review: remove static global vars to support multi-instance simultaneously and thread safe

This commit is contained in:
zhou.weiguo 2024-06-07 14:56:07 +08:00
parent 2fab33d825
commit 94ee775058
No known key found for this signature in database
GPG key ID: 952EA81D18BB2FA8

View file

@ -936,10 +936,10 @@ private:
std::unordered_set<Qnn_MemHandle_t> _qnn_mem_set; std::unordered_set<Qnn_MemHandle_t> _qnn_mem_set;
static std::mutex _init_mutex; std::mutex _init_mutex;
static std::unordered_map<BackendIdType, void *> _loaded_lib_handle; std::unordered_map<BackendIdType, void *> _loaded_lib_handle;
static std::unordered_map<std::string, BackendIdType> _lib_path_to_backend_id; std::unordered_map<std::string, BackendIdType> _lib_path_to_backend_id;
static std::unordered_map<BackendIdType, const QnnInterface_t *> _loaded_backend; std::unordered_map<BackendIdType, const QnnInterface_t *> _loaded_backend;
void * _rpc_lib_handle = nullptr; void * _rpc_lib_handle = nullptr;
std::atomic_bool _rpcmem_initialized{false}; std::atomic_bool _rpcmem_initialized{false};
@ -950,26 +950,15 @@ private:
pfn_rpc_mem_deinit _pfn_rpc_mem_deinit; pfn_rpc_mem_deinit _pfn_rpc_mem_deinit;
std::unordered_map<void *, void *> _rpcmem_store_map; std::unordered_map<void *, void *> _rpcmem_store_map;
std::string _graph_name; std::string _graph_name;
}; };
// ================================================================================================= // =================================================================================================
// //
// implementation of wrapper class // implementation of wrapper class
// //
// ================================================================================================= // =================================================================================================
std::mutex qnn_instance::_init_mutex;
std::unordered_map<qnn_instance::BackendIdType, void *> qnn_instance::_loaded_lib_handle;
std::unordered_map<std::string, qnn_instance::BackendIdType> qnn_instance::_lib_path_to_backend_id;
std::unordered_map<qnn_instance::BackendIdType, const QnnInterface_t *> qnn_instance::_loaded_backend;
void * qnn_instance::alloc_rpcmem(size_t bytes, size_t alignment) { void * qnn_instance::alloc_rpcmem(size_t bytes, size_t alignment) {
if (!_rpcmem_initialized) { if (!_rpcmem_initialized) {
QNN_LOG_WARN("rpc memory not initialized\n"); QNN_LOG_WARN("rpc memory not initialized\n");
@ -983,8 +972,7 @@ void * qnn_instance::alloc_rpcmem(size_t bytes, size_t alignment) {
return nullptr; return nullptr;
} }
auto aligned_buf = reinterpret_cast<void *>(align_to(alignment, auto aligned_buf = reinterpret_cast<void *>(align_to(alignment,reinterpret_cast<intptr_t>(buf)));
reinterpret_cast<intptr_t>(buf)));
bool status = _rpcmem_store_map.insert(std::pair<void *, void *>(aligned_buf, buf)).second; bool status = _rpcmem_store_map.insert(std::pair<void *, void *>(aligned_buf, buf)).second;
if (!status) { if (!status) {
QNN_LOG_WARN("failed to allocate rpc memory\n"); QNN_LOG_WARN("failed to allocate rpc memory\n");