gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/ipc/cgo/sem/queue/test/queue32-pop/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "gitee.com/sy_183/go-common/ipc/cgo/sem/queue" 6 "gitee.com/sy_183/go-common/ipc/cgo/sem/queue/test" 7 shmPkg "gitee.com/sy_183/go-common/ipc/cgo/shm" 8 "os" 9 "syscall" 10 "time" 11 "unsafe" 12 ) 13 14 func main() { 15 shm, err := shmPkg.Open(test.ShmName, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0644) 16 if err != nil { 17 panic(err) 18 } 19 defer shm.Close() 20 if err = shm.Truncate(int64(test.ShmSize)); err != nil { 21 panic(err) 22 } 23 if err = shm.Mmap(0, int(test.ShmSize), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED); err != nil { 24 panic(err) 25 } 26 data := shm.Data() 27 q := (*queue.Queue32)(unsafe.Pointer(&data[0])) 28 data = data[unsafe.Sizeof(queue.Queue32View{}):] 29 q.Init(uint32(len(data))) 30 31 okCount, failedCount := -1, -1 32 _ = failedCount 33 for { 34 //view, ok := q.TryUse(test.AllocSize) 35 //if !ok { 36 // if failedCount++; failedCount%test.PrintInterval == 0 { 37 // fmt.Printf("use failed %d\n", failedCount) 38 // } 39 // if test.PopSleep != 0 { 40 // time.Sleep(test.PopSleep) 41 // } 42 // continue 43 //} 44 view, err := q.Use(test.AllocSize, nil) 45 if err != nil { 46 panic(err) 47 } 48 //prefix, suffix := view.Slice(data) 49 //var s string 50 //if j := bytes.IndexByte(prefix, 0); j > 0 { 51 // s = string(prefix[:j]) 52 //} else if j = bytes.IndexByte(suffix, 0); j > 0 { 53 // s = string(bytes.Join([][]byte{prefix, suffix[:0]}, nil)) 54 //} 55 //if i%test.PrintInterval == 0 { 56 // fmt.Println(s) 57 //} 58 view.Post(queue.PostModeCanAlloc, nil) 59 if okCount++; okCount%test.PrintInterval == 0 { 60 fmt.Printf("use success %d\n", okCount) 61 } 62 if test.PopSleep != 0 { 63 time.Sleep(test.PopSleep) 64 } 65 } 66 }