@@ -21,27 +21,27 @@ namespace cloud {
2121namespace storage_internal {
2222GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2323
24+ void BucketMetadataCache::MoveToFront (std::list<std::string>::iterator it) {
25+ list_.splice (list_.begin (), list_, it);
26+ }
27+
2428absl::optional<BucketCacheEntry> BucketMetadataCache::Get (
2529 std::string const & bucket_name) {
26- std::lock_guard <std::mutex> lock (mu_);
30+ std::unique_lock <std::mutex> lk (mu_);
2731 auto it = map_.find (bucket_name);
2832 if (it == map_.end ()) return absl::nullopt ;
2933
30- list_.erase (it->second .second );
31- list_.push_front (bucket_name);
32- it->second .second = list_.begin ();
34+ MoveToFront (it->second .second );
3335 return it->second .first ;
3436}
3537
3638void BucketMetadataCache::Put (std::string const & bucket_name,
3739 BucketCacheEntry entry) {
38- std::lock_guard <std::mutex> lock (mu_);
40+ std::unique_lock <std::mutex> lk (mu_);
3941 auto it = map_.find (bucket_name);
4042 if (it != map_.end ()) {
4143 it->second .first = std::move (entry);
42- list_.erase (it->second .second );
43- list_.push_front (bucket_name);
44- it->second .second = list_.begin ();
44+ MoveToFront (it->second .second );
4545 return ;
4646 }
4747
@@ -56,7 +56,7 @@ void BucketMetadataCache::Put(std::string const& bucket_name,
5656}
5757
5858void BucketMetadataCache::Invalidate (std::string const & bucket_name) {
59- std::lock_guard <std::mutex> lock (mu_);
59+ std::unique_lock <std::mutex> lk (mu_);
6060 auto it = map_.find (bucket_name);
6161 if (it != map_.end ()) {
6262 list_.erase (it->second .second );
@@ -65,14 +65,14 @@ void BucketMetadataCache::Invalidate(std::string const& bucket_name) {
6565}
6666
6767void BucketMetadataCache::Clear () {
68- std::lock_guard <std::mutex> lock (mu_);
68+ std::unique_lock <std::mutex> lk (mu_);
6969 map_.clear ();
7070 list_.clear ();
7171 in_flight_fetch_.clear ();
7272}
7373
7474bool BucketMetadataCache::StartFetch (std::string const & bucket_name) {
75- std::lock_guard <std::mutex> lock (mu_);
75+ std::unique_lock <std::mutex> lk (mu_);
7676 if (in_flight_fetch_.find (bucket_name) != in_flight_fetch_.end ()) {
7777 return false ;
7878 }
@@ -81,7 +81,7 @@ bool BucketMetadataCache::StartFetch(std::string const& bucket_name) {
8181}
8282
8383void BucketMetadataCache::EndFetch (std::string const & bucket_name) {
84- std::lock_guard <std::mutex> lock (mu_);
84+ std::unique_lock <std::mutex> lk (mu_);
8585 in_flight_fetch_.erase (bucket_name);
8686}
8787
0 commit comments