mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 17:28:53 +02:00
fix edge cases refCounter close channel twice
This commit is contained in:
@@ -24,11 +24,31 @@ func (rc *RefCount) Zero() <-chan struct{} {
|
||||
}
|
||||
|
||||
func (rc *RefCount) Add() {
|
||||
atomic.AddUint32(&rc.refCount, 1)
|
||||
// We add before checking to ensure proper ordering
|
||||
newV := atomic.AddUint32(&rc.refCount, 1)
|
||||
if newV == 1 {
|
||||
// If it was 0 before we added, that means we're incrementing after a close
|
||||
// This is a programming error
|
||||
panic("RefCount.Add() called after count reached zero")
|
||||
}
|
||||
}
|
||||
|
||||
func (rc *RefCount) Sub() {
|
||||
if atomic.AddUint32(&rc.refCount, ^uint32(0)) == 0 {
|
||||
close(rc.zeroCh)
|
||||
// First read the current value
|
||||
for {
|
||||
current := atomic.LoadUint32(&rc.refCount)
|
||||
if current == 0 {
|
||||
// Already at zero, channel should be closed
|
||||
return
|
||||
}
|
||||
|
||||
// Try to decrement, but only if the value hasn't changed
|
||||
if atomic.CompareAndSwapUint32(&rc.refCount, current, current-1) {
|
||||
if current == 1 { // Was this the last reference?
|
||||
close(rc.zeroCh)
|
||||
}
|
||||
return
|
||||
}
|
||||
// If CAS failed, someone else modified the count, try again
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user