lbcd/signal.go

60 lines
1.7 KiB
Go
Raw Normal View History

2014-01-01 17:16:15 +01:00
// Copyright (c) 2013-2014 Conformal Systems LLC.
2013-08-06 23:55:22 +02:00
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"os"
"os/signal"
)
// interruptChannel is used to receive SIGINT (Ctrl+C) signals.
var interruptChannel chan os.Signal
// addHandlerChannel is used to add an interrupt handler to the list of handlers
// to be invoked on SIGINT (Ctrl+C) signals.
var addHandlerChannel = make(chan func())
// mainInterruptHandler listens for SIGINT (Ctrl+C) signals on the
// interruptChannel and invokes the registered interruptCallbacks accordingly.
// It also listens for callback registration. It must be run as a goroutine.
func mainInterruptHandler() {
// interruptCallbacks is a list of callbacks to invoke when a
// SIGINT (Ctrl+C) is received.
var interruptCallbacks []func()
for {
select {
case <-interruptChannel:
btcdLog.Infof("Received SIGINT (Ctrl+C). Shutting down...")
2013-12-10 20:46:32 +01:00
// run handlers in LIFO order.
2013-12-11 01:41:03 +01:00
for i := range interruptCallbacks {
idx := len(interruptCallbacks) - 1 - i
2013-12-10 20:46:32 +01:00
callback := interruptCallbacks[idx]
callback()
}
// Signal the main goroutine to shutdown.
shutdownChannel <- struct{}{}
case handler := <-addHandlerChannel:
interruptCallbacks = append(interruptCallbacks, handler)
}
}
}
2013-08-06 23:55:22 +02:00
// addInterruptHandler adds a handler to call when a SIGINT (Ctrl+C) is
// received.
func addInterruptHandler(handler func()) {
// Create the channel and start the main interrupt handler which invokes
// all other callbacks and exits if not already done.
if interruptChannel == nil {
interruptChannel = make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Interrupt)
go mainInterruptHandler()
2013-08-06 23:55:22 +02:00
}
addHandlerChannel <- handler
2013-08-06 23:55:22 +02:00
}