zhangmeng
2021-09-24 2baeac99a903eae2e99bf30581633da6aa7f8abc
bug fixed withintime
1个文件已修改
95 ■■■■■ 已修改文件
shmqueue.go 95 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
shmqueue.go
@@ -1,9 +1,9 @@
package shmqueue
import (
    shm "basic.com/valib/goshm.git"
    "context"
    "fmt"
    shm "basic.com/valib/goshm.git"
    "reflect"
    "runtime"
    "sync/atomic"
@@ -12,7 +12,7 @@
)
const (
    TimePeriodPutOrGet = time.Duration(5)*time.Millisecond //ms
    TimePeriodPutOrGet = time.Duration(5) * time.Millisecond //ms
)
//Element info
@@ -252,25 +252,33 @@
        return ok, qua
    }
    to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
    defer to.Stop()
    loopB:
        for {
            select {
            case <-ctx.Done():
                return false, 0
            case <-to.C:
                return false, 0
            default:
                ok, qua = eqi.Put(val)
                if ok {
                    break loopB
                }
                time.Sleep(TimePeriodPutOrGet)
            }
    count := timeout/int(TimePeriodPutOrGet) + 1
    for i := 0; i < count; i++ {
        if ok, qua := eqi.Put(val); ok {
            return ok, qua
        }
    return ok, qua
    }
    return false, 0
    //     to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
    //     defer to.Stop()
    // loopB:
    //     for {
    //         select {
    //         case <-ctx.Done():
    //             return false, 0
    //         case <-to.C:
    //             return false, 0
    //         default:
    //             ok, qua = eqi.Put(val)
    //             if ok {
    //                 break loopB
    //             }
    //             time.Sleep(TimePeriodPutOrGet)
    //         }
    //     }
    //     return ok, qua
}
// Get 单次获取,可能失败
@@ -306,7 +314,7 @@
        putNo := atomic.LoadUint32(&cache.putNo)
        if getPosNew == getNo && getNo == putNo-eqi.Queue.capacity {
            val := cache.value
            cache.value = ElemInfo{PicId: 0, InfoId:0}
            cache.value = ElemInfo{PicId: 0, InfoId: 0}
            atomic.AddUint32(&cache.getNo, eqi.Queue.capacity)
            return val, true, int(posCnt - 1)
        } else {
@@ -343,26 +351,33 @@
    if ok {
        return val, ok, qua
    }
    to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
    defer to.Stop()
    loopB:
        for {
            select {
            case <-ctx.Done():
                return ElemInfo{}, false, 0
            case <-to.C:
                return ElemInfo{}, false, 0
            default:
                val, ok, qua = eqi.Get()
                if ok {
                    break loopB
                }
                time.Sleep(TimePeriodPutOrGet)
            }
    count := timeout/int(TimePeriodPutOrGet) + 1
    for i := 0; i < count; i++ {
        if val, ok, qua := eqi.Get(); ok {
            return val, ok, qua
        }
    return val, ok, qua
    }
    return ElemInfo{}, false, 0
    //     to := time.NewTimer(time.Duration(timeout) * time.Millisecond)
    //     defer to.Stop()
    // loopB:
    //     for {
    //         select {
    //         case <-ctx.Done():
    //             return ElemInfo{}, false, 0
    //         case <-to.C:
    //             return ElemInfo{}, false, 0
    //         default:
    //             val, ok, qua = eqi.Get()
    //             if ok {
    //                 break loopB
    //             }
    //             time.Sleep(TimePeriodPutOrGet)
    //         }
    //     }
    //     return val, ok, qua
}
// round 到最近的2的倍数