atexit: add Signalled() function - set if called from a signal #4804

This commit is contained in:
Nick Craig-Wood 2020-11-27 10:49:24 +00:00
parent 1cfce703b2
commit 83406bc473

View File

@ -8,6 +8,7 @@ import (
"os"
"os/signal"
"sync"
"sync/atomic"
"github.com/rclone/rclone/fs"
)
@ -18,6 +19,7 @@ var (
exitChan chan os.Signal
exitOnce sync.Once
registerOnce sync.Once
signalled int32
)
// FnHandle is the type of the handle returned by function `Register`
@ -40,6 +42,7 @@ func Register(fn func()) FnHandle {
if sig == nil {
return
}
atomic.StoreInt32(&signalled, 1)
fs.Infof(nil, "Signal received: %s", sig)
Run()
fs.Infof(nil, "Exiting...")
@ -50,6 +53,11 @@ func Register(fn func()) FnHandle {
return &fn
}
// Signalled returns true if an exit signal has been received
func Signalled() bool {
return atomic.LoadInt32(&signalled) != 0
}
// Unregister a function using the handle returned by `Register`
func Unregister(handle FnHandle) {
fnsMutex.Lock()