1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package framework
- import (
- "sync"
- )
- type CleanupActionHandle *int
- type cleanupFuncHandle struct {
- actionHandle CleanupActionHandle
- actionHook func()
- }
- var (
- cleanupActionsLock sync.Mutex
- cleanupHookList = []cleanupFuncHandle{}
- )
- func AddCleanupAction(fn func()) CleanupActionHandle {
- p := CleanupActionHandle(new(int))
- cleanupActionsLock.Lock()
- defer cleanupActionsLock.Unlock()
- c := cleanupFuncHandle{actionHandle: p, actionHook: fn}
- cleanupHookList = append([]cleanupFuncHandle{c}, cleanupHookList...)
- return p
- }
- func RemoveCleanupAction(p CleanupActionHandle) {
- cleanupActionsLock.Lock()
- defer cleanupActionsLock.Unlock()
- for i, item := range cleanupHookList {
- if item.actionHandle == p {
- cleanupHookList = append(cleanupHookList[:i], cleanupHookList[i+1:]...)
- break
- }
- }
- }
- func RunCleanupActions() {
- list := []func(){}
- func() {
- cleanupActionsLock.Lock()
- defer cleanupActionsLock.Unlock()
- for _, p := range cleanupHookList {
- list = append(list, p.actionHook)
- }
- }()
-
- for _, fn := range list {
- fn()
- }
- }
|