1 |
/* timers.h - header file for timers package |
2 |
** |
3 |
** Copyright © 1995,1998,1999,2000 by Jef Poskanzer <jef@acme.com>. |
4 |
** All rights reserved. |
5 |
** |
6 |
** Redistribution and use in source and binary forms, with or without |
7 |
** modification, are permitted provided that the following conditions |
8 |
** are met: |
9 |
** 1. Redistributions of source code must retain the above copyright |
10 |
** notice, this list of conditions and the following disclaimer. |
11 |
** 2. Redistributions in binary form must reproduce the above copyright |
12 |
** notice, this list of conditions and the following disclaimer in the |
13 |
** documentation and/or other materials provided with the distribution. |
14 |
** |
15 |
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
** SUCH DAMAGE. |
26 |
*/ |
27 |
|
28 |
#ifndef _TIMERS_H_ |
29 |
#define _TIMERS_H_ |
30 |
|
31 |
#include <sys/time.h> |
32 |
|
33 |
#ifndef INFTIM |
34 |
#define INFTIM -1 |
35 |
#endif /* INFTIM */ |
36 |
|
37 |
/* ClientData is a random value that tags along with a timer. The client |
38 |
** can use it for whatever, and it gets passed to the callback when the |
39 |
** timer triggers. |
40 |
*/ |
41 |
typedef union { |
42 |
void* p; |
43 |
int i; |
44 |
long l; |
45 |
} ClientData; |
46 |
|
47 |
extern ClientData JunkClientData; /* for use when you don't care */ |
48 |
|
49 |
/* The TimerProc gets called when the timer expires. It gets passed |
50 |
** the ClientData associated with the timer, and a timeval in case |
51 |
** it wants to schedule another timer. |
52 |
*/ |
53 |
typedef void TimerProc( ClientData client_data, struct timeval* nowP ); |
54 |
|
55 |
/* The Timer struct. */ |
56 |
typedef struct TimerStruct { |
57 |
TimerProc* timer_proc; |
58 |
ClientData client_data; |
59 |
long msecs; |
60 |
int periodic; |
61 |
struct timeval time; |
62 |
struct TimerStruct* prev; |
63 |
struct TimerStruct* next; |
64 |
int hash; |
65 |
} Timer; |
66 |
|
67 |
/* Initialize the timer package. */ |
68 |
extern void tmr_init( void ); |
69 |
|
70 |
/* Set up a timer, either periodic or one-shot. Returns (Timer*) 0 on errors. */ |
71 |
extern Timer* tmr_create( |
72 |
struct timeval* nowP, TimerProc* timer_proc, ClientData client_data, |
73 |
long msecs, int periodic ); |
74 |
|
75 |
/* Returns a timeout indicating how long until the next timer triggers. You |
76 |
** can just put the call to this routine right in your select(). Returns |
77 |
** (struct timeval*) 0 if no timers are pending. |
78 |
*/ |
79 |
extern struct timeval* tmr_timeout( struct timeval* nowP ); |
80 |
|
81 |
/* Returns a timeout in milliseconds indicating how long until the next timer |
82 |
** triggers. You can just put the call to this routine right in your poll(). |
83 |
** Returns INFTIM (-1) if no timers are pending. |
84 |
*/ |
85 |
extern long tmr_mstimeout( struct timeval* nowP ); |
86 |
|
87 |
/* Run the list of timers. Your main program needs to call this every so often, |
88 |
** or as indicated by tmr_timeout(). |
89 |
*/ |
90 |
extern void tmr_run( struct timeval* nowP ); |
91 |
|
92 |
/* Reset the clock on a timer, to current time plus the original timeout. */ |
93 |
extern void tmr_reset( struct timeval* nowP, Timer* timer ); |
94 |
|
95 |
/* Deschedule a timer. Note that non-periodic timers are automatically |
96 |
** descheduled when they run, so you don't have to call this on them. |
97 |
*/ |
98 |
extern void tmr_cancel( Timer* timer ); |
99 |
|
100 |
/* Clean up the timers package, freeing any unused storage. */ |
101 |
extern void tmr_cleanup( void ); |
102 |
|
103 |
/* Cancel all timers and free storage, usually in preparation for exitting. */ |
104 |
extern void tmr_destroy( void ); |
105 |
|
106 |
/* Generate debugging statistics syslog message. */ |
107 |
extern void tmr_logstats( long secs ); |
108 |
|
109 |
#endif /* _TIMERS_H_ */ |