From: Kefu Chai Date: Fri, 20 Jun 2025 07:23:00 +0000 (+0800) Subject: osd/ECExtentCache: Replace manual mutex operations with std::lock_guard X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=611dee7286611fb8a5ebfa129dc623abfcd6fc21;p=ceph.git osd/ECExtentCache: Replace manual mutex operations with std::lock_guard Replace manual mutex.lock() and mutex.unlock() calls with std::lock_guard for LRU cache access serialization. This improves code safety by ensuring automatic unlock on scope exit and reduces maintenance burden compared to manual mutex management. Signed-off-by: Kefu Chai --- diff --git a/src/osd/ECExtentCache.cc b/src/osd/ECExtentCache.cc index 9ee94c2d9dc81..1e04bc02bc2ad 100644 --- a/src/osd/ECExtentCache.cc +++ b/src/osd/ECExtentCache.cc @@ -4,6 +4,7 @@ #include "ECExtentCache.h" #include "ECUtil.h" +#include #include using namespace std; @@ -369,33 +370,31 @@ void ECExtentCache::LRU::add(const Line &line) { shared_ptr cache = line.cache; - mutex.lock(); + std::lock_guard lock{mutex}; ceph_assert(!map.contains(k)); auto i = lru.insert(lru.end(), k); auto j = make_pair(std::move(i), std::move(cache)); map.insert(std::pair(std::move(k), std::move(j))); size += line.size; // This is already accounted for in mempool. free_maybe(); - mutex.unlock(); } shared_ptr ECExtentCache::LRU::find( const hobject_t &oid, uint64_t offset) { Key k(offset, oid); shared_ptr cache = nullptr; - mutex.lock(); + std::lock_guard lock{mutex}; if (map.contains(k)) { auto &&[lru_iter, c] = map.at(k); cache = c; auto it = lru_iter; // Intentional copy. erase(it, false); } - mutex.unlock(); return cache; } void ECExtentCache::LRU::remove_object(const hobject_t &oid) { - mutex.lock(); + std::lock_guard lock{mutex}; for (auto it = lru.begin(); it != lru.end();) { if (it->oid == oid) { it = erase(it, true); @@ -403,7 +402,6 @@ void ECExtentCache::LRU::remove_object(const hobject_t &oid) { ++it; } } - mutex.unlock(); } void ECExtentCache::LRU::free_maybe() { @@ -414,12 +412,11 @@ void ECExtentCache::LRU::free_maybe() { } void ECExtentCache::LRU::discard() { - mutex.lock(); + std::lock_guard lock{mutex}; lru.clear(); update_mempool(0 - map.size(), 0 - size); map.clear(); size = 0; - mutex.unlock(); } const extent_set ECExtentCache::Op::get_pin_eset(uint64_t alignment) const {