4
* Timeout functions for the Common UNIX Printing System (CUPS).
6
* Copyright (C) 2010, 2011 Red Hat, Inc.
8
* Tim Waugh <twaugh@redhat.com>
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
14
* Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
17
* Redistributions in binary form must reproduce the above copyright
18
* notice, this list of conditions and the following disclaimer in the
19
* documentation and/or other materials provided with the distribution.
21
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32
* OF THE POSSIBILITY OF SUCH DAMAGE.
36
* cupsdInitTimeouts() - Initialise timeout structure.
37
* cupsdAddTimeout() - Add a timed callback.
38
* cupsdNextTimeout() - Find the next enabled timed callback.
39
* cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
40
* cupsdRemoveTimeout() - Discard a timed callback.
41
* compare_timeouts() - Compare timed callbacks for array sorting.
46
#ifdef HAVE_AVAHI /* Applies to entire file... */
49
* Include necessary headers...
54
#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
56
#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
59
# include <avahi-common/timeval.h>
60
#endif /* HAVE_AVAHI */
63
struct _cupsd_timeout_s
67
cupsd_timeoutfunc_t callback;
76
* 'compare_timeouts()' - Compare timed callbacks for array sorting.
80
compare_addrs (void *p0, void *p1)
90
compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
92
int addrsdiff = compare_addrs (p0, p1);
98
if (!p0->enabled || !p1->enabled)
100
if (!p0->enabled && !p1->enabled)
103
return (p0->enabled ? -1 : 1);
106
tvdiff = avahi_timeval_compare (&p0->when, &p1->when);
115
* 'cupsdInitTimeouts()' - Initialise timeout structures.
119
cupsdInitTimeouts(void)
121
Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
126
* 'cupsdAddTimeout()' - Add a timed callback.
129
cupsd_timeout_t * /* O - Timeout handle */
130
cupsdAddTimeout(const struct timeval *tv, /* I - Absolute time */
131
cupsd_timeoutfunc_t cb, /* I - Callback function */
132
void *data) /* I - User data */
134
cupsd_timeout_t *timeout;
136
timeout = malloc (sizeof(cupsd_timeout_t));
139
timeout->enabled = (tv != NULL);
142
timeout->when.tv_sec = tv->tv_sec;
143
timeout->when.tv_usec = tv->tv_usec;
146
timeout->callback = cb;
147
timeout->data = data;
148
cupsArrayAdd (Timeouts, timeout);
156
* 'cupsdNextTimeout()' - Find the next enabled timed callback.
159
cupsd_timeout_t * /* O - Next enabled timeout or NULL */
160
cupsdNextTimeout(long *delay) /* O - Seconds before scheduled */
162
cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
163
struct timeval curtime;
165
if (first && !first->enabled)
170
gettimeofday (&curtime, NULL);
171
if (avahi_timeval_compare (&curtime, &first->when) > 0)
175
*delay = 1 + first->when.tv_sec - curtime.tv_sec;
176
if (first->when.tv_usec < curtime.tv_usec)
186
* 'cupsdRunTimeout()' - Run a timed callback.
190
cupsdRunTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
194
timeout->enabled = 0;
195
if (!timeout->callback)
197
timeout->callback (timeout, timeout->data);
201
* 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
205
cupsdUpdateTimeout(cupsd_timeout_t *timeout, /* I - Timeout */
206
const struct timeval *tv) /* I - Absolute time or NULL */
208
cupsArrayRemove (Timeouts, timeout);
209
timeout->enabled = (tv != NULL);
212
timeout->when.tv_sec = tv->tv_sec;
213
timeout->when.tv_usec = tv->tv_usec;
215
cupsArrayAdd (Timeouts, timeout);
220
* 'cupsdRemoveTimeout()' - Discard a timed callback.
224
cupsdRemoveTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
226
cupsArrayRemove (Timeouts, timeout);
231
#endif /* HAVE_AVAHI ... from top of file */