From dfa63345edb496986fecd70fca28ff8aba476f2a Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 14 Aug 2015 15:45:19 -0700 Subject: [PATCH] Always hold stat_cache_lock when using stat_cache We could further improve this code by holding stat_cache_lock before calls to DelStat instead of unlocking then relocking it. --- src/cache.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/cache.cpp b/src/cache.cpp index 655d6ee..e050fe8 100644 --- a/src/cache.cpp +++ b/src/cache.cpp @@ -247,10 +247,17 @@ bool StatCache::AddStat(std::string& key, headers_t& meta, bool forcedir) } DPRNNN("add stat cache entry[path=%s]", key.c_str()); - if(stat_cache.end() != stat_cache.find(key)){ + pthread_mutex_lock(&StatCache::stat_cache_lock); + + bool found = stat_cache.end() != stat_cache.find(key); + bool do_truncate = stat_cache.size() > CacheSize; + + pthread_mutex_unlock(&StatCache::stat_cache_lock); + + if(found){ DelStat(key.c_str()); }else{ - if(stat_cache.size() > CacheSize){ + if(do_truncate){ if(!TruncateCache()){ return false; } @@ -302,10 +309,17 @@ bool StatCache::AddNoObjectCache(string& key) } DPRNNN("add no object cache entry[path=%s]", key.c_str()); - if(stat_cache.end() != stat_cache.find(key)){ + pthread_mutex_lock(&StatCache::stat_cache_lock); + + bool found = stat_cache.end() != stat_cache.find(key); + bool do_truncate = stat_cache.size() > CacheSize; + + pthread_mutex_unlock(&StatCache::stat_cache_lock); + + if(found){ DelStat(key.c_str()); }else{ - if(stat_cache.size() > CacheSize){ + if(do_truncate){ if(!TruncateCache()){ return false; } @@ -330,12 +344,12 @@ bool StatCache::AddNoObjectCache(string& key) bool StatCache::TruncateCache(void) { + pthread_mutex_lock(&StatCache::stat_cache_lock); + if(stat_cache.empty()){ return true; } - pthread_mutex_lock(&StatCache::stat_cache_lock); - time_t lowest_time = time(NULL) + 1; stat_cache_t::iterator iter_to_delete = stat_cache.end(); stat_cache_t::iterator iter;