about summary refs log tree commit diff
path: root/doc/libstddjb/alarm.html
blob: a6cc748ed19e62ea5a25a19bf362d901014d3d7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="en" />
    <title>skalibs: the alarm library interface</title>
    <meta name="Description" content="skalibs: the alarm library interface" />
    <meta name="Keywords" content="skalibs c unix alarm getitimer setitimer timer_create timer_gettime timer_settime library libstddjb" />
    <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
  </head>
<body>

<p>
<a href="index.html">libstddjb</a><br />
<a href="../libskarnet.html">libskarnet</a><br />
<a href="../index.html">skalibs</a><br />
<a href="//skarnet.org/software/">Software</a><br />
<a href="//skarnet.org/">skarnet.org</a>
</p>

<h1> The <tt>alarm</tt> library interface </h1>

<p>
 The following functions are declared in the <tt>skalibs/alarm.h</tt> header,
and implemented in the <tt>libskarnet.a</tt> or <tt>libskarnet.so</tt> library.
</p>

<h2> General information </h2>

<p>
 <tt>alarm</tt> is a set of primitives to provide the same functionality as
<a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/alarm.html">alarm()</a>,
but with sub-second precision.
</p>

<p>
 Depending on the functionality the underlying system provides,
the precision can be 1 nanosecond (implementation via
<a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html">timer_settime()</a>,
1 microsecond (implementation via
<a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/setitimer.html">setitimer()</a>,
or 1 second (fallback implementation with
<a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/alarm.html">alarm()</a>
when nothing better can be found).
</p>

<h2> Functions </h2>

<p>
<code> int alarm_milliseconds (unsigned int n) </code> <br />
Sets a fuse that will raise a SIGALRM after <em>n</em> milliseconds.
If <em>n</em> is 0, the SIGALRM will be raised instantly.
Returns 1 on success and 0 (and sets errno) on failure.
</p>

<p>
<code> int alarm_timeout (tain_t const *tto) </code> <br />
Sets a fuse that will raise a SIGALRM after some amount
of time has passed. The amount of time is described in
*<em>tto</em>, which is a relative
<a href="tai.html">tain_t</a>, i.e. a structure containing
a relative TAIN64 time.
Returns 1 on success and 0 (and sets errno) on failure.
</p>

<p>
<code> int alarm_deadline (tain_t const *deadline) </code> <br />
Sets a fuse that will raise a SIGALRM when the clock reaches
*<em>deadline</em>, which is an absolute time expressed in
<a href="tai.html">TAI64N</a> format.
Returns 1 on success and 0 (and sets errno) on failure.
</p>

<p>
<code> void alarm_disable (void) </code> <br />
Cancels a previously set fuse. No SIGALRM will be raised.
</p>

<h2> Notes </h2>

<ul>
 <li> Asynchronous programming via signals is bad. The best way
to handle situations where something happens after some time has
elapsed is to use an asynchronous loop primitive such as
<a href="iopause.html">iopause()</a>. The problem is that some
external libraries only provide synchronous functions (including
functions talking to the network!) with no obvious way to set a
timeout. The <tt>alarm_*</tt> set of functions is meant to work
around that, with hopefully better granularity than the POSIX
<tt>alarm()</tt> function. </li>
</ul>

</body>
</html>