chenshijun
2020-04-01 edf8a5b86f66a5b956ce17b2ba9852680dfc9c86
增加只創建共享内存的接口,删除强制删除的接口
1个文件已修改
51 ■■■■ 已修改文件
shmwrap.go 51 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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)
    }
}
@@ -177,17 +211,4 @@
    }
    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)
        }
    }
    RemoveShmID(shmId)
    return nil
}
}