mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
runtime: add support for os/signal
This adds support for enabling and listening to signals on Linux and MacOS.
This commit is contained in:
committed by
Ron Evans
parent
0f95b4102d
commit
b8fe75a9dd
Vendored
+42
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
// Test POSIX signals.
|
||||
// TODO: run `tinygo test os/signal` instead, once CGo errno return values are
|
||||
// supported.
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGUSR1)
|
||||
|
||||
// Wait for signals to arrive.
|
||||
go func() {
|
||||
for sig := range c {
|
||||
if sig == syscall.SIGUSR1 {
|
||||
println("got expected signal")
|
||||
} else {
|
||||
println("got signal:", sig.String())
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Send the signal.
|
||||
syscall.Kill(syscall.Getpid(), syscall.SIGUSR1)
|
||||
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
|
||||
// Stop notifying.
|
||||
// (This is just a smoke test, it's difficult to test the default behavior
|
||||
// in a unit test).
|
||||
signal.Ignore(syscall.SIGUSR1)
|
||||
|
||||
signal.Stop(c)
|
||||
|
||||
println("exiting signal program")
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
got expected signal
|
||||
exiting signal program
|
||||
Reference in New Issue
Block a user