GoDoxy/internal/utils/ref_count.go
2024-10-14 10:31:27 +08:00

42 lines
607 B
Go

package utils
type RefCount struct {
_ NoCopy
refCh chan bool
notifyZero chan struct{}
}
func NewRefCounter() *RefCount {
rc := &RefCount{
refCh: make(chan bool, 1),
notifyZero: 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
}
}
}()
return rc
}
func (rc *RefCount) Zero() <-chan struct{} {
return rc.notifyZero
}
func (rc *RefCount) Add() {
rc.refCh <- true
}
func (rc *RefCount) Sub() {
rc.refCh <- false
}