Add eraseIfEqual to ConcurrentMap.
To only delete a key if its value matches
the passed in value.
Bug: 63940457
Test: hidl_test
Merged-In: I67ac1ccd0da16a84de88886af54e234642400cf6
Change-Id: I67ac1ccd0da16a84de88886af54e234642400cf6
diff --git a/transport/include/hidl/ConcurrentMap.h b/transport/include/hidl/ConcurrentMap.h
index cd55ad4..4066869 100644
--- a/transport/include/hidl/ConcurrentMap.h
+++ b/transport/include/hidl/ConcurrentMap.h
@@ -50,6 +50,20 @@
return mMap.erase(k);
}
+ size_type eraseIfEqual(const K& k, const V& v) {
+ std::unique_lock<std::mutex> _lock(mMutex);
+ const_iterator iter = mMap.find(k);
+ if (iter == mMap.end()) {
+ return 0;
+ }
+ if (iter->second == v) {
+ mMap.erase(iter);
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+
std::unique_lock<std::mutex> lock() { return std::unique_lock<std::mutex>(mMutex); }
void setLocked(K&& k, V&& v) { mMap[std::forward<K>(k)] = std::forward<V>(v); }