From 2de7ebf4516e58a8fa959977a51dccd22823c0e0 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 06 五月 2021 18:58:35 +0800
Subject: [PATCH] fix node recursive lock.

---
 src/robust.h |   17 ++++++++++-------
 1 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/robust.h b/src/robust.h
index b7459ad..b29fce5 100644
--- a/src/robust.h
+++ b/src/robust.h
@@ -19,6 +19,7 @@
 #ifndef ROBUST_Q31RCWYU
 #define ROBUST_Q31RCWYU
 
+#include "log.h"
 #include <atomic>
 #include <chrono>
 #include <memory>
@@ -171,7 +172,7 @@
 		} else if (!is_alive(Locker(old))) {
 			r = static_cast<int>(MetaCas(old, Meta(1, this_locker()))) << 1;
 			if (r) {
-				printf("captured locker %ld -> %ld, locker = %d\n", int64_t(Locker(old)), int64_t(this_locker()), r);
+				LOG_DEBUG() << "captured locker " << int64_t(Locker(old)) << " -> " << int64_t(this_locker()) << ", locker = " << r;
 			}
 		}
 		return r;
@@ -312,9 +313,10 @@
 		size_type i = 0;
 		do {
 			auto pos = tail();
-			auto cur = buf[pos].load();
-			r = Empty(cur) && buf[pos].compare_exchange_strong(cur, Enc(d));
-			tail_.compare_exchange_strong(pos, Next(pos));
+			if (tail_.compare_exchange_strong(pos, Next(pos))) {
+				auto cur = buf[pos].load();
+				r = Empty(cur) && buf[pos].compare_exchange_strong(cur, Enc(d));
+			}
 		} while (try_more && !r && ++i < capacity);
 		return r;
 	}
@@ -325,9 +327,10 @@
 		size_type i = 0;
 		do {
 			auto pos = head();
-			cur = buf[pos].load();
-			r = !Empty(cur) && buf[pos].compare_exchange_strong(cur, 0);
-			head_.compare_exchange_strong(pos, Next(pos));
+			if (head_.compare_exchange_strong(pos, Next(pos))) {
+				cur = buf[pos].load();
+				r = !Empty(cur) && buf[pos].compare_exchange_strong(cur, 0);
+			}
 		} while (try_more && !r && ++i < capacity);
 		if (r) { d = Dec(cur); }
 		return r;

--
Gitblit v1.8.0