From 82a262ef5cf721e5a236c8b1d2ab3ff92ca33122 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 24 九月 2021 11:40:20 +0800
Subject: [PATCH] bug fixed withintime

---
 shmqueue.go |  121 ++++++++++++++++++++++------------------
 1 files changed, 66 insertions(+), 55 deletions(-)

diff --git a/shmqueue.go b/shmqueue.go
index bf6dcfd..cb2c12a 100644
--- a/shmqueue.go
+++ b/shmqueue.go
@@ -1,9 +1,9 @@
 package shmqueue
 
 import (
+	shm "basic.com/valib/goshm.git"
 	"context"
 	"fmt"
-	"github.com/gen2brain/shm"
 	"reflect"
 	"runtime"
 	"sync/atomic"
@@ -12,7 +12,7 @@
 )
 
 const (
-	TimePeriodPutOrGet = time.Duration(5) * time.Microsecond
+	TimePeriodPutOrGet = time.Duration(5) * time.Millisecond //ms
 )
 
 //Element info
@@ -72,6 +72,9 @@
 	shmLen = shmblocks*uint32(unsafe.Sizeof(datainfo)) + uint32(unsafe.Sizeof(shmstruct))
 
 	data, shmid := CreateRawShm(ctx, int(shmLen), key)
+	if shmid == -1 {
+		return nil
+	}
 	q := bytes2shmEsQueue(data)
 	//init parameters
 	q.capacity = shmblocks
@@ -103,7 +106,10 @@
 func AttachQueue(ctx context.Context, key int) *EsQueueInfo {
 
 	var eqi EsQueueInfo
-	data, shmid := AttachRawShm(ctx, key)
+	data, shmid := AttachRawShmTimeout(ctx, key)
+	if shmid == -1 {
+		return nil
+	}
 	shmdata := bytes2shmEsQueue(data)
 	eqi.EsCaches = ptr2esCache(unsafe.Pointer(&shmdata.cache), int(shmdata.capacity))
 	//fmt.Println("AttachQueue EsCaches:", eqi.EsCaches)
@@ -189,8 +195,8 @@
 	}
 
 	//todo
-	if posCnt >= capMod-1 {
-	//if posCnt >= capMod {
+	//if posCnt >= capMod-1 {
+	if posCnt >= capMod {
 		runtime.Gosched()
 		return false, int(posCnt)
 	}
@@ -204,8 +210,6 @@
 
 	cache = &(eqi.EsCaches[putPosNew&capMod])
 
-	//tryMax := 100
-	//tryCount := 0
 	for {
 		getNo := atomic.LoadUint32(&cache.getNo)
 		putNo := atomic.LoadUint32(&cache.putNo)
@@ -216,10 +220,6 @@
 		} else {
 			runtime.Gosched()
 		}
-		//tryCount++
-		//if tryCount >= tryMax {
-		//	return false, int(posCnt)
-		//}
 	}
 }
 
@@ -252,25 +252,34 @@
 		return ok, qua
 	}
 
-	to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
-	defer to.Stop()
-
-	loopB:
-		for {
-			select {
-			case <-ctx.Done():
-				return false, 0
-			case <-to.C:
-				return false, 0
-			default:
-				ok, qua = eqi.Put(val)
-				if ok {
-					break loopB
-				}
-				time.Sleep(TimePeriodPutOrGet)
-			}
+	count := timeout/int(TimePeriodPutOrGet) + 1
+	for i := 0; i < count; i++ {
+		if ok, qua := eqi.Put(val); ok {
+			return ok, qua
 		}
-	return ok, qua
+		time.Sleep(TimePeriodPutOrGet)
+	}
+	return false, 0
+
+	// 	to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
+	// 	defer to.Stop()
+
+	// loopB:
+	// 	for {
+	// 		select {
+	// 		case <-ctx.Done():
+	// 			return false, 0
+	// 		case <-to.C:
+	// 			return false, 0
+	// 		default:
+	// 			ok, qua = eqi.Put(val)
+	// 			if ok {
+	// 				break loopB
+	// 			}
+	// 			time.Sleep(TimePeriodPutOrGet)
+	// 		}
+	// 	}
+	// 	return ok, qua
 }
 
 // Get 鍗曟鑾峰彇锛屽彲鑳藉け璐�
@@ -301,23 +310,17 @@
 
 	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, InfoId: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)
-		//}
 	}
 }
 
@@ -349,26 +352,34 @@
 	if ok {
 		return val, ok, qua
 	}
-
-	to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
-	defer to.Stop()
-
-	loopB:
-		for {
-			select {
-			case <-ctx.Done():
-				return ElemInfo{}, false, 0
-			case <-to.C:
-				return ElemInfo{}, false, 0
-			default:
-				val, ok, qua = eqi.Get()
-				if ok {
-					break loopB
-				}
-				time.Sleep(TimePeriodPutOrGet)
-			}
+	count := timeout/int(TimePeriodPutOrGet) + 1
+	for i := 0; i < count; i++ {
+		if val, ok, qua := eqi.Get(); ok {
+			return val, ok, qua
 		}
-	return val, ok, qua
+		time.Sleep(TimePeriodPutOrGet)
+	}
+	return ElemInfo{}, false, 0
+
+	// 	to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
+	// 	defer to.Stop()
+
+	// loopB:
+	// 	for {
+	// 		select {
+	// 		case <-ctx.Done():
+	// 			return ElemInfo{}, false, 0
+	// 		case <-to.C:
+	// 			return ElemInfo{}, false, 0
+	// 		default:
+	// 			val, ok, qua = eqi.Get()
+	// 			if ok {
+	// 				break loopB
+	// 			}
+	// 			time.Sleep(TimePeriodPutOrGet)
+	// 		}
+	// 	}
+	// 	return val, ok, qua
 }
 
 // round 鍒版渶杩戠殑2鐨勫�嶆暟

--
Gitblit v1.8.0