From 1df33ffacf98f09a33969a8c8ed4078247524d29 Mon Sep 17 00:00:00 2001
From: chenshijun <chenshijun@aiotlink.com>
Date: 星期五, 09 十月 2020 14:27:45 +0800
Subject: [PATCH] attach增加超时

---
 shmqueue.go |   28 +++++++++++-----------------
 1 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/shmqueue.go b/shmqueue.go
index bf6dcfd..9c4f16c 100644
--- a/shmqueue.go
+++ b/shmqueue.go
@@ -3,7 +3,7 @@
 import (
 	"context"
 	"fmt"
-	"github.com/gen2brain/shm"
+	shm "basic.com/valib/goshm.git"
 	"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)
-		//}
 	}
 }
 
@@ -301,8 +301,6 @@
 
 	cache = &(eqi.EsCaches[getPosNew&capMod])
 
-	//tryMax := 100
-	//tryCount := 0
 	for {
 		getNo := atomic.LoadUint32(&cache.getNo)
 		putNo := atomic.LoadUint32(&cache.putNo)
@@ -314,10 +312,6 @@
 		} else {
 			runtime.Gosched()
 		}
-		//tryCount++
-		//if tryCount >= tryMax {
-		//	return ElemInfo{}, false, int(posCnt)
-		//}
 	}
 }
 

--
Gitblit v1.8.0