From 03d31805824ac16fc4c3b3f24bc0de8399d1e2e1 Mon Sep 17 00:00:00 2001 From: hslam <791874158@qq.com> Date: Mon, 30 Nov 2020 18:04:30 +0800 Subject: [PATCH] update shm --- shm_unix.go | 33 +++++++++++++++++++++++++-------- shm_unix_test.go | 12 ++++++------ shm_windows.go | 4 ++-- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/shm_unix.go b/shm_unix.go index 7685de2..eebe8bf 100644 --- a/shm_unix.go +++ b/shm_unix.go @@ -12,22 +12,39 @@ import ( const ( // IPC_CREAT creates if key is nonexistent - IPC_CREAT = 00001000 + IPC_CREAT = 01000 - //IPC_RMID removes identifier + // IPC_EXCL fails if key exists. + IPC_EXCL = 02000 + + // IPC_NOWAIT returns error no wait. + IPC_NOWAIT = 04000 + + // IPC_PRIVATE is private key + IPC_PRIVATE = 00000 + + // SEM_UNDO sets up adjust on exit entry + SEM_UNDO = 010000 + + // IPC_RMID removes identifier IPC_RMID = 0 + // IPC_SET sets ipc_perm options. + IPC_SET = 1 + // IPC_STAT gets ipc_perm options. + IPC_STAT = 2 ) // GetAt calls the shmget and shmat system call. -func GetAt(key int, size int, shmFlg int) (uintptr, []byte, error) { +func GetAt(key int, size int, shmFlg int) (int, []byte, error) { if shmFlg == 0 { shmFlg = IPC_CREAT | 0600 } - shmid, _, errno := syscall.Syscall(SYS_SHMGET, uintptr(key), uintptr(validSize(int64(size))), uintptr(shmFlg)) - if int(shmid) < 0 { + r1, _, errno := syscall.Syscall(SYS_SHMGET, uintptr(key), uintptr(validSize(int64(size))), uintptr(shmFlg)) + shmid := int(r1) + if shmid < 0 { return 0, nil, syscall.Errno(errno) } - shmaddr, _, errno := syscall.Syscall(SYS_SHMAT, shmid, 0, uintptr(shmFlg)) + shmaddr, _, errno := syscall.Syscall(SYS_SHMAT, uintptr(shmid), 0, uintptr(shmFlg)) if int(shmaddr) < 0 { Remove(shmid) return 0, nil, syscall.Errno(errno) @@ -51,8 +68,8 @@ func Dt(b []byte) error { } // Remove removes the shm with the given id. -func Remove(shmid uintptr) error { - r1, _, errno := syscall.Syscall(SYS_SHMCTL, shmid, IPC_RMID, 0) +func Remove(shmid int) error { + r1, _, errno := syscall.Syscall(SYS_SHMCTL, uintptr(shmid), IPC_RMID, 0) if int(r1) < 0 { return syscall.Errno(errno) } diff --git a/shm_unix_test.go b/shm_unix_test.go index 81c132f..935929c 100644 --- a/shm_unix_test.go +++ b/shm_unix_test.go @@ -26,10 +26,10 @@ func TestGetAt(t *testing.T) { defer Remove(shmid) defer Dt(data) copy(data, context) - time.Sleep(time.Second * 2) + time.Sleep(time.Millisecond * 200) close(done) }() - time.Sleep(time.Second) + time.Sleep(time.Millisecond * 100) key, err := ftok.Ftok("/tmp", 0x22) if err != nil { panic(err) @@ -60,10 +60,10 @@ func TestGetAtZeroFlag(t *testing.T) { defer Remove(shmid) defer Dt(data) copy(data, context) - time.Sleep(time.Second * 2) + time.Sleep(time.Millisecond * 200) close(done) }() - time.Sleep(time.Second) + time.Sleep(time.Millisecond * 100) key, err := ftok.Ftok("/tmp", 0x22) if err != nil { panic(err) @@ -98,10 +98,10 @@ func TestOpen(t *testing.T) { } defer mmap.Munmap(data) copy(data, []byte("Hello World")) - time.Sleep(time.Second * 2) + time.Sleep(time.Millisecond * 200) close(done) }() - time.Sleep(time.Second) + time.Sleep(time.Millisecond * 100) fd, err := Open("shared", O_RDONLY, 0600) if err != nil { panic(err) diff --git a/shm_windows.go b/shm_windows.go index bbc2951..f895c50 100644 --- a/shm_windows.go +++ b/shm_windows.go @@ -26,7 +26,7 @@ func Unlink(name string) error { } // GetAt calls the shmget and shmat system call. -func GetAt(key int, size int, shmFlg int) (uintptr, []byte, error) { +func GetAt(key int, size int, shmFlg int) (int, []byte, error) { return 0, nil, errors.New("not supported") } @@ -36,7 +36,7 @@ func Dt(b []byte) error { } // Remove removes the shm with the given id. -func Remove(shmid uintptr) error { +func Remove(shmid int) error { return errors.New("not supported") }