-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There is no functional change here yet; this just factors the recounting we were already doing into a package, and exposes the ability to refcount to other parts of the code. This is a stepping stone towards addressing #349; the idea is that the method receiver should get a reference to this too to keep it alive for the right amount of time.
- Loading branch information
Showing
3 changed files
with
56 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package rc | ||
|
||
import "sync/atomic" | ||
|
||
type Releaser struct { | ||
release func() | ||
refcount atomic.Int32 | ||
} | ||
|
||
func NewReleaser(init int32, release func()) *Releaser { | ||
ret := &Releaser{ | ||
release: release, | ||
} | ||
ret.refcount.Store(init) | ||
return ret | ||
} | ||
|
||
func (rc *Releaser) Decr() { | ||
newCount := rc.refcount.Add(-1) | ||
if newCount == 0 { | ||
rc.release() | ||
rc.release = nil | ||
} | ||
} | ||
|
||
func (rc *Releaser) Incr() { | ||
newCount := rc.refcount.Add(1) | ||
if newCount == 1 { | ||
panic("Incremented an already-zero refcount") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters