blob: c6d8a0b4d2ec4d8d50ddbb8dc45a943b1fceefe3 [file]
/-
Copyright (c) 2024 Lean FRO, LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sofia Rodrigues
-/
module
prelude
public import Std.Time
public import Std.Internal.UV.Signal
public import Std.Async.Select
public section
namespace Std
namespace Async
/--
Unix style signals for Unix and Windows.
SIGKILL and SIGSTOP are missing because they cannot be caught.
SIGBUS, SIGFPE, SIGILL, and SIGSEGV are missing because they cannot be caught safely.
SIGPIPE is not present because the runtime ignores the signal.
-/
inductive Signal
/--
Hangup detected on controlling terminal or death of controlling process.
On Windows:
- SIGHUP is generated when the user closes the console window. The program is given ~10 seconds to
perform cleanup before Windows unconditionally terminates it.
-/
| sighup
/--
Interrupt program.
Notes:
- Normally delivered when the user presses CTRL+C. Not generated when terminal raw mode is enabled.
-/
| sigint
/--
Quit program.
-/
| sigquit
/--
Trace/breakpoint trap.
-/
| sigtrap
/--
Abort signal.
Notes:
- SIGABRT is not catchable if generated by certain runtime functions, such as abort or assertion failure.
- On Windows, watchers can be created for SIGABRT, but they never receive the signal.
-/
| sigabrt
/--
User-defined signal 1.
-/
| sigusr1
/--
User-defined signal 2.
-/
| sigusr2
/--
Real-time timer expired.
-/
| sigalrm
/--
Termination signal.
Notes:
- On Windows, watchers can be created for SIGTERM, but they never receive the signal.
-/
| sigterm
/--
Child status has changed.
-/
| sigchld
/--
Continue after stop.
-/
| sigcont
/--
Stop typed at terminal.
-/
| sigtstp
/--
Background read attempted from control terminal.
-/
| sigttin
/--
Background write attempted to control terminal.
-/
| sigttou
/--
Urgent condition on socket.
-/
| sigurg
/--
CPU time limit exceeded.
-/
| sigxcpu
/--
File size limit exceeded.
-/
| sigxfsz
/--
Virtual alarm clock.
-/
| sigvtalrm
/--
Profiling timer expired.
-/
| sigprof
/--
Window size change.
Notes:
- SIGWINCH is raised whenever the runtime detects the console has been resized.
- Under console emulators, or on 32-bit apps on 64-bit systems, SIGWINCH is emulated.
- In these cases, signals may not be delivered timely.
-/
| sigwinch
/--
I/O now possible.
-/
| sigio
/--
Bad system call.
-/
| sigsys
deriving Repr, DecidableEq, BEq
namespace Signal
/--
Converts a `Signal` to its corresponding `Int32` value as defined in `man 7 signal`.
These values are then mapped to the underlying architecture's values in runtime/uv/signal.cpp,
so make sure to update that whenever you update this code.
-/
private def toInt32 : Signal → Int32
| .sighup => 1
| .sigint => 2
| .sigquit => 3
| .sigtrap => 5
| .sigabrt => 6
| .sigusr1 => 10
| .sigusr2 => 12
| .sigalrm => 14
| .sigterm => 15
| .sigchld => 17
| .sigcont => 18
| .sigtstp => 20
| .sigttin => 21
| .sigttou => 22
| .sigurg => 23
| .sigxcpu => 24
| .sigxfsz => 25
| .sigvtalrm => 26
| .sigprof => 27
| .sigwinch => 28
| .sigio => 29
| .sigsys => 31
/--
`Signal.Waiter` can be used to wait for a specific signal.
-/
structure Waiter where
private ofNative ::
native : Internal.UV.Signal
namespace Waiter
/--
Set up a `Signal.Waiter` that waits for the specified `signum`.
This function only initializes but does not yet start listening for the signal.
While the waiter listens for the signal, the default action of the signal is replaced. A waiter
that is no longer referenced can keep listening if the last operation that reached it was `wait`: a
one-shot waiter until the signal arrives, a repeating one for the rest of the process. After a
select that checked it, it stops listening once it is dropped. `stop` stops it in every case.
-/
@[inline]
def mk (signum : Signal) (repeating : Bool) : IO Signal.Waiter := do
let native ← Internal.UV.Signal.mk signum.toInt32 repeating
return .ofNative native
/--
If:
- `s` is not yet running start listening and return an `AsyncTask` that will resolve once the
previously configured signal is received.
- `s` is running, or is a one-shot `s` that finished after receiving the signal, return an
`AsyncTask` for the pending signal, which is the one returned by the previous call unless that one
resolved (for a repeating `s`) or a select that did not find the signal already received dropped
it. Tasks whose promise was dropped fail.
- `s` was stopped with `stop`, and is repeating or had not received the signal, return an
`AsyncTask` that fails.
The resolved `AsyncTask` contains the number that `Signal` assigns to the received signal, which is
the same on every platform (the x86 Linux numbering).
-/
@[inline]
def wait (s : Signal.Waiter) : IO (AsyncTask Int) := do
let promise ← s.native.next
return .ofPurePromise promise
/--
If:
- `s` is still running this stops `s` without resolving any remaining `AsyncTask`s that were created
through `wait`. Those tasks fail once the last reference to their promise is dropped, rather than
producing a value.
- `s` is not yet or not anymore running this is a no-op.
-/
@[inline]
def stop (s : Signal.Waiter) : IO Unit :=
s.native.stop
/--
Create a `Selector` that resolves once `s` has received the signal. Calling this function does not
start the signal waiter: a select starts it when it checks it, and it may not check it at all if
another selector is already ready.
A select that checks `s` without picking it leaves it listening until `stop` is called or `s` is
dropped, and a signal that arrives afterwards is kept and reported by the next select that checks
`s`. For a repeating `s`, a signal that arrives while another selector wins the same select may be
lost.
-/
def selector (s : Signal.Waiter) : Selector Unit :=
{
tryFn := do
let signalWaiter ← s.native.next
-- Also when `signalWaiter` is already resolved: a repeating signal otherwise keeps the loop's
-- reference to it, so it would keep listening after it is dropped.
s.native.cancel
-- A signal that arrived before `cancel` resolved `signalWaiter` instead of being kept.
if ← signalWaiter.isResolved then
return some ()
return none
registerFn waiter := do
let signalWaiter ← s.wait
discard <| AsyncTask.mapIO (x := signalWaiter) fun _ => do
let lose := return ()
let win promise := promise.resolve (.ok ())
waiter.race lose win
unregisterFn := s.native.cancel
}