mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-05 08:37:31 -04:00
unix: add Sysv shared memory support
Implements proposed API from https://golang.org/issue/46084. I chose `SysvShmDesc` since it's a clearer name than `SysvShm`. Initially supports Darwin and Linux. Solaris support has a blocker (https://golang.org/issue/46084#issuecomment-836980018) For golang/go#46084 Change-Id: Ied0f768a74c448254adc3315348417825a7ec63e GitHub-Last-Rev: befbd7af6b2f838753ac163ef387cebdb7957467 GitHub-Pull-Request: golang/sys#110 Reviewed-on: https://go-review.googlesource.com/c/sys/+/327830 Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
committed by
Tobias Klauser
parent
39ccf1dd6f
commit
969570ce7c
@@ -0,0 +1,74 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build (darwin && amd64) || linux
|
||||
// +build darwin,amd64 linux
|
||||
|
||||
package unix_test
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func TestSysvSharedMemory(t *testing.T) {
|
||||
// create ipc
|
||||
id, err := unix.SysvShmGet(unix.IPC_PRIVATE, 1024, unix.IPC_CREAT|unix.IPC_EXCL|0o600)
|
||||
|
||||
// ipc isn't implemented on android, should fail
|
||||
if runtime.GOOS == "android" {
|
||||
if err != unix.ENOSYS {
|
||||
t.Fatalf("expected android to fail, but it didn't")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("SysvShmGet: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
_, err := unix.SysvShmCtl(id, unix.IPC_RMID, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Remove failed: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// attach
|
||||
b1, err := unix.SysvShmAttach(id, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Attach: %v", err)
|
||||
}
|
||||
|
||||
if len(b1) != 1024 {
|
||||
t.Fatalf("b1 len = %v, want 1024", len(b1))
|
||||
}
|
||||
|
||||
b1[42] = 'x'
|
||||
|
||||
// attach again
|
||||
b2, err := unix.SysvShmAttach(id, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Attach: %v", err)
|
||||
}
|
||||
|
||||
if len(b2) != 1024 {
|
||||
t.Fatalf("b2 len = %v, want 1024", len(b1))
|
||||
}
|
||||
|
||||
b2[43] = 'y'
|
||||
if b2[42] != 'x' || b1[43] != 'y' {
|
||||
t.Fatalf("shared memory isn't shared")
|
||||
}
|
||||
|
||||
// detach
|
||||
if err = unix.SysvShmDetach(b2); err != nil {
|
||||
t.Fatalf("Detach: %v", err)
|
||||
}
|
||||
|
||||
if b1[42] != 'x' || b1[43] != 'y' {
|
||||
t.Fatalf("shared memory was invalidated")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user