Debian Codegraph @

libhylafax/Timeout.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include <unistd.h>
27 #include <signal.h>
28 #include <sys/time.h>
29 #include "Timeout.h"
30
31 bool Timeout::timerExpired = false;
32
33 Timeout::Timeout() {}
34 Timeout::~Timeout() {}
35 void Timeout::traceTimer(const char* ...) {}
36
37 void
38 Timeout::sigAlarm(int)
39 {
40 Timeout::timerExpired = true;
41 }
42
43 #ifndef SA_INTERRUPT
44 #define SA_INTERRUPT 0
45 #endif
46
47 void
48 Timeout::startTimeout(long ms)
49 {
50 timerExpired = false;
51 #ifdef SA_NOCLDSTOP /* POSIX */
52 static struct sigaction sa;
53 sa.sa_handler = fxSIGACTIONHANDLER(Timeout::sigAlarm);
54 sa.sa_flags = SA_INTERRUPT;
55 sigaction(SIGALRM, &sa, (struct sigaction*) 0);
56 #else
57 #ifdef SV_INTERRUPT /* BSD-style */
58 static struct sigvec sv;
59 sv.sv_handler = fxSIGVECHANDLER(Timeout::sigAlarm);
60 sv.sv_flags = SV_INTERRUPT;
61 sigvec(SIGALRM, &sv, (struct sigvec*) 0);
62 #else /* System V-style */
63 signal(SIGALRM, fxSIGHANDLER(sigAlarm));
64 #endif
65 #endif
66 #ifdef ITIMER_REAL
67 itimerval itv;
68 itv.it_value.tv_sec = ms / 1000;
69 itv.it_value.tv_usec = (ms % 1000) * 1000;
70 timerclear(&itv.it_interval);
71 (void) setitimer(ITIMER_REAL, &itv, (itimerval*) 0);
72 traceTimer("START %ld.%02ld second timeout",
73 itv.it_value.tv_sec, itv.it_value.tv_usec / 10000);
74 #else
75 long secs = howmany(ms, 1000);
76 (void) alarm(secs);
77 traceTimer("START %ld second timeout", secs);
78 #endif
79 }
80
81 void
82 Timeout::stopTimeout()
83 {
84 #ifdef ITIMER_REAL
85 static itimerval itv = { { 0, 0 }, { 0, 0 } };
86 (void) setitimer(ITIMER_REAL, &itv, (itimerval*) 0);
87 #else
88 (void) alarm(0);
89 #endif
90 traceTimer("STOP timeout%s", timerExpired ? ", timer expired" : "");
91 }