Debian Codegraph @

src/util/atexit.go

raw
1 package util
2
3 import (
4 "sync"
5 )
6
7 var atExitFuncs []func()
8
9 // AtExit registers the function fn to be called on program termination.
10 // The functions will be called in reverse order they were registered.
11 func AtExit(fn func()) {
12 if fn == nil {
13 panic("AtExit called with nil func")
14 }
15 once := &sync.Once{}
16 atExitFuncs = append(atExitFuncs, func() {
17 once.Do(fn)
18 })
19 }
20
21 // RunAtExitFuncs runs any functions registered with AtExit().
22 func RunAtExitFuncs() {
23 fns := atExitFuncs
24 for i := len(fns) - 1; i >= 0; i-- {
25 fns[i]()
26 }
27 atExitFuncs = nil
28 }