Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/inference/src/cache_guard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CacheGuardEntry::CacheGuardEntry(CacheGuard& cacheGuard,
std::atomic_int& refCount)
: m_cacheGuard(cacheGuard),
m_hash(hash),
m_mutex(m),
m_mutex(std::move(m)),
m_refCount(refCount) {
// Don't lock mutex right here for exception-safe considerations
m_refCount++;
Expand All @@ -31,21 +31,22 @@ void CacheGuardEntry::perform_lock() {
//////////////////////////////////////////////////////

std::unique_ptr<CacheGuardEntry> CacheGuard::get_hash_lock(const std::string& hash) {
std::unique_lock<std::mutex> lock(m_tableMutex);
auto& data = m_table[hash];
std::unique_ptr<CacheGuardEntry> res;
try {
// TODO: use std::make_unique when migrated to C++14
res =
std::unique_ptr<CacheGuardEntry>(new CacheGuardEntry(*this, hash, data.m_mutexPtr, data.m_itemRefCounter));
} catch (...) {
// In case of exception, we shall remove hash entry if it is not used
if (data.m_itemRefCounter == 0) {
m_table.erase(hash);
{
std::unique_lock<std::mutex> lock(m_tableMutex);
auto& data = m_table[hash];
try {
// TODO: use std::make_unique when migrated to C++14
res = std::unique_ptr<CacheGuardEntry>(
new CacheGuardEntry(*this, hash, data.m_mutexPtr, data.m_itemRefCounter));
} catch (...) {
// In case of exception, we shall remove hash entry if it is not used
if (data.m_itemRefCounter == 0) {
m_table.erase(hash);
}
throw;
}
throw;
}
lock.unlock(); // can unlock table lock here, as refCounter is positive and nobody can remove entry
res->perform_lock(); // in case of exception, 'res' will be destroyed and item will be cleaned up from table
return res;
}
Expand Down