kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ale...@apache.org
Subject [2/4] kudu git commit: cache: switch to std::atomic
Date Mon, 30 Apr 2018 15:31:55 GMT
cache: switch to std::atomic

A previous commit added some use of std::atomic, so this patch makes the
file self-consistent.

Change-Id: I359f273af9ba7130d230ef639a40e12a8ac51a06
Reviewed-on: http://gerrit.cloudera.org:8080/10209
Tested-by: Kudu Jenkins
Reviewed-by: Todd Lipcon <todd@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/0aa22030
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/0aa22030
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/0aa22030

Branch: refs/heads/master
Commit: 0aa220303276af8979448507c74e3e2820db6fbc
Parents: 64d6d38
Author: Todd Lipcon <todd@apache.org>
Authored: Wed Apr 25 14:26:45 2018 -0700
Committer: Todd Lipcon <todd@apache.org>
Committed: Fri Apr 27 19:59:30 2018 +0000

----------------------------------------------------------------------
 src/kudu/util/cache.cc | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/0aa22030/src/kudu/util/cache.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/cache.cc b/src/kudu/util/cache.cc
index bd6fa05..c0bbba3 100644
--- a/src/kudu/util/cache.cc
+++ b/src/kudu/util/cache.cc
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "kudu/util/cache.h"
+
 #include <atomic>
 #include <cstdint>
 #include <cstring>
@@ -14,12 +16,9 @@
 #include <gflags/gflags.h>
 #include <glog/logging.h>
 
-#include "kudu/gutil/atomicops.h"
-#include "kudu/gutil/atomic_refcount.h"
-#include "kudu/gutil/dynamic_annotations.h"
 #include "kudu/gutil/bits.h"
-#include "kudu/gutil/hash/city.h"
 #include "kudu/gutil/gscoped_ptr.h"
+#include "kudu/gutil/hash/city.h"
 #include "kudu/gutil/macros.h"
 #include "kudu/gutil/port.h"
 #include "kudu/gutil/ref_counted.h"
@@ -27,7 +26,6 @@
 #include "kudu/gutil/strings/substitute.h"
 #include "kudu/gutil/sysinfo.h"
 #include "kudu/util/alignment.h"
-#include "kudu/util/cache.h"
 #include "kudu/util/cache_metrics.h"
 #include "kudu/util/flag_tags.h"
 #include "kudu/util/locks.h"
@@ -77,7 +75,7 @@ struct LRUHandle {
   size_t charge;      // TODO(opt): Only allow uint32_t?
   uint32_t key_length;
   uint32_t val_length;
-  Atomic32 refs;
+  std::atomic<int32_t> refs;
   uint32_t hash;      // Hash of key(); used for fast sharding and comparisons
 
   // The storage for the key/value pair itself. The data is stored as:
@@ -272,7 +270,8 @@ LRUCache::LRUCache(MemTracker* tracker)
 LRUCache::~LRUCache() {
   for (LRUHandle* e = lru_.next; e != &lru_; ) {
     LRUHandle* next = e->next;
-    DCHECK_EQ(e->refs, 1);  // Error if caller has an unreleased handle
+    DCHECK_EQ(e->refs.load(std::memory_order_relaxed), 1)
+        << "caller has an unreleased handle";
     if (Unref(e)) {
       FreeEntry(e);
     }
@@ -282,12 +281,12 @@ LRUCache::~LRUCache() {
 }
 
 bool LRUCache::Unref(LRUHandle* e) {
-  DCHECK_GT(ANNOTATE_UNPROTECTED_READ(e->refs), 0);
-  return !base::RefCountDec(&e->refs);
+  DCHECK_GT(e->refs.load(std::memory_order_relaxed), 0);
+  return e->refs.fetch_sub(1) == 1;
 }
 
 void LRUCache::FreeEntry(LRUHandle* e) {
-  DCHECK_EQ(ANNOTATE_UNPROTECTED_READ(e->refs), 0);
+  DCHECK_EQ(e->refs.load(std::memory_order_relaxed), 0);
   if (e->eviction_callback) {
     e->eviction_callback->EvictedEntry(e->key(), e->value());
   }
@@ -331,7 +330,7 @@ Cache::Handle* LRUCache::Lookup(const Slice& key, uint32_t hash, bool
caching) {
     std::lock_guard<MutexType> l(mutex_);
     e = table_.Lookup(key, hash);
     if (e != nullptr) {
-      base::RefCountInc(&e->refs);
+      e->refs.fetch_add(1, std::memory_order_relaxed);
       LRU_Remove(e);
       LRU_Append(e);
     }
@@ -372,7 +371,7 @@ Cache::Handle* LRUCache::Insert(LRUHandle* e, Cache::EvictionCallback
*eviction_
   // Set the remaining LRUHandle members which were not already allocated during
   // Allocate().
   e->eviction_callback = eviction_callback;
-  e->refs = 2;  // One from LRUCache, one for the returned handle
+  e->refs.store(2, std::memory_order_relaxed);  // One from LRUCache, one for the returned
handle
   UpdateMemTracker(e->charge);
   if (PREDICT_TRUE(metrics_)) {
     metrics_->cache_usage->IncrementBy(e->charge);


Mime
View raw message