From 3766acf3262235e825fb90f75266fc9de01d8b8e Mon Sep 17 00:00:00 2001
From: chenshijun <chenshijun@aiotlink.com>
Date: 星期四, 16 四月 2020 10:03:27 +0800
Subject: [PATCH] 根据github的go-queue作者的源码修改

---
 shmqueue.go |   30 +++++++++++++++++++++---------
 1 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/shmqueue.go b/shmqueue.go
index 8c5b3a4..bf6dcfd 100644
--- a/shmqueue.go
+++ b/shmqueue.go
@@ -12,7 +12,7 @@
 )
 
 const (
-	TimePeriodPutOrGet = time.Duration(1) * time.Microsecond
+	TimePeriodPutOrGet = time.Duration(5) * time.Microsecond
 )
 
 //Element info
@@ -146,14 +146,14 @@
 func (eqi *EsQueueInfo) QueueSize() int {
 	var putPos, getPos uint32
 	var quantity uint32
+
 	getPos = atomic.LoadUint32(&eqi.Queue.getPos)
 	putPos = atomic.LoadUint32(&eqi.Queue.putPos)
 
 	if putPos >= getPos {
 		quantity = putPos - getPos
 	} else {
-		//quantity = q.capMod + (putPos - getPos)
-		quantity = (eqi.Queue.capMod + (putPos - getPos)) % eqi.Queue.capMod
+		quantity = eqi.Queue.capMod + (putPos - getPos)
 	}
 
 	return int(quantity)
@@ -164,7 +164,8 @@
 	var elems []ElemInfo
 
 	for i := 0; i < len(eqi.EsCaches); i++ {
-		if eqi.EsCaches[i].value.PicId != 0 && eqi.EsCaches[i].value.InfoId != 0 {
+		if eqi.EsCaches[i].value.PicId != -1 && eqi.EsCaches[i].value.InfoId != -1 &&
+			eqi.EsCaches[i].value.PicId != 0 && eqi.EsCaches[i].value.InfoId != 0 {
 			elems = append(elems, eqi.EsCaches[i].value)
 		}
 	}
@@ -184,13 +185,12 @@
 	if putPos >= getPos {
 		posCnt = putPos - getPos
 	} else {
-		//posCnt = capMod + (putPos - getPos)
-		posCnt = (capMod + (putPos - getPos)) % capMod
+		posCnt = capMod + (putPos - getPos)
 	}
 
 	//todo
-	//if posCnt >= capMod-1 {
-	if posCnt >= capMod {
+	if posCnt >= capMod-1 {
+	//if posCnt >= capMod {
 		runtime.Gosched()
 		return false, int(posCnt)
 	}
@@ -204,6 +204,8 @@
 
 	cache = &(eqi.EsCaches[putPosNew&capMod])
 
+	//tryMax := 100
+	//tryCount := 0
 	for {
 		getNo := atomic.LoadUint32(&cache.getNo)
 		putNo := atomic.LoadUint32(&cache.putNo)
@@ -214,6 +216,10 @@
 		} else {
 			runtime.Gosched()
 		}
+		//tryCount++
+		//if tryCount >= tryMax {
+		//	return false, int(posCnt)
+		//}
 	}
 }
 
@@ -295,17 +301,23 @@
 
 	cache = &(eqi.EsCaches[getPosNew&capMod])
 
+	//tryMax := 100
+	//tryCount := 0
 	for {
 		getNo := atomic.LoadUint32(&cache.getNo)
 		putNo := atomic.LoadUint32(&cache.putNo)
 		if getPosNew == getNo && getNo == putNo-eqi.Queue.capacity {
 			val := cache.value
-			cache.value = ElemInfo{PicId: 0}
+			cache.value = ElemInfo{PicId: 0, InfoId:0}
 			atomic.AddUint32(&cache.getNo, eqi.Queue.capacity)
 			return val, true, int(posCnt - 1)
 		} else {
 			runtime.Gosched()
 		}
+		//tryCount++
+		//if tryCount >= tryMax {
+		//	return ElemInfo{}, false, int(posCnt)
+		//}
 	}
 }
 

--
Gitblit v1.8.0