| 154 | } |
| 155 | |
| 156 | void Cache::removeAllNotInUseMatching(const IKey &key) |
| 157 | { |
| 158 | // When we're removing items, we don't want their |
| 159 | // refcount getting to 0 while the mutex is locked, as |
| 160 | // deleting the object might recursively call removeAllNotInUseMatching, |
| 161 | // leading to a dead lock. |
| 162 | // |
| 163 | // Instead, we gather the removed objects in the vector below, which will |
| 164 | // be destroyed after the mutex is unlocked. When this happens, the items' |
| 165 | // refcount will be decremented, and any object destruction will happen |
| 166 | // after the mutex is unlocked. Recursion can happen in this case, but won't |
| 167 | // lead to deadlocks |
| 168 | std::vector<std::shared_ptr<CacheItem>> holdItemsUntilMtxUnlocked; |
| 169 | |
| 170 | { |
| 171 | std::unique_lock<std::mutex> lk(pimpl->mtx); |
| 172 | |
| 173 | auto itrange = pimpl->items.equal_range(&key); |
| 174 | |
| 175 | int numItems = std::distance(itrange.first, itrange.second); |
| 176 | |
| 177 | auto it = itrange.first; |
| 178 | for (int i = 0; i < numItems; ++i) |
| 179 | { |
| 180 | if (!it->second->isInUse()) |
| 181 | { |
| 182 | holdItemsUntilMtxUnlocked.push_back(it->second); |
| 183 | pimpl->current_size_inbytes -= it->second->GetSizeInBytes(); |
| 184 | pimpl->items.erase(it++); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | ++it; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | std::vector<std::shared_ptr<CacheItem>> Cache::fetch(const IKey &key) const |
| 195 | { |
no test coverage detected