| | |
| | | |
| | | //Element info |
| | | type ElemInfo struct { |
| | | ShmId int |
| | | PicId int |
| | | InfoId int |
| | | } |
| | | |
| | | //queue info |
| | |
| | | |
| | | type EsQueueInfo struct { |
| | | EsCaches []esCache |
| | | ShmKey int |
| | | ShmID int |
| | | ShmData []byte |
| | | Queue *esQueue |
| | | ShmKey int |
| | | ShmID int |
| | | ShmData []byte |
| | | Queue *esQueue |
| | | } |
| | | |
| | | // ptr2esCache convert unsafe.Pointer to []esCache |
| | |
| | | q.putPos = 0 |
| | | q.getPos = 0 |
| | | |
| | | fmt.Println("int(q.capacity)",int(q.capacity)) |
| | | fmt.Println("int(q.capacity)", int(q.capacity)) |
| | | eqi.EsCaches = ptr2esCache(unsafe.Pointer(&q.cache), int(q.capacity)) |
| | | fmt.Printf("EsCaches:%p\n", &eqi.EsCaches) |
| | | fmt.Printf("EsCaches[0]:%p\n", &(eqi.EsCaches[0])) |
| | | fmt.Printf("EsCaches[1]:%p\n", &(eqi.EsCaches[1])) |
| | | |
| | | fmt.Println("int(q.capacity)",int(q.capacity)) |
| | | fmt.Println("int(q.capacity)", int(q.capacity)) |
| | | for i := 0; i < int(q.capacity); i++ { |
| | | fmt.Println("i", i) |
| | | fmt.Printf("EsCaches[%d]:%p\n", i, &(eqi.EsCaches[i])) |
| | | fmt.Printf("EsCaches[%d].getNo:%p\n", i, &(eqi.EsCaches[i].getNo)) |
| | | fmt.Printf("EsCaches[%d].putNo:%p\n", i, &(eqi.EsCaches[i].putNo)) |
| | | fmt.Printf("EsCaches[%d]:%p\n", i, &(eqi.EsCaches[i])) |
| | | fmt.Printf("EsCaches[%d].getNo:%p\n", i, &(eqi.EsCaches[i].getNo)) |
| | | fmt.Printf("EsCaches[%d].putNo:%p\n", i, &(eqi.EsCaches[i].putNo)) |
| | | |
| | | eqi.EsCaches[i].getNo = uint32(i) |
| | | eqi.EsCaches[i].putNo = uint32(i) |
| | | fmt.Printf("cache.putNo:%p\n", &(eqi.EsCaches[i].putNo)) |
| | | fmt.Printf("cache.putNo:%p\n", &(eqi.EsCaches[i].putNo)) |
| | | fmt.Printf("cache.putNo:%p\n", &(eqi.EsCaches[i].putNo)) |
| | | fmt.Printf("cache.putNo:%p\n", &(eqi.EsCaches[i].putNo)) |
| | | fmt.Println("cache:", eqi.EsCaches[i]) |
| | | } |
| | | cache := &eqi.EsCaches[0] |
| | |
| | | fmt.Println("cache:", cache) |
| | | |
| | | cache0 := eqi.EsCaches[0] |
| | | fmt.Printf("cache0:%p\n", &(cache0)) |
| | | fmt.Printf("cache0:%p\n", &(cache0)) |
| | | fmt.Printf("cache0.getNo:%p\n", &(cache0.getNo)) |
| | | fmt.Printf("cache0.putNo:%p\n", &(cache0.putNo)) |
| | | fmt.Println("cache0:", cache0) |
| | | |
| | | cache1 := eqi.EsCaches[1] |
| | | fmt.Printf("cache1:%p\n", &(cache1)) |
| | | fmt.Printf("cache1:%p\n", &(cache1)) |
| | | fmt.Printf("cache1.getNo:%p\n", &(cache1.getNo)) |
| | | fmt.Printf("cache1.putNo:%p\n", &(cache1.putNo)) |
| | | fmt.Println("cache1:", cache1) |
| | |
| | | eqi.ShmID = shmid |
| | | eqi.ShmKey = key |
| | | eqi.Queue = q |
| | | time.Sleep(10*time.Second) |
| | | time.Sleep(10 * time.Second) |
| | | |
| | | return &eqi |
| | | } |
| | |
| | | shmdata := bytes2shmEsQueue(data) |
| | | fmt.Println("shmdata:", shmdata) |
| | | eqi.EsCaches = ptr2esCache(unsafe.Pointer(&shmdata.cache), int(shmdata.capacity)) |
| | | //fmt.Println("EsCaches:", eqi.EsCaches) |
| | | fmt.Println("EsCaches:", eqi.EsCaches) |
| | | |
| | | eqi.Queue = shmdata |
| | | eqi.ShmKey = key |
| | |
| | | |
| | | // ReleaseQueue detach an exist shm queue |
| | | func (eqi *EsQueueInfo) ReleaseQueue() { |
| | | fmt.Println("ReleaseQueue: key", eqi.ShmKey) |
| | | fmt.Printf("ReleaseQueue: key=%x\n", eqi.ShmKey) |
| | | DestroyShm(eqi.ShmData) |
| | | } |
| | | |
| | | // RemoveShmId remove an exist shm queue (ipcrm -m shmid) |
| | | func (eqi *EsQueueInfo) RemoveShmId() error { |
| | | fmt.Printf("RemoveShmId: key=%x\n", eqi.ShmKey) |
| | | return shm.Rm(eqi.ShmID) |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | return int(quantity) |
| | | } |
| | | |
| | | // Status status of shm queue |
| | | func (eqi *EsQueueInfo) QueueElements() []ElemInfo { |
| | | var elems []ElemInfo |
| | | |
| | | for i := 0; i < len(eqi.EsCaches); i++ { |
| | | if eqi.EsCaches[i].value.PicId != 0 && eqi.EsCaches[i].value.InfoId != 0 { |
| | | elems = append(elems, eqi.EsCaches[i].value) |
| | | } |
| | | } |
| | | |
| | | return elems |
| | | } |
| | | |
| | | // Put 单次写入,可能失败 |
| | |
| | | return ok, qua |
| | | } |
| | | |
| | | to := time.NewTimer(time.Duration(timeout)*time.Millisecond) |
| | | to := time.NewTimer(time.Duration(timeout) * time.Millisecond) |
| | | defer to.Stop() |
| | | |
| | | for { |
| | |
| | | putNo := atomic.LoadUint32(&cache.putNo) |
| | | if getPosNew == getNo && getNo == putNo-eqi.Queue.capacity { |
| | | val := cache.value |
| | | cache.value = ElemInfo{ShmId:0} |
| | | cache.value = ElemInfo{PicId: 0} |
| | | atomic.AddUint32(&cache.getNo, eqi.Queue.capacity) |
| | | return val, true, int(posCnt - 1) |
| | | } else { |
| | |
| | | return val, ok, qua |
| | | } |
| | | |
| | | to := time.NewTimer(time.Duration(timeout)*time.Millisecond) |
| | | to := time.NewTimer(time.Duration(timeout) * time.Millisecond) |
| | | defer to.Stop() |
| | | |
| | | for { |
| | | select { |
| | | case <-to.C: |
| | | return ElemInfo{},false, 0 |
| | | return ElemInfo{}, false, 0 |
| | | default: |
| | | val, ok, qua = eqi.Get() |
| | | if ok { |
| | |
| | | v++ |
| | | return v |
| | | } |
| | | |