You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by jb...@apache.org on 2017/03/07 01:36:46 UTC

[06/21] geode-native git commit: GEODE-2494: Replace SpinLock with spinlock_mutex.

GEODE-2494: Replace SpinLock with spinlock_mutex.


Project: http://git-wip-us.apache.org/repos/asf/geode-native/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode-native/commit/f4eefdf6
Tree: http://git-wip-us.apache.org/repos/asf/geode-native/tree/f4eefdf6
Diff: http://git-wip-us.apache.org/repos/asf/geode-native/diff/f4eefdf6

Branch: refs/heads/feature/GEODE-2602
Commit: f4eefdf6c7719c64a28390448583128c896f5717
Parents: e086685
Author: Jacob Barrett <jb...@pivotal.io>
Authored: Mon Feb 20 13:42:19 2017 -0800
Committer: Jacob Barrett <jb...@pivotal.io>
Committed: Mon Mar 6 17:32:10 2017 -0800

----------------------------------------------------------------------
 src/cppcache/src/LRUList.cpp | 47 ++++++++++++++++++++++++---------------
 src/cppcache/src/LRUList.hpp | 38 ++++++++++++++++---------------
 2 files changed, 49 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode-native/blob/f4eefdf6/src/cppcache/src/LRUList.cpp
----------------------------------------------------------------------
diff --git a/src/cppcache/src/LRUList.cpp b/src/cppcache/src/LRUList.cpp
index c752faa..00879c9 100644
--- a/src/cppcache/src/LRUList.cpp
+++ b/src/cppcache/src/LRUList.cpp
@@ -15,12 +15,18 @@
  * limitations under the License.
  */
 #include "LRUList.hpp"
-#include "SpinLock.hpp"
+#include "util/concurrent/spinlock_mutex.hpp"
 
-using namespace apache::geode::client;
+#include <mutex>
 
