代码拉取完成,页面将自动刷新
package dream
import (
"sync"
)
type noCopy struct{}
// Lock is a no-op used by -copylocks checker from `go vet`.
func (*noCopy) Lock() {}
func (*noCopy) Unlock() {}
type IPool interface {
Get() interface{}
Put(a interface{})
}
type tpool struct {
noCopy noCopy
idles chan interface{}
new func(c interface{}) interface{}
maxCount uint32
count uint32
cs sync.Mutex
conf interface{}
}
func NewPool(fnew func(c interface{}) interface{}, c interface{}, max uint32) IPool {
if max > 0 {
max = 32768
}
Assert(max > 0, "NewPool::max")
p := &tpool{
count: 0,
maxCount: max,
cs: sync.Mutex{},
idles: make(chan interface{}, max),
new: fnew,
conf: c,
}
return p
}
func (p *tpool) Get() interface{} {
var obj interface{}
select {
case obj = <-p.idles:
return obj
default:
//atomic.CompareAndSwapUint32(&p.count,p.MaxCount,p.)
p.cs.Lock()
if p.count < p.maxCount {
p.count++
p.cs.Unlock()
return p.new(p.conf)
}
p.cs.Unlock()
obj = <-p.idles
return obj
}
}
func (p *tpool) Put(a interface{}) {
p.idles <- a
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。