init code of share memory
This commit is contained in:
parent
6a1c42e22b
commit
1899546ba4
|
|
@ -0,0 +1,52 @@
|
|||
package sharememory
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 打开文件
|
||||
file, err := os.OpenFile("testfile.txt", os.O_RDWR|os.O_CREATE, 0o666)
|
||||
if err != nil {
|
||||
fmt.Println("Error opening file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 加独占锁
|
||||
fmt.Println("Acquiring exclusive lock...")
|
||||
err = unix.Flock(int(file.Fd()), unix.LOCK_EX)
|
||||
if err != nil {
|
||||
fmt.Println("Error acquiring exclusive lock:", err)
|
||||
return
|
||||
}
|
||||
defer unix.Flock(int(file.Fd()), unix.LOCK_UN) // 释放锁
|
||||
|
||||
fmt.Println("Exclusive lock acquired. Writing to file...")
|
||||
// 这里可以添加写文件的逻辑
|
||||
fmt.Println("Writing complete.")
|
||||
|
||||
// 打开文件
|
||||
file, err = os.OpenFile("testfile.txt", os.O_RDONLY, 0o666)
|
||||
if err != nil {
|
||||
fmt.Println("Error opening file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 加共享锁
|
||||
fmt.Println("Acquiring shared lock...")
|
||||
err = unix.Flock(int(file.Fd()), unix.LOCK_SH)
|
||||
if err != nil {
|
||||
fmt.Println("Error acquiring shared lock:", err)
|
||||
return
|
||||
}
|
||||
defer unix.Flock(int(file.Fd()), unix.LOCK_UN) // 释放锁
|
||||
|
||||
fmt.Println("Shared lock acquired. Reading from file...")
|
||||
// 这里可以添加读文件的逻辑
|
||||
fmt.Println("Reading complete.")
|
||||
}
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
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
|
||||
}
|
||||
Loading…
Reference in New Issue