-#define LOCK_HEAD SpinLockGuard headLockGuard(m_headLock)
-#define LOCK_TAIL SpinLockGuard tailLockGuard(m_tailLock)
+namespace apache {
+namespace geode {
+namespace client {
+
+using util::concurrent::spinlock_mutex;
+
+//#define LOCK_HEAD SpinLockGuard headLockGuard(m_headLock)
+//#define LOCK_TAIL SpinLockGuard tailLockGuard(m_tailLock)
 
 template <typename TEntry, typename TCreateEntry>
 LRUList<TEntry, TCreateEntry>::LRUList() : m_headLock(), m_tailLock() {
@@ -32,9 +38,9 @@ LRUList<TEntry, TCreateEntry>::LRUList() : m_headLock(), m_tailLock() {
 
 template <typename TEntry, typename TCreateEntry>
 LRUList<TEntry, TCreateEntry>::~LRUList() {
-  m_tailNode = NULL;
+  m_tailNode = nullptr;
   LRUListNode* next;
-  while (m_headNode != NULL) {
+  while (m_headNode != nullptr) {
     next = m_headNode->getNextLRUListNode();
     delete m_headNode;
     m_headNode = next;
@@ -43,7 +49,7 @@ LRUList<TEntry, TCreateEntry>::~LRUList() {
 
 template <typename TEntry, typename TCreateEntry>
 void LRUList<TEntry, TCreateEntry>::appendEntry(const LRUListEntryPtr& entry) {
-  LOCK_TAIL;
+  std::lock_guard<spinlock_mutex> lk(m_tailLock);
 
   LRUListNode* aNode = new LRUListNode(entry);
   m_tailNode->setNextLRUListNode(aNode);
@@ -52,9 +58,9 @@ void LRUList<TEntry, TCreateEntry>::appendEntry(const LRUListEntryPtr& entry) {
 
 template <typename TEntry, typename TCreateEntry>
 void LRUList<TEntry, TCreateEntry>::appendNode(LRUListNode* aNode) {
-  LOCK_TAIL;
+  std::lock_guard<spinlock_mutex> lk(m_tailLock);
 
-  GF_D_ASSERT(aNode != NULL);
+  GF_D_ASSERT(aNode != nullptr);
 
   aNode->clearNextLRUListNode();
   m_tailNode->setNextLRUListNode(aNode);
@@ -66,8 +72,8 @@ void LRUList<TEntry, TCreateEntry>::getLRUEntry(LRUListEntryPtr& result) {
   bool isLast = false;
   LRUListNode* aNode;
   while (true) {
-    aNode = getHeadNode(isLast);
-    if (aNode == NULL) {
+    aNode = getHeadNode(&isLast);
+    if (aNode == nullptr) {
       result = NULLPTR;
       break;
     }
@@ -96,23 +102,24 @@ void LRUList<TEntry, TCreateEntry>::getLRUEntry(LRUListEntryPtr& result) {
 
 template <typename TEntry, typename TCreateEntry>
 typename LRUList<TEntry, TCreateEntry>::LRUListNode*
-LRUList<TEntry, TCreateEntry>::getHeadNode(bool& isLast) {
-  LOCK_HEAD;
+LRUList<TEntry, TCreateEntry>::getHeadNode(bool* isLast) {
+  std::lock_guard<spinlock_mutex> lk(m_headLock);
 
   LRUListNode* result = m_headNode;
   LRUListNode* nextNode;
 
   {
-    LOCK_TAIL;
+    std::lock_guard<spinlock_mutex> lk(m_tailLock);
+
     nextNode = m_headNode->getNextLRUListNode();
-    if (nextNode == NULL) {
+    if (nextNode == nullptr) {
       // last one in the list...
-      isLast = true;
+      *isLast = true;
       LRUListEntryPtr entry;
       result->getEntry(entry);
       if (entry->getLRUProperties().testEvicted()) {
         // list is empty.
-        return NULL;
+        return nullptr;
       } else {
         entry->getLRUProperties().setEvicted();
         return result;
@@ -120,9 +127,13 @@ LRUList<TEntry, TCreateEntry>::getHeadNode(bool& isLast) {
     }
   }
 
-  isLast = false;
+  *isLast = false;
   // advance head node, and return old value.
   m_headNode = nextNode;
 
   return result;
 }
+
+}  // namespace client
+}  // namespace geode
+}  // namespace apache

http://git-wip-us.apache.org/repos/asf/geode-native/blob/f4eefdf6/src/cppcache/src/LRUList.hpp
----------------------------------------------------------------------
diff --git a/src/cppcache/src/LRUList.hpp b/src/cppcache/src/LRUList.hpp
index 3e1d30a..fc18fcd 100644
--- a/src/cppcache/src/LRUList.hpp
+++ b/src/cppcache/src/LRUList.hpp
@@ -20,32 +20,32 @@
  * limitations under the License.
  */
 
+#include <atomic>
+
 #include <geode/geode_globals.hpp>
 #include <geode/SharedPtr.hpp>
-#include "SpinLock.hpp"
+
+#include "util/concurrent/spinlock_mutex.hpp"
 
 namespace apache {
 namespace geode {
 namespace client {
+
 // Bit mask for recently used
-#define RECENTLY_USED_BITS 1ul
+#define RECENTLY_USED_BITS 1u
 // Bit mask for evicted
-#define EVICTED_BITS 2ul
+#define EVICTED_BITS 2u
 
 /**
  * @brief This class encapsulates LRU specific properties for a LRUList node.
  */
 class CPPCACHE_EXPORT LRUEntryProperties {
  public:
-  inline LRUEntryProperties() : m_bits(0), m_persistenceInfo(NULL) {}
+  inline LRUEntryProperties() : m_bits(0), m_persistenceInfo(nullptr) {}
 
-  inline void setRecentlyUsed() {
-    HostAsm::atomicSetBits(m_bits, RECENTLY_USED_BITS);
-  }
+  inline void setRecentlyUsed() { m_bits |= RECENTLY_USED_BITS; }
 
-  inline void clearRecentlyUsed() {
-    HostAsm::atomicClearBits(m_bits, RECENTLY_USED_BITS);
-  }
+  inline void clearRecentlyUsed() { m_bits &= ~RECENTLY_USED_BITS; }
 
   inline bool testRecentlyUsed() const {
     return (m_bits & RECENTLY_USED_BITS) == RECENTLY_USED_BITS;
@@ -55,9 +55,9 @@ class CPPCACHE_EXPORT LRUEntryProperties {
     return (m_bits & EVICTED_BITS) == EVICTED_BITS;
   }
 
-  inline void setEvicted() { HostAsm::atomicSetBits(m_bits, EVICTED_BITS); }
+  inline void setEvicted() { m_bits |= EVICTED_BITS; }
 
-  inline void clearEvicted() { HostAsm::atomicClearBits(m_bits, EVICTED_BITS); }
+  inline void clearEvicted() { m_bits &= ~EVICTED_BITS; }
 
   inline void* getPersistenceInfo() const { return m_persistenceInfo; }
 
@@ -70,10 +70,12 @@ class CPPCACHE_EXPORT LRUEntryProperties {
   inline LRUEntryProperties(bool noInit) {}
 
  private:
-  volatile uint32_t m_bits;
+  std::atomic<uint32_t> m_bits;
   void* m_persistenceInfo;
 };
 
+using util::concurrent::spinlock_mutex;
+
 /**
  * @brief Maintains a list of entries returning them through head in
  * approximate LRU order. The <code>TEntry</code> template argument
@@ -92,7 +94,7 @@ class LRUList {
   class LRUListNode {
    public:
     inline LRUListNode(const LRUListEntryPtr& entry)
-        : m_entry(entry), m_nextLRUListNode(NULL) {}
+        : m_entry(entry), m_nextLRUListNode(nullptr) {}
 
     inline ~LRUListNode() {}
 
@@ -104,7 +106,7 @@ class LRUList {
       m_nextLRUListNode = next;
     }
 
-    inline void clearNextLRUListNode() { m_nextLRUListNode = NULL; }
+    inline void clearNextLRUListNode() { m_nextLRUListNode = nullptr; }
 
    private:
     LRUListEntryPtr m_entry;
@@ -140,10 +142,10 @@ class LRUList {
    * @brief return the head entry in the list,
    * and removing it from the list.
    */
-  LRUListNode* getHeadNode(bool& isLast);
+  LRUListNode* getHeadNode(bool* isLast);
 
-  SpinLock m_headLock;
-  SpinLock m_tailLock;
+  spinlock_mutex m_headLock;
+  spinlock_mutex m_tailLock;
 
   LRUListNode* m_headNode;
   LRUListNode* m_tailNode;