diff options
author | costan <costan@google.com> | 2018-03-16 10:06:35 -0700 |
---|---|---|
committer | Victor Costan <pwnall@chromium.org> | 2018-03-16 10:32:40 -0700 |
commit | 0fa5a4f7b1ad9dc16b705bcad1f3ca913f187325 (patch) | |
tree | 1e90dee838f0acc2c9d9ccb187f79700e83b4518 /util/cache.cc | |
parent | 8143c12f3fc483b1ba61cdce11f9c1faf6d01bea (diff) | |
download | leveldb-0fa5a4f7b1ad9dc16b705bcad1f3ca913f187325.tar.gz |
Extend thread safety annotations.
This CL makes it easier to reason about thread safety by:
1) Adding Clang thread safety annotations according to comments.
2) Expanding a couple of variable names, without adding extra lines of code.
3) Adding const in a couple of places.
4) Replacing an always-non-null const pointer with a reference.
5) Fixing style warnings in the modified files.
This CL does not annotate the DBImpl members that claim to be protected
by the instance mutex, but are accessed without the mutex being held.
Those members (and their unprotected accesses) will be addressed in
future CLs.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=189354657
Diffstat (limited to 'util/cache.cc')
-rw-r--r-- | util/cache.cc | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/util/cache.cc b/util/cache.cc index bd914ae..10b7103 100644 --- a/util/cache.cc +++ b/util/cache.cc @@ -8,6 +8,7 @@ #include "leveldb/cache.h" #include "port/port.h" +#include "port/thread_annotations.h" #include "util/hash.h" #include "util/mutexlock.h" @@ -174,25 +175,25 @@ class LRUCache { void LRU_Append(LRUHandle*list, LRUHandle* e); void Ref(LRUHandle* e); void Unref(LRUHandle* e); - bool FinishErase(LRUHandle* e); + bool FinishErase(LRUHandle* e) EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Initialized before use. size_t capacity_; // mutex_ protects the following state. mutable port::Mutex mutex_; - size_t usage_; + size_t usage_ GUARDED_BY(mutex_); // Dummy head of LRU list. // lru.prev is newest entry, lru.next is oldest entry. // Entries have refs==1 and in_cache==true. - LRUHandle lru_; + LRUHandle lru_ GUARDED_BY(mutex_); // Dummy head of in-use list. // Entries are in use by clients, and have refs >= 2 and in_cache==true. - LRUHandle in_use_; + LRUHandle in_use_ GUARDED_BY(mutex_); - HandleTable table_; + HandleTable table_ GUARDED_BY(mutex_); }; LRUCache::LRUCache() @@ -227,11 +228,12 @@ void LRUCache::Ref(LRUHandle* e) { void LRUCache::Unref(LRUHandle* e) { assert(e->refs > 0); e->refs--; - if (e->refs == 0) { // Deallocate. + if (e->refs == 0) { // Deallocate. assert(!e->in_cache); (*e->deleter)(e->key(), e->value); free(e); - } else if (e->in_cache && e->refs == 1) { // No longer in use; move to lru_ list. + } else if (e->in_cache && e->refs == 1) { + // No longer in use; move to lru_ list. LRU_Remove(e); LRU_Append(&lru_, e); } |