pkg: atexit: 增加自定义类型prio

This commit is contained in:
Yousong Zhou
2019-06-15 10:56:38 +00:00
parent ad613d5110
commit dd49b2ef9e
2 changed files with 11 additions and 8 deletions
+7 -6
View File
@@ -32,14 +32,14 @@ type ExitHandlerFunc func(ExitHandler)
// Prio at exit time. Handler func will receive a copy of the ExitHandler
// struct previously registered
type ExitHandler struct {
Prio int
Prio Prio
Reason string
Func ExitHandlerFunc
Value interface{}
}
var (
handlers = map[int][]ExitHandler{}
handlers = map[Prio][]ExitHandler{}
handlersLock = &sync.Mutex{}
once = &sync.Once{}
)
@@ -79,12 +79,13 @@ func Handle() {
handlersLock.Lock()
defer handlersLock.Unlock()
prios := make([]int, 0, len(handlers))
ints := make([]int, 0, len(handlers))
for prio := range handlers {
prios = append(prios, prio)
ints = append(ints, int(prio))
}
sort.Ints(prios)
for _, prio := range prios {
sort.Ints(ints)
for _, i := range ints {
prio := Prio(i)
ehs := handlers[prio]
for _, eh := range ehs {
print("atexit: prio=", prio, ", reason=", eh.Reason, "\n")
+4 -2
View File
@@ -14,7 +14,9 @@
package atexit
type Prio int
const (
PRIO_LOG_OTHER = 20000
PRIO_LOG_CLOSE = 40000
PRIO_LOG_OTHER Prio = 20000
PRIO_LOG_CLOSE = 40000
)