blob: e134430443c53d3aa79c1ff908fe061cf4df0ab4 [file] [log] [blame]
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Alarm signal scheduling."><meta name="keywords" content="rust, rustlang, rust-lang, alarm"><title>nix::unistd::alarm - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../../../nix/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../nix/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module alarm</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#functions">Functions</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Module <a href="../../index.html">nix</a>::<wbr><a href="../index.html">unistd</a>::<wbr><a class="mod" href="#">alarm</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../../src/nix/unistd.rs.html#1753">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Alarm signal scheduling.</p>
<p>Scheduling an alarm will trigger a <code>SIGALRM</code> signal when the time has
elapsed, which has to be caught, because the default action for the
signal is to terminate the program. This signal also can’t be ignored
because the system calls like <code>pause</code> will not be interrupted, see the
second example below.</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<p>Canceling an alarm:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nix::unistd::alarm;
<span class="comment">// Set an alarm for 60 seconds from now.
</span>alarm::set(<span class="number">60</span>);
<span class="comment">// Cancel the above set alarm, which returns the number of seconds left
// of the previously set alarm.
</span><span class="macro">assert_eq!</span>(alarm::cancel(), <span class="prelude-val">Some</span>(<span class="number">60</span>));</code></pre></div>
<p>Scheduling an alarm and waiting for the signal:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::time::{Duration, Instant};
<span class="kw">use </span>nix::unistd::{alarm, pause};
<span class="kw">use </span>nix::sys::signal::<span class="kw-2">*</span>;
<span class="comment">// We need to setup an empty signal handler to catch the alarm signal,
// otherwise the program will be terminated once the signal is delivered.
</span><span class="kw">extern fn </span>signal_handler(<span class="kw">_</span>: nix::libc::c_int) { }
<span class="kw">let </span>sa = SigAction::new(
SigHandler::Handler(signal_handler),
SaFlags::SA_RESTART,
SigSet::empty()
);
<span class="kw">unsafe </span>{
sigaction(Signal::SIGALRM, <span class="kw-2">&amp;</span>sa);
}
<span class="kw">let </span>start = Instant::now();
<span class="comment">// Set an alarm for 1 second from now.
</span>alarm::set(<span class="number">1</span>);
<span class="comment">// Pause the process until the alarm signal is received.
</span><span class="kw">let </span><span class="kw-2">mut </span>sigset = SigSet::empty();
sigset.add(Signal::SIGALRM);
sigset.wait();
<span class="macro">assert!</span>(start.elapsed() &gt;= Duration::from_secs(<span class="number">1</span>));</code></pre></div>
<h2 id="references"><a href="#references">References</a></h2>
<p>See also <a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/alarm.html">alarm(2)</a>.</p>
</div></details><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.cancel.html" title="nix::unistd::alarm::cancel fn">cancel</a></div><div class="item-right docblock-short">Cancel an previously set alarm signal.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.set.html" title="nix::unistd::alarm::set fn">set</a></div><div class="item-right docblock-short">Schedule an alarm signal.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="nix" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>