Merge from vendor branch ZLIB:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / include / isc / timer.h
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: timer.h,v 1.28.2.1 2004/03/09 06:12:03 marka Exp $ */
19
20 #ifndef ISC_TIMER_H
21 #define ISC_TIMER_H 1
22
23 /*****
24  ***** Module Info
25  *****/
26
27 /*
28  * Timers
29  *
30  * Provides timers which are event sources in the task system.
31  *
32  * Three types of timers are supported:
33  *
34  *      'ticker' timers generate a periodic tick event.
35  *
36  *      'once' timers generate an idle timeout event if they are idle for too
37  *      long, and generate a life timeout event if their lifetime expires.
38  *      They are used to implement both (possibly expiring) idle timers and
39  *      'one-shot' timers.
40  *
41  *      'inactive' timers generate no events.
42  *
43  * Timers can change type.  It is typical to create a timer as
44  * an 'inactive' timer and then change it into a 'ticker' or
45  * 'once' timer.
46  *
47  * MP:
48  *      The module ensures appropriate synchronization of data structures it
49  *      creates and manipulates.
50  *
51  *      Clients of this module must not be holding a timer's task's lock when
52  *      making a call that affects that timer.  Failure to follow this rule
53  *      can result in deadlock.
54  *
55  *      The caller must ensure that isc_timermgr_destroy() is called only
56  *      once for a given manager.
57  *
58  * Reliability:
59  *      No anticipated impact.
60  *
61  * Resources:
62  *      <TBS>
63  *
64  * Security:
65  *      No anticipated impact.
66  *
67  * Standards:
68  *      None.
69  */
70
71
72 /***
73  *** Imports
74  ***/
75
76 #include <isc/types.h>
77 #include <isc/event.h>
78 #include <isc/eventclass.h>
79 #include <isc/lang.h>
80
81 ISC_LANG_BEGINDECLS
82
83 /***
84  *** Types
85  ***/
86
87 typedef enum {
88         isc_timertype_ticker = 0,
89         isc_timertype_once = 1,
90         isc_timertype_inactive = 2
91 } isc_timertype_t;
92
93 typedef struct isc_timerevent {
94         struct isc_event        common;
95 } isc_timerevent_t;
96
97 #define ISC_TIMEREVENT_FIRSTEVENT       (ISC_EVENTCLASS_TIMER + 0)
98 #define ISC_TIMEREVENT_TICK             (ISC_EVENTCLASS_TIMER + 1)
99 #define ISC_TIMEREVENT_IDLE             (ISC_EVENTCLASS_TIMER + 2)
100 #define ISC_TIMEREVENT_LIFE             (ISC_EVENTCLASS_TIMER + 3)
101 #define ISC_TIMEREVENT_LASTEVENT        (ISC_EVENTCLASS_TIMER + 65535)
102
103 /***
104  *** Timer and Timer Manager Functions
105  ***
106  *** Note: all Ensures conditions apply only if the result is success for
107  *** those functions which return an isc_result_t.
108  ***/
109
110 isc_result_t
111 isc_timer_create(isc_timermgr_t *manager,
112                  isc_timertype_t type,
113                  isc_time_t *expires,
114                  isc_interval_t *interval,
115                  isc_task_t *task,
116                  isc_taskaction_t action,
117                  const void *arg,
118                  isc_timer_t **timerp);
119 /*
120  * Create a new 'type' timer managed by 'manager'.  The timers parameters
121  * are specified by 'expires' and 'interval'.  Events will be posted to
122  * 'task' and when dispatched 'action' will be called with 'arg' as the
123  * arg value.  The new timer is returned in 'timerp'.
124  *
125  * Notes:
126  *
127  *      For ticker timers, the timer will generate a 'tick' event every
128  *      'interval' seconds.  The value of 'expires' is ignored.
129  *
130  *      For once timers, 'expires' specifies the time when a life timeout
131  *      event should be generated.  If 'expires' is 0 (the epoch), then no life
132  *      timeout will be generated.  'interval' specifies how long the timer
133  *      can be idle before it generates an idle timeout.  If 0, then no
134  *      idle timeout will be generated.
135  *
136  *      If 'expires' is NULL, the epoch will be used.
137  *
138  *      If 'interval' is NULL, the zero interval will be used.
139  *
140  * Requires:
141  *
142  *      'manager' is a valid manager
143  *
144  *      'task' is a valid task
145  *
146  *      'action' is a valid action
147  *
148  *      'expires' points to a valid time, or is NULL.
149  *
150  *      'interval' points to a valid interval, or is NULL.
151  *
152  *      type == isc_timertype_inactive ||
153  *      ('expires' and 'interval' are not both 0)
154  *
155  *      'timerp' is a valid pointer, and *timerp == NULL
156  *
157  * Ensures:
158  *
159  *      '*timerp' is attached to the newly created timer
160  *
161  *      The timer is attached to the task
162  *
163  *      An idle timeout will not be generated until at least Now + the
164  *      timer's interval if 'timer' is a once timer with a non-zero
165  *      interval.
166  *
167  * Returns:
168  *
169  *      Success
170  *      No memory
171  *      Unexpected error
172  */
173
174 isc_result_t
175 isc_timer_reset(isc_timer_t *timer,
176                 isc_timertype_t type,
177                 isc_time_t *expires,
178                 isc_interval_t *interval,
179                 isc_boolean_t purge);
180 /*
181  * Change the timer's type, expires, and interval values to the given
182  * values.  If 'purge' is TRUE, any pending events from this timer
183  * are purged from its task's event queue.
184  *
185  * Notes:
186  *
187  *      If 'expires' is NULL, the epoch will be used.
188  *
189  *      If 'interval' is NULL, the zero interval will be used.
190  *
191  * Requires:
192  *
193  *      'timer' is a valid timer
194  *
195  *      The same requirements that isc_timer_create() imposes on 'type',
196  *      'expires' and 'interval' apply.
197  *
198  * Ensures:
199  *
200  *      An idle timeout will not be generated until at least Now + the
201  *      timer's interval if 'timer' is a once timer with a non-zero
202  *      interval.
203  *
204  * Returns:
205  *
206  *      Success
207  *      No memory
208  *      Unexpected error
209  */
210
211 isc_result_t
212 isc_timer_touch(isc_timer_t *timer);
213 /*
214  * Set the last-touched time of 'timer' to the current time.
215  *
216  * Requires:
217  *
218  *      'timer' is a valid once timer.
219  *
220  * Ensures:
221  *
222  *      An idle timeout will not be generated until at least Now + the
223  *      timer's interval if 'timer' is a once timer with a non-zero
224  *      interval.
225  *
226  * Returns:
227  *
228  *      Success
229  *      Unexpected error
230  */
231
232 void
233 isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp);
234 /*
235  * Attach *timerp to timer.
236  *
237  * Requires:
238  *
239  *      'timer' is a valid timer.
240  *
241  *      'timerp' points to a NULL timer.
242  *
243  * Ensures:
244  *
245  *      *timerp is attached to timer.
246  */
247
248 void
249 isc_timer_detach(isc_timer_t **timerp);
250 /*
251  * Detach *timerp from its timer.
252  *
253  * Requires:
254  *
255  *      'timerp' points to a valid timer.
256  *
257  * Ensures:
258  *
259  *      *timerp is NULL.
260  *
261  *      If '*timerp' is the last reference to the timer,
262  *      then:
263  *
264  *              The timer will be shutdown
265  *
266  *              The timer will detach from its task
267  *
268  *              All resources used by the timer have been freed
269  *
270  *              Any events already posted by the timer will be purged.
271  *              Therefore, if isc_timer_detach() is called in the context
272  *              of the timer's task, it is guaranteed that no more
273  *              timer event callbacks will run after the call.
274  */
275
276 isc_result_t
277 isc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp);
278 /*
279  * Create a timer manager.
280  *
281  * Notes:
282  *
283  *      All memory will be allocated in memory context 'mctx'.
284  *
285  * Requires:
286  *
287  *      'mctx' is a valid memory context.
288  *
289  *      'managerp' points to a NULL isc_timermgr_t.
290  *
291  * Ensures:
292  *
293  *      '*managerp' is a valid isc_timermgr_t.
294  *
295  * Returns:
296  *
297  *      Success
298  *      No memory
299  *      Unexpected error
300  */
301
302 void
303 isc_timermgr_destroy(isc_timermgr_t **managerp);
304 /*
305  * Destroy a timer manager.
306  *
307  * Notes:
308  *
309  *      This routine blocks until there are no timers left in the manager,
310  *      so if the caller holds any timer references using the manager, it
311  *      must detach them before calling isc_timermgr_destroy() or it will
312  *      block forever.
313  *
314  * Requires:
315  *
316  *      '*managerp' is a valid isc_timermgr_t.
317  *
318  * Ensures:
319  *
320  *      *managerp == NULL
321  *
322  *      All resources used by the manager have been freed.
323  */
324
325 ISC_LANG_ENDDECLS
326
327 #endif /* ISC_TIMER_H */