You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/syncx/refresource.go

54 lines
892 B
Go

4 years ago
package syncx
import (
"errors"
"sync"
)
// ErrUseOfCleaned is an error that indicates using a cleaned resource.
4 years ago
var ErrUseOfCleaned = errors.New("using a cleaned resource")
// A RefResource is used to reference counting a resource.
4 years ago
type RefResource struct {
lock sync.Mutex
ref int32
cleaned bool
clean func()
}
// NewRefResource returns a RefResource.
4 years ago
func NewRefResource(clean func()) *RefResource {
return &RefResource{
clean: clean,
}
}
// Use uses the resource with reference count incremented.
4 years ago
func (r *RefResource) Use() error {
r.lock.Lock()
defer r.lock.Unlock()
if r.cleaned {
return ErrUseOfCleaned
}
r.ref++
return nil
}
// Clean cleans a resource with reference count decremented.
4 years ago
func (r *RefResource) Clean() {
r.lock.Lock()
defer r.lock.Unlock()
if r.cleaned {
return
}
r.ref--
if r.ref == 0 {
r.cleaned = true
r.clean()
}
}