99 lines
3.4 KiB
Go
99 lines
3.4 KiB
Go
package sharememory
|
|
|
|
import (
|
|
"fmt"
|
|
"unsafe"
|
|
|
|
"modelRT/orm"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// CreateShareMemory defines a function to create a shared memory
|
|
func CreateShareMemory(key uintptr, structSize uintptr) (uintptr, error) {
|
|
// logger := logger.GetLoggerInstance()
|
|
// create shared memory
|
|
shmID, _, err := unix.Syscall(unix.SYS_SHMGET, key, structSize, unix.IPC_CREAT|0o666)
|
|
if err != 0 {
|
|
// logger.Error(fmt.Sprintf("create shared memory by key %v failed:", key), zap.Error(err))
|
|
return 0, fmt.Errorf("create shared memory failed:%w", err)
|
|
}
|
|
|
|
// attach shared memory
|
|
shmAddr, _, err := unix.Syscall(unix.SYS_SHMAT, shmID, 0, 0)
|
|
if err != 0 {
|
|
// logger.Error(fmt.Sprintf("attach shared memory by shmID %v failed:", shmID), zap.Error(err))
|
|
return 0, fmt.Errorf("attach shared memory failed:%w", err)
|
|
}
|
|
return shmAddr, nil
|
|
}
|
|
|
|
// ReadComponentFromShareMemory defines a function to read component value from shared memory
|
|
func ReadComponentFromShareMemory(key uintptr, componentInfo *orm.Component) error {
|
|
structSize := unsafe.Sizeof(orm.Component{})
|
|
shmID, _, err := unix.Syscall(unix.SYS_SHMGET, key, uintptr(int(structSize)), 0o666)
|
|
if err != 0 {
|
|
return fmt.Errorf("get shared memory failed:%w", err)
|
|
}
|
|
|
|
shmAddr, _, err := unix.Syscall(unix.SYS_SHMAT, shmID, 0, 0)
|
|
if err != 0 {
|
|
return fmt.Errorf("attach shared memory failed:%w", err)
|
|
}
|
|
|
|
// 读取共享内存中的数据
|
|
componentInfo = (*orm.Component)(unsafe.Pointer(shmAddr + structSize))
|
|
|
|
// Detach shared memory
|
|
unix.Syscall(unix.SYS_SHMDT, shmAddr, 0, 0)
|
|
return nil
|
|
}
|
|
|
|
func WriteComponentInShareMemory(key uintptr, componentInfo *orm.Component) error {
|
|
structSize := unsafe.Sizeof(orm.Component{})
|
|
shmID, _, err := unix.Syscall(unix.SYS_SHMGET, key, uintptr(int(structSize)), 0o666)
|
|
if err != 0 {
|
|
return fmt.Errorf("get shared memory failed:%w", err)
|
|
}
|
|
|
|
shmAddr, _, err := unix.Syscall(unix.SYS_SHMAT, shmID, 0, 0)
|
|
if err != 0 {
|
|
return fmt.Errorf("attach shared memory failed:%w", err)
|
|
}
|
|
|
|
obj := (*orm.Component)(unsafe.Pointer(shmAddr + unsafe.Sizeof(structSize)))
|
|
fmt.Println(obj)
|
|
obj.ComponentType = componentInfo.ComponentType
|
|
|
|
// id integer NOT NULL DEFAULT nextval('component_id_seq'::regclass),
|
|
// global_uuid uuid NOT NULL DEFAULT gen_random_uuid(),
|
|
// nspath character varying(32) COLLATE pg_catalog."default",
|
|
// tag character varying(32) COLLATE pg_catalog."default" NOT NULL,
|
|
// name character varying(64) COLLATE pg_catalog."default" NOT NULL,
|
|
// description character varying(512) COLLATE pg_catalog."default" NOT NULL DEFAULT ''::character varying,
|
|
// grid character varying(64) COLLATE pg_catalog."default" NOT NULL,
|
|
// zone character varying(64) COLLATE pg_catalog."default" NOT NULL,
|
|
// station character varying(64) COLLATE pg_catalog."default" NOT NULL,
|
|
// type integer NOT NULL,
|
|
// in_service boolean DEFAULT false,
|
|
// state integer NOT NULL DEFAULT 0,
|
|
// connected_bus jsonb NOT NULL DEFAULT '{}'::jsonb,
|
|
// label jsonb NOT NULL DEFAULT '{}'::jsonb,
|
|
// context jsonb NOT NULL DEFAULT '{}'::jsonb,
|
|
// page_id integer NOT NULL,
|
|
// op integer NOT NULL DEFAULT '-1'::integer,
|
|
// ts timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
unix.Syscall(unix.SYS_SHMDT, shmAddr, 0, 0)
|
|
return nil
|
|
}
|
|
|
|
// DeleteShareMemory defines a function to delete shared memory
|
|
func DeleteShareMemory(key uintptr) error {
|
|
_, _, err := unix.Syscall(unix.SYS_SHM_UNLINK, key, 0, 0o666)
|
|
if err != 0 {
|
|
return fmt.Errorf("get shared memory failed:%w", err)
|
|
}
|
|
return nil
|
|
}
|