From 78fcef38239d7eacb44dde2f75dbec93594eb57b Mon Sep 17 00:00:00 2001 From: hslam <791874158@qq.com> Date: Mon, 30 Nov 2020 18:06:51 +0800 Subject: [PATCH] update msg --- msg_test.go | 4 ++-- msg_unix.go | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/msg_test.go b/msg_test.go index fb11164..9a23875 100644 --- a/msg_test.go +++ b/msg_test.go @@ -24,10 +24,10 @@ func TestMsg(t *testing.T) { if err != nil { panic(err) } - 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) diff --git a/msg_unix.go b/msg_unix.go index df59a58..4af3e3c 100644 --- a/msg_unix.go +++ b/msg_unix.go @@ -11,10 +11,26 @@ import ( const ( // IPC_CREAT creates if key is nonexistent - IPC_CREAT = 00001000 + IPC_CREAT = 01000 + + // 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 maxText = 8192 ) @@ -28,22 +44,23 @@ type message struct { } // Get calls the msgget system call. -func Get(key int, msgflg int) (uintptr, error) { - msgid, _, err := syscall.Syscall(syscall.SYS_MSGGET, uintptr(key), uintptr(msgflg), 0) - if int(msgid) < 0 { - return 0, syscall.Errno(err) +func Get(key int, msgflg int) (int, error) { + r1, _, err := syscall.Syscall(syscall.SYS_MSGGET, uintptr(key), uintptr(msgflg), 0) + msgid := int(r1) + if msgid < 0 { + return msgid, syscall.Errno(err) } return msgid, nil } // Snd calls the msgsnd system call. -func Snd(msgid uintptr, msgType uint, msgText []byte, flags uint) error { +func Snd(msgid int, msgType uint, msgText []byte, flags int) error { if len(msgText) > maxText { return ErrTooLong } m := message{Type: msgType} copy(m.Text[:], msgText) - _, _, err := syscall.Syscall6(syscall.SYS_MSGSND, msgid, uintptr(unsafe.Pointer(&m)), uintptr(len(msgText)), uintptr(flags), 0, 0) + _, _, err := syscall.Syscall6(syscall.SYS_MSGSND, uintptr(msgid), uintptr(unsafe.Pointer(&m)), uintptr(len(msgText)), uintptr(flags), 0, 0) if err != 0 { return err } @@ -51,9 +68,9 @@ func Snd(msgid uintptr, msgType uint, msgText []byte, flags uint) error { } // Rcv calls the msgrcv system call. -func Rcv(msgid uintptr, msgType uint, flags uint) ([]byte, error) { +func Rcv(msgid int, msgType uint, flags int) ([]byte, error) { m := message{Type: msgType} - length, _, err := syscall.Syscall6(syscall.SYS_MSGRCV, msgid, uintptr(unsafe.Pointer(&m)), maxText, uintptr(msgType), uintptr(flags), 0) + length, _, err := syscall.Syscall6(syscall.SYS_MSGRCV, uintptr(msgid), uintptr(unsafe.Pointer(&m)), maxText, uintptr(msgType), uintptr(flags), 0) if err != 0 { return nil, err } @@ -61,8 +78,8 @@ func Rcv(msgid uintptr, msgType uint, flags uint) ([]byte, error) { } // Remove removes the message queue with the given id. -func Remove(msgid uintptr) error { - r1, _, errno := syscall.Syscall(syscall.SYS_MSGCTL, msgid, IPC_RMID, 0) +func Remove(msgid int) error { + r1, _, errno := syscall.Syscall(syscall.SYS_MSGCTL, uintptr(msgid), IPC_RMID, 0) if int(r1) < 0 { return syscall.Errno(errno) }