From commits-return-5910-archive-asf-public=cust-asf.ponee.io@kudu.apache.org Mon Apr 30 17:31:57 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 482DD180677 for ; Mon, 30 Apr 2018 17:31:56 +0200 (CEST) Received: (qmail 53180 invoked by uid 500); 30 Apr 2018 15:31:55 -0000 Mailing-List: contact commits-help@kudu.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@kudu.apache.org Delivered-To: mailing list commits@kudu.apache.org Received: (qmail 53110 invoked by uid 99); 30 Apr 2018 15:31:55 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 30 Apr 2018 15:31:55 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B08DCF4EAD; Mon, 30 Apr 2018 15:31:54 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: alexey@apache.org To: commits@kudu.apache.org Date: Mon, 30 Apr 2018 15:31:55 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/4] kudu git commit: cache: switch to std::atomic 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 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 Authored: Wed Apr 25 14:26:45 2018 -0700 Committer: Todd Lipcon 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 #include #include @@ -14,12 +16,9 @@ #include #include -#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 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 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);