github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/os/signal/signal.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package signal 6 7 import ( 8 "github.com/shogo82148/std/context" 9 "github.com/shogo82148/std/os" 10 ) 11 12 // Ignore causes the provided signals to be ignored. If they are received by 13 // the program, nothing will happen. Ignore undoes the effect of any prior 14 // calls to [Notify] for the provided signals. 15 // If no signals are provided, all incoming signals will be ignored. 16 func Ignore(sig ...os.Signal) 17 18 // Ignored reports whether sig is currently ignored. 19 func Ignored(sig os.Signal) bool 20 21 // Notify causes package signal to relay incoming signals to c. 22 // If no signals are provided, all incoming signals will be relayed to c. 23 // Otherwise, just the provided signals will. 24 // 25 // Package signal will not block sending to c: the caller must ensure 26 // that c has sufficient buffer space to keep up with the expected 27 // signal rate. For a channel used for notification of just one signal value, 28 // a buffer of size 1 is sufficient. 29 // 30 // It is allowed to call Notify multiple times with the same channel: 31 // each call expands the set of signals sent to that channel. 32 // The only way to remove signals from the set is to call [Stop]. 33 // 34 // It is allowed to call Notify multiple times with different channels 35 // and the same signals: each channel receives copies of incoming 36 // signals independently. 37 func Notify(c chan<- os.Signal, sig ...os.Signal) 38 39 // Reset undoes the effect of any prior calls to [Notify] for the provided 40 // signals. 41 // If no signals are provided, all signal handlers will be reset. 42 func Reset(sig ...os.Signal) 43 44 // Stop causes package signal to stop relaying incoming signals to c. 45 // It undoes the effect of all prior calls to [Notify] using c. 46 // When Stop returns, it is guaranteed that c will receive no more signals. 47 func Stop(c chan<- os.Signal) 48 49 // NotifyContext returns a copy of the parent context that is marked done 50 // (its Done channel is closed) when one of the listed signals arrives, 51 // when the returned stop function is called, or when the parent context's 52 // Done channel is closed, whichever happens first. 53 // 54 // The stop function unregisters the signal behavior, which, like [signal.Reset], 55 // may restore the default behavior for a given signal. For example, the default 56 // behavior of a Go program receiving [os.Interrupt] is to exit. Calling 57 // NotifyContext(parent, os.Interrupt) will change the behavior to cancel 58 // the returned context. Future interrupts received will not trigger the default 59 // (exit) behavior until the returned stop function is called. 60 // 61 // The stop function releases resources associated with it, so code should 62 // call stop as soon as the operations running in this Context complete and 63 // signals no longer need to be diverted to the context. 64 func NotifyContext(parent context.Context, signals ...os.Signal) (ctx context.Context, stop context.CancelFunc)