1
0
mirror of https://github.com/hslam/shm.git synced 2025-04-25 13:48:57 +08:00

update shm

This commit is contained in:
hslam 2020-11-30 18:04:30 +08:00
parent f5d474b6f4
commit 03d3180582
3 changed files with 33 additions and 16 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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")
}