gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/ipc/cgo/sem/queue/test/queue32-push/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_RDWR, 0644) 16 if err != nil { 17 panic(err) 18 } 19 defer shm.Close() 20 if err = shm.Mmap(0, int(test.ShmSize), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED); err != nil { 21 panic(err) 22 } 23 data := shm.Data() 24 q := (*queue.Queue32)(unsafe.Pointer(&data[0])) 25 data = data[unsafe.Sizeof(queue.Queue32View{}):] 26 27 okCount, failedCount := -1, -1 28 _ = failedCount 29 for { 30 //view, ok := q.TryAlloc(test.AllocSize) 31 //if !ok { 32 // if failedCount++; failedCount%test.PrintInterval == 0 { 33 // fmt.Printf("alloc failed %d\n", failedCount) 34 // } 35 // if test.PushSleep != 0 { 36 // time.Sleep(test.PushSleep) 37 // } 38 // continue 39 //} 40 view, err := q.Alloc(test.AllocSize, nil) 41 if err != nil { 42 panic(err) 43 } 44 //prefix, suffix := view.Slice(data) 45 //s := fmt.Sprintf("alloc %d", okCount+1) 46 //if n := copy(prefix, s); n < len(s) { 47 // suffix[copy(suffix, s[n:])] = 0 48 //} else if len(prefix) == n { 49 // suffix[0] = 0 50 //} else { 51 // prefix[n] = 0 52 //} 53 view.Post(queue.PostModeCanUse, nil) 54 if okCount++; okCount%test.PrintInterval == 0 { 55 fmt.Printf("alloc success %d\n", okCount) 56 } 57 if test.PushSleep != 0 { 58 time.Sleep(test.PushSleep) 59 } 60 } 61 }