HashTable: Use NonNegativeCounter<> for num{...}Items 47/76047/9
authorDave Rigby <daver@couchbase.com>
Tue, 28 Mar 2017 15:35:10 +0000 (16:35 +0100)
committerDave Rigby <daver@couchbase.com>
Fri, 7 Apr 2017 11:05:04 +0000 (11:05 +0000)
Use cb::NonNegativeCounter<> for counts of items in the HashTable,
instead of std::atomic and manually checking for underflow.

Change-Id: I3b8182d6b443971c6d23f0e55e42a78825e147c2
Reviewed-on: http://review.couchbase.org/76047
Reviewed-by: Oliver Downard <oliver.downard@couchbase.com>
Reviewed-by: Daniel Owen <owend@couchbase.com>
Tested-by: Build Bot <build@couchbase.com>
src/hash_table.cc
src/hash_table.h

index 06a8d67..4079e44 100644 (file)
@@ -289,7 +289,7 @@ MutationStatus HashTable::unlocked_updateStoredValue(
     }
 
     if (v.isDeleted() && !itm.isDeleted()) {
-        numDeletedItems.fetch_sub(1, std::memory_order_relaxed);
+        --numDeletedItems;
     }
 
     // If the item we are replacing is resident then we need to make sure we
@@ -383,7 +383,7 @@ void HashTable::unlocked_softDelete(const std::unique_lock<std::mutex>& htLock,
         }
         v.del(*this);
     }
-    numDeletedItems.fetch_add(1, std::memory_order_relaxed);
+    ++numDeletedItems;
 }
 
 StoredValue* HashTable::unlocked_find(const DocKey& key,
@@ -446,7 +446,7 @@ StoredValue::UniquePtr HashTable::unlocked_release(
         decrNumTotalItems();
         --datatypeCounts[released->getDatatype()];
         if (released->isDeleted()) {
-            numDeletedItems.fetch_sub(1, std::memory_order_relaxed);
+            --numDeletedItems;
         }
     }
     return released;
index 0f4079a..54dafe7 100644 (file)
@@ -228,40 +228,16 @@ public:
         numTotalItems = totalItems;
     }
 
-    void decrNumItems(void) {
-        size_t count;
-        do {
-            count = numItems.load();
-            if (count == 0) {
-                LOG(EXTENSION_LOG_DEBUG,
-                    "Cannot decrement numItems, value at 0 already");
-                break;
-            }
-        } while (!numItems.compare_exchange_strong(count, count - 1));
+    void decrNumItems() {
+        --numItems;
     }
 
-    void decrNumTotalItems(void) {
-        size_t count;
-        do {
-            count = numTotalItems.load();
-            if (count == 0) {
-                LOG(EXTENSION_LOG_DEBUG,
-                    "Cannot decrement numTotalItems, value at 0 already");
-                break;
-            }
-        } while (!numTotalItems.compare_exchange_strong(count, count - 1));
+    void decrNumTotalItems() {
+        --numTotalItems;
     }
 
-    void decrNumNonResidentItems(void) {
-        size_t count;
-        do {
-            count = numNonResidentItems.load();
-            if (count == 0) {
-                LOG(EXTENSION_LOG_DEBUG,
-                    "Cannot decrement numNonResidentItems, value at 0 already");
-                break;
-            }
-        } while (!numNonResidentItems.compare_exchange_strong(count, count - 1));
+    void decrNumNonResidentItems() {
+        --numNonResidentItems;
     }
 
     /**
@@ -609,8 +585,8 @@ public:
     std::atomic<size_t>       numTotalItems;
     std::array<cb::NonNegativeCounter<size_t>, mcbp::datatype::highest + 1>
             datatypeCounts;
-    std::atomic<size_t>       numNonResidentItems;
-    std::atomic<size_t> numDeletedItems;
+    cb::NonNegativeCounter<size_t> numNonResidentItems;
+    cb::NonNegativeCounter<size_t> numDeletedItems;
     std::atomic<size_t>       numEjects;
     //! Memory consumed by items in this hashtable.
     std::atomic<size_t>       memSize;
@@ -635,7 +611,7 @@ private:
     EPStats&             stats;
     std::unique_ptr<AbstractStoredValueFactory> valFact;
     std::atomic<size_t>       visitors;
-    std::atomic<size_t>       numItems;
+    cb::NonNegativeCounter<size_t> numItems;
     std::atomic<size_t>       numResizes;
     std::atomic<size_t>       numTempItems;
     bool                 activeState;