From d33a69463f1a75134d01191be0b9e1bdd757dd4b Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 30 四月 2021 15:27:59 +0800
Subject: [PATCH] add atomic queue, no lock, unorder.

---
 utest/robust_test.cpp |  122 +++++++++++++++++++++-------------------
 1 files changed, 65 insertions(+), 57 deletions(-)

diff --git a/utest/robust_test.cpp b/utest/robust_test.cpp
index 9384c10..7799aad 100644
--- a/utest/robust_test.cpp
+++ b/utest/robust_test.cpp
@@ -8,52 +8,54 @@
 	eLockerMask = MaskBits(sizeof(int) * 8),
 };
 
-typedef CircularBuffer<int64_t, Allocator<int64_t>> Rcb;
-Rcb *GetRCB(SharedMemory &shm, const int nelem)
-{
-	int cap = nelem + 1;
-	typedef uint64_t Data;
-	auto size = sizeof(Rcb) + sizeof(Data) * cap;
-	void *p = shm.Alloc(size);
-	if (p) {
-		return new (p) Rcb(cap, shm.get_segment_manager());
-	}
-	return nullptr;
-}
-
 void MySleep()
 {
 	std::this_thread::sleep_for(2us);
 }
 
+/////////////////////////////////////////////////////////////////////////////////////////
+
 BOOST_AUTO_TEST_CASE(QueueTest)
 {
+	const int nthread = 100;
+	const uint64_t nmsg = 1000 * 1000 * 10;
+
 	SharedMemory &shm = TestShm();
 	shm.Remove();
-	pid_t pid = getpid();
-	printf("pid : %d\n", pid);
-	auto Access = [](pid_t pid) {
-		char buf[100] = {0};
-		sprintf(buf, "/proc/%d/stat", pid);
-		int r = access(buf, F_OK);
-		printf("access %d\n", r);
-	};
-	Access(pid);
-	Access(pid + 1);
-	// Sleep(10s);
-	// return;
+	// return; /////////////////////////////////////////////////
+	int64_t i64 = 0;
+	char c = 0;
+	for (int i = 0; i < 256; ++i) {
+		c = i;
+		i64 = int64_t(c) << 1;
+		BOOST_CHECK_EQUAL(c, (i64 >> 1));
+		uint64_t u64 = i;
+		BOOST_CHECK_EQUAL((u64 & 255), i);
+	}
 
-	int nelement = 640;
-	auto rcb = GetRCB(shm, nelement);
-	BOOST_CHECK(rcb != nullptr);
-	BOOST_CHECK(rcb->empty());
-	BOOST_CHECK(rcb->push_back(1));
-	BOOST_CHECK(rcb->size() == 1);
+#if 1
+	typedef AtomicQueue<3> Rcb;
+
+	Rcb tmp;
+	BOOST_CHECK(tmp.like_empty());
+	BOOST_CHECK(tmp.push_back(1));
+	BOOST_CHECK(tmp.tail() == 1);
+	BOOST_CHECK(tmp.head() == 0);
 	int64_t d;
-	BOOST_CHECK(rcb->pop_front(d));
-	BOOST_CHECK(rcb->empty());
+	BOOST_CHECK(tmp.pop_front(d));
+	BOOST_CHECK(tmp.like_empty());
+	BOOST_CHECK(tmp.head() == 1);
+	BOOST_CHECK(tmp.tail() == 1);
 
-	const uint64_t nmsg = 1000 * 1000 * 1;
+	ShmObject<Rcb> rcb(shm, "test_rcb");
+#else
+	typedef Circular<int64_t> Rcb;
+	ShmObject<Rcb> rcb(shm, "test_rcb", 64, shm.get_segment_manager());
+#endif
+
+	const int nsize = sizeof(Rcb);
+
+	bool try_more = false;
 	uint64_t correct_total = nmsg * (nmsg - 1) / 2;
 	std::atomic<uint64_t> total(0);
 	std::atomic<uint64_t> nwrite(0);
@@ -61,7 +63,7 @@
 	auto Writer = [&]() {
 		uint64_t n = 0;
 		while ((n = nwrite++) < nmsg) {
-			while (!rcb->push_back(n)) {
+			while (!rcb->push_back(n, try_more)) {
 				// MySleep();
 			}
 			++writedone;
@@ -71,11 +73,11 @@
 	auto Reader = [&]() {
 		while (nread.load() < nmsg) {
 			int64_t d;
-			if (rcb->pop_front(d)) {
+			if (rcb->pop_front(d, try_more)) {
 				++nread;
 				total += d;
 			} else {
-				MySleep();
+				// MySleep();
 			}
 		}
 	};
@@ -89,21 +91,25 @@
 			next += 1s;
 			auto w = writedone.load();
 			auto r = nread.load();
-			printf("write: %6ld, spd: %6ld,  read: %6ld, spd: %6ld , queue size: %d\n", w, w - lw, r, r - lr, rcb->size());
+			printf("write: %6ld, spd: %6ld,  read: %6ld, spd: %6ld\n",
+			       w, w - lw, r, r - lr);
 			lw = w;
 			lr = r;
 		} while (nread.load() < nmsg);
 	};
 
-	ThreadManager threads;
-	boost::timer::auto_cpu_timer timer;
-	printf("Testing Robust Buffer, msgs %ld, queue size: %d \n", nmsg, nelement);
-	threads.Launch(status);
-	for (int i = 0; i < 10; ++i) {
-		threads.Launch(Reader);
-		threads.Launch(Writer);
+	std::thread st(status);
+	{
+		ThreadManager threads;
+		boost::timer::auto_cpu_timer timer;
+		printf("Testing Robust Buffer, msgs %ld, queue size: %d, threads: %d \n", nmsg, Rcb::capacity, nthread);
+		for (int i = 0; i < nthread; ++i) {
+			threads.Launch(Reader);
+			threads.Launch(Writer);
+		}
+		threads.WaitAll();
 	}
-	threads.WaitAll();
+	st.join();
 	printf("total: %ld, expected: %ld\n", total.load(), correct_total);
 	BOOST_CHECK_EQUAL(total.load(), correct_total);
 }
@@ -137,9 +143,7 @@
 	}
 
 	{
-		boost::timer::auto_cpu_timer timer;
 		const int ntimes = 1000 * 1000;
-		printf("test lock/unlock %d times: ", ntimes);
 		RobustMutex mutex;
 		auto Lock = [&]() {
 			for (int i = 0; i < ntimes; ++i) {
@@ -147,16 +151,20 @@
 				mutex.unlock();
 			}
 		};
-		std::thread t1(Lock), t2(Lock);
-		t1.join();
-		t2.join();
-	}
 
-	auto MSFromNow = [](const int ms) {
-		using namespace boost::posix_time;
-		ptime cur = boost::posix_time::microsec_clock::universal_time();
-		return cur + millisec(ms);
-	};
+		{
+			boost::timer::auto_cpu_timer timer;
+			printf("test lock/unlock %d times: ", ntimes);
+			Lock();
+		}
+		{
+			boost::timer::auto_cpu_timer timer;
+			printf("test lock/unlock %d times, 2 thread: ", ntimes);
+			std::thread t1(Lock), t2(Lock);
+			t1.join();
+			t2.join();
+		}
+	}
 
 	auto TryLock = [&]() {
 		if (mtx->try_lock()) {

--
Gitblit v1.8.0