rewrite and fix reference counter

This commit is contained in:
yusing
2025-01-02 09:59:31 +08:00
parent 5fa0d47c0d
commit af14966b09
2 changed files with 97 additions and 21 deletions

View File

@@ -1,42 +1,41 @@
package utils
import (
"sync"
"sync/atomic"
)
type RefCount struct {
_ NoCopy
refCh chan bool
notifyZero chan struct{}
mu sync.Mutex
cond *sync.Cond
refCount uint32
zeroCh chan struct{}
}
func NewRefCounter() *RefCount {
rc := &RefCount{
refCh: make(chan bool, 1),
notifyZero: make(chan struct{}),
refCount: 1,
zeroCh: make(chan struct{}),
}
go func() {
refCount := uint32(1)
for isAdd := range rc.refCh {
if isAdd {
refCount++
} else {
refCount--
}
if refCount <= 0 {
close(rc.notifyZero)
return
}
}
}()
rc.cond = sync.NewCond(&rc.mu)
return rc
}
func (rc *RefCount) Zero() <-chan struct{} {
return rc.notifyZero
return rc.zeroCh
}
func (rc *RefCount) Add() {
rc.refCh <- true
atomic.AddUint32(&rc.refCount, 1)
}
func (rc *RefCount) Sub() {
rc.refCh <- false
if atomic.AddUint32(&rc.refCount, ^uint32(0)) == 0 {
rc.mu.Lock()
close(rc.zeroCh)
rc.cond.Broadcast()
rc.mu.Unlock()
}
}