mirror of https://github.com/golang/go.git
internal/poll: use runtime.AddCleanup instead of runtime.SetFinalizer
Replace the use of SetFinalizer with AddCleanup. For #70907 Change-Id: I0cb2c2985eb9285e5f92be9dbcb9d77acc0f59c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/671441 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
parent
b4e992b6e1
commit
c44c4de51b
|
|
@ -9,7 +9,6 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -179,10 +178,7 @@ type splicePipeFields struct {
|
||||||
|
|
||||||
type splicePipe struct {
|
type splicePipe struct {
|
||||||
splicePipeFields
|
splicePipeFields
|
||||||
|
cleanup runtime.Cleanup
|
||||||
// We want to use a finalizer, so ensure that the size is
|
|
||||||
// large enough to not use the tiny allocator.
|
|
||||||
_ [24 - unsafe.Sizeof(splicePipeFields{})%24]byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// splicePipePool caches pipes to avoid high-frequency construction and destruction of pipe buffers.
|
// splicePipePool caches pipes to avoid high-frequency construction and destruction of pipe buffers.
|
||||||
|
|
@ -197,7 +193,10 @@ func newPoolPipe() any {
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(p, destroyPipe)
|
|
||||||
|
p.cleanup = runtime.AddCleanup(p, func(spf splicePipeFields) {
|
||||||
|
destroyPipe(&splicePipe{splicePipeFields: spf})
|
||||||
|
}, p.splicePipeFields)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -214,7 +213,7 @@ func putPipe(p *splicePipe) {
|
||||||
// If there is still data left in the pipe,
|
// If there is still data left in the pipe,
|
||||||
// then close and discard it instead of putting it back into the pool.
|
// then close and discard it instead of putting it back into the pool.
|
||||||
if p.data != 0 {
|
if p.data != 0 {
|
||||||
runtime.SetFinalizer(p, nil)
|
p.cleanup.Stop()
|
||||||
destroyPipe(p)
|
destroyPipe(p)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue