From 1644fdbc4aea52a63e4152ad0f5ec6a2b3d7e6ec Mon Sep 17 00:00:00 2001
From: chenshijun <chenshijun@aiotlink.com>
Date: 星期四, 02 四月 2020 13:40:46 +0800
Subject: [PATCH] shmqueue 修改参数使用的标准化

---
 shmwrap.go |   56 ++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/shmwrap.go b/shmwrap.go
index 7975d61..8dad1d7 100644
--- a/shmwrap.go
+++ b/shmwrap.go
@@ -24,6 +24,16 @@
     return data, id, nil
 }
 
+// NewBlockOnly shm block with size, no attach
+func NewBlockOnly(size int, key int) (int, error) {
+    id, err := shm.Get(key, size, shm.IPC_CREAT|0666)
+    if err != nil || id == -1 {
+        fmt.Printf("NewBlock Get:%x, %v\n", key, err)
+        return -1, err
+    }
+    return id, nil
+}
+
 // AttachBlock attach exist shm
 func AttachBlock(key int) ([]byte, int, error) {
     id, err := shm.Get(key, 0, 0)
@@ -91,6 +101,28 @@
     return data, id
 }
 
+// CreateShmOnly create shm block with size, only space, no padding, return id(int)
+// context for quit
+func CreateShmOnly(ctx context.Context, size int, key int) int {
+    id, err := NewBlockOnly(size, key)
+    if err != nil {
+    loopB:
+        for {
+            select {
+            case <-ctx.Done():
+                return -1
+            default:
+                if err == nil {
+                    break loopB
+                }
+                time.Sleep(time.Millisecond)
+                id, err = NewBlockOnly(size, key)
+            }
+        }
+    }
+    return id
+}
+
 // CreatePaddingShm create padding shm block with size, return data-with-padding([]byte), id(int)
 // context for quit, padding for fill raw shm block
 func CreatePaddingShm(ctx context.Context, size int, key int, padding []byte) ([]byte, int) {
@@ -139,10 +171,11 @@
 
 // RemoveShmKey Remove shmkey
 func RemoveShmKey(shmkey int) error {
-    _, id, err := AttachBlock(shmkey)
+    d, id, err := AttachBlock(shmkey)
     if err != nil {
         return err
     } else {
+        Detach(d)
         return shm.Rm(id)
     }
 }
@@ -153,6 +186,7 @@
     if err != nil {
         return err
     } else {
+        Detach(d)
         return Detach(d)
     }
 }
@@ -179,15 +213,13 @@
     return int(idDs.Nattch), nil
 }
 
-func ForceRemoveShm(data []byte, shmId int) error {
-    count, err := ShmAttachNum(shmId)
-    if err != nil{
-        return err
-    }else{
-        for i:=0; i < count; i++{
-            Detach(data)
-        }
+func GetKeyById(shmId int) (int,error) {
+    var idDs shm.IdDs
+
+    _, err := shm.Ctl(shmId, shm.IPC_STAT, &idDs)
+    if err != nil {
+        return 0, err
     }
-    RemoveShmID(shmId)
-    return nil
-}
+
+    return int(idDs.Perm.Key), nil
+}
\ No newline at end of file

--
Gitblit v1.8.0