Source File
mfinal.go
Belonging Package
runtime
package runtime
import (
)
type finblock struct {
alllink *finblock
next *finblock
cnt uint32
_ int32
fin [(_FinBlockSize - 2*sys.PtrSize - 2*4) / unsafe.Sizeof(finalizer{})]finalizer
}
var finlock mutex // protects the following variables
var fing *g // goroutine that runs finalizers
var finq *finblock // list of finalizers that are to be executed
var finc *finblock // cache of free blocks
var finptrmask [_FinBlockSize / sys.PtrSize / 8]byte
var fingwait bool
var fingwake bool
var allfin *finblock // list of all blocks
1<<0 | 1<<1 | 0<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 0<<7,
1<<0 | 1<<1 | 1<<2 | 1<<3 | 0<<4 | 1<<5 | 1<<6 | 1<<7,
1<<0 | 0<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 0<<6 | 1<<7,
1<<0 | 1<<1 | 1<<2 | 0<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7,
0<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 0<<5 | 1<<6 | 1<<7,
}
func ( unsafe.Pointer, *funcval, uintptr, *_type, *ptrtype) {
if (unsafe.Sizeof(finalizer{}) != 5*sys.PtrSize ||
unsafe.Offsetof(finalizer{}.fn) != 0 ||
unsafe.Offsetof(finalizer{}.arg) != sys.PtrSize ||
unsafe.Offsetof(finalizer{}.nret) != 2*sys.PtrSize ||
unsafe.Offsetof(finalizer{}.fint) != 3*sys.PtrSize ||
unsafe.Offsetof(finalizer{}.ot) != 4*sys.PtrSize) {
throw("finalizer out of sync")
}
for := range finptrmask {
finptrmask[] = finalizer1[%len(finalizer1)]
}
}
}
:= finc
finc = .next
.next = finq
finq =
}
:= &finq.fin[finq.cnt]
atomic.Xadd(&finq.cnt, +1) // Sync with markroots
.fn =
.nret =
.fint =
.ot =
.arg =
fingwake = true
unlock(&finlock)
}
func ( func(*funcval, unsafe.Pointer, uintptr, *_type, *ptrtype)) {
for := allfin; != nil; = .alllink {
for := uint32(0); < .cnt; ++ {
:= &.fin[]
(.fn, .arg, .nret, .fint, .ot)
}
}
}
func () *g {
var *g
lock(&finlock)
if fingwait && fingwake {
fingwait = false
fingwake = false
= fing
}
unlock(&finlock)
return
}
var (
fingCreate uint32
fingRunning bool
)
if fingCreate == 0 && atomic.Cas(&fingCreate, 0, 1) {
go runfinq()
}
}
func () {
var (
unsafe.Pointer
uintptr
)
for {
lock(&finlock)
:= finq
finq = nil
if == nil {
:= getg()
fing =
fingwait = true
goparkunlock(&finlock, waitReasonFinalizerWait, traceEvGoBlock, 1)
continue
}
unlock(&finlock)
if raceenabled {
racefingo()
}
for != nil {
for := .cnt; > 0; -- {
:= &.fin[-1]
:= unsafe.Sizeof((interface{})(nil)) + .nret
*(*unsafe.Pointer)() = .arg
case kindInterface:
*(*iface)() = assertE2I(, *(*eface)())
}
default:
throw("bad kind in runfinq")
}
fingRunning = true
reflectcall(nil, unsafe.Pointer(.fn), , uint32(), uint32())
fingRunning = false
func ( interface{}, interface{}) {
, , := findObject(uintptr(.data), 0, 0)
for := &firstmoduledata; != nil; = .next {
if .noptrdata <= uintptr(.data) && uintptr(.data) < .enoptrdata ||
.data <= uintptr(.data) && uintptr(.data) < .edata ||
.bss <= uintptr(.data) && uintptr(.data) < .ebss ||
.noptrbss <= uintptr(.data) && uintptr(.data) < .enoptrbss {
return
}
}
throw("runtime.SetFinalizer: pointer not in allocated block")
}
systemstack(func() {
removefinalizer(.data)
})
return
}
if .kind&kindMask != kindFunc {
throw("runtime.SetFinalizer: second argument is " + .string() + ", not a function")
}
:= (*functype)(unsafe.Pointer())
if .dotdotdot() {
throw("runtime.SetFinalizer: cannot pass " + .string() + " to finalizer " + .string() + " because dotdotdot")
}
if .inCount != 1 {
throw("runtime.SetFinalizer: cannot pass " + .string() + " to finalizer " + .string())
}
:= .in()[0]
switch {
goto
}
case .kind&kindMask == kindInterface:
:= (*interfacetype)(unsafe.Pointer())
goto
}
if , := assertE2I2(, *efaceOf(&)); {
goto
}
}
throw("runtime.SetFinalizer: cannot pass " + .string() + " to finalizer " + .string())
createfing()
systemstack(func() {
if !addfinalizer(.data, (*funcval)(.data), , , ) {
throw("runtime.SetFinalizer: finalizer already set")
}
})
}
if cgoAlwaysFalse {
println()
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |