Search results

There are no results.

std.signal

Handling of Unix signals.

This module allows processes to wait for Unix signals to be received, overriding their default behaviour in the process. To do so, create an instance of the Signal enum and call Signal.wait on it:

import std.signal (Signal)

Signal.User1.wait

Signals are handled asynchronously, meaning there may be a small delay between receiving the signal and Signal.wait returning. The order in which signals are received is unspecified. The underlying platform may also coalesce multiple instances of the same signal being received into a single receive, such that sending e.g. SIGHUP 3 times in a short period results in it only being received once.

Default signal handlers

If no process is waiting for a specific signal and the signal is received, it's default behaviour according to the underlying platform is invoked. For example, the default signal handler for SIGUSR1 terminates the current process.

Supported signals

To increase portability and to prevent users from handling problematic signals (SIGSEGV or SIGFPE for example), this module only supports a limited number of signals.

Handling of SIGPIPE isn't supported as this is almost always a terrible idea, and the runtime disables/masks this signal as to not interfere with (amongst other things) non-blocking sockets.

Waiting for multiple signals

If you need to wait for multiple signals at once, you can do so using a std.sync.Channel and a process for each signal. For example:

import std.signal (Signal)
import std.sync (Channel)

class async Waiter {
  let @channel: Channel[Signal]

  fn async wait(signal: uni Signal) {
    signal.wait
    @channel.send(signal)
  }
}

class async Main {
  fn async main {
    let chan = Channel.new

    Waiter(recover chan.clone).wait(recover Signal.User1)
    Waiter(recover chan.clone).wait(recover Signal.User2)
    chan.receive
  }
}

In this example the chan.receive line returns either uni Signal.User1 or uni Signal.User2, depending on which signal is received first.

Classes

Signal

A Unix signal.