-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds support for enabling and listening to signals on Linux and MacOS.
- Loading branch information
Showing
9 changed files
with
270 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//go:build none | ||
|
||
// Ignore the //go:build above. This file is manually included on Linux and | ||
// MacOS to provide os/signal support. | ||
|
||
#include <stdint.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
|
||
// Signal handler in the runtime. | ||
void tinygo_signal_handler(int sig); | ||
|
||
// Enable a signal from the runtime. | ||
void tinygo_signal_enable(uint32_t sig) { | ||
struct sigaction act = { 0 }; | ||
act.sa_handler = &tinygo_signal_handler; | ||
sigaction(sig, &act, NULL); | ||
} | ||
|
||
void tinygo_signal_ignore(uint32_t sig) { | ||
struct sigaction act = { 0 }; | ||
act.sa_handler = SIG_IGN; | ||
sigaction(sig, &act, NULL); | ||
} | ||
|
||
void tinygo_signal_disable(uint32_t sig) { | ||
struct sigaction act = { 0 }; | ||
act.sa_handler = SIG_DFL; | ||
sigaction(sig, &act, NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
got expected signal | ||
exiting signal program |