2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <avahi-common/timeval.h>
28 #include <avahi-common/malloc.h>
30 #include "timeeventq.h"
33 struct AvahiTimeEvent {
34 AvahiTimeEventQueue *queue;
35 AvahiPrioQueueNode *node;
36 struct timeval expiry;
37 struct timeval last_run;
38 AvahiTimeEventCallback callback;
42 struct AvahiTimeEventQueue {
43 const AvahiPoll *poll_api;
44 AvahiPrioQueue *prioq;
45 AvahiTimeout *timeout;
48 static int compare(const void* _a, const void* _b) {
49 const AvahiTimeEvent *a = _a, *b = _b;
52 if ((ret = avahi_timeval_compare(&a->expiry, &b->expiry)) != 0)
55 /* If both exevents are scheduled for the same time, put the entry
56 * that has been run earlier the last time first. */
57 return avahi_timeval_compare(&a->last_run, &b->last_run);
60 static AvahiTimeEvent* time_event_queue_root(AvahiTimeEventQueue *q) {
63 return q->prioq->root ? q->prioq->root->data : NULL;
66 static void update_timeout(AvahiTimeEventQueue *q) {
70 if ((e = time_event_queue_root(q)))
71 q->poll_api->timeout_update(q->timeout, &e->expiry);
73 q->poll_api->timeout_update(q->timeout, NULL);
76 static void expiration_event(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void *userdata) {
77 AvahiTimeEventQueue *q = userdata;
80 if ((e = time_event_queue_root(q))) {
83 gettimeofday(&now, NULL);
85 /* Check if expired */
86 if (avahi_timeval_compare(&now, &e->expiry) >= 0) {
88 /* Make sure to move the entry away from the front */
90 avahi_prio_queue_shuffle(q->prioq, e->node);
94 e->callback(e, e->userdata);
101 avahi_log_debug(__FILE__": Strange, expiration_event() called, but nothing really happened.");
105 static void fix_expiry_time(AvahiTimeEvent *e) {
109 return; /*** DO WE REALLY NEED THIS? ***/
111 gettimeofday(&now, NULL);
113 if (avahi_timeval_compare(&now, &e->expiry) > 0)
117 AvahiTimeEventQueue* avahi_time_event_queue_new(const AvahiPoll *poll_api) {
118 AvahiTimeEventQueue *q;
120 if (!(q = avahi_new(AvahiTimeEventQueue, 1))) {
121 avahi_log_error(__FILE__": Out of memory");
125 q->poll_api = poll_api;
127 if (!(q->prioq = avahi_prio_queue_new(compare)))
130 if (!(q->timeout = poll_api->timeout_new(poll_api, NULL, expiration_event, q)))
141 avahi_prio_queue_free(q->prioq);
147 void avahi_time_event_queue_free(AvahiTimeEventQueue *q) {
152 while ((e = time_event_queue_root(q)))
153 avahi_time_event_free(e);
154 avahi_prio_queue_free(q->prioq);
156 q->poll_api->timeout_free(q->timeout);
161 AvahiTimeEvent* avahi_time_event_new(
162 AvahiTimeEventQueue *q,
163 const struct timeval *timeval,
164 AvahiTimeEventCallback callback,
173 if (!(e = avahi_new(AvahiTimeEvent, 1))) {
174 avahi_log_error(__FILE__": Out of memory");
175 return NULL; /* OOM */
179 e->callback = callback;
180 e->userdata = userdata;
183 e->expiry = *timeval;
185 e->expiry.tv_sec = 0;
186 e->expiry.tv_usec = 0;
191 e->last_run.tv_sec = 0;
192 e->last_run.tv_usec = 0;
194 if (!(e->node = avahi_prio_queue_put(q->prioq, e))) {
203 void avahi_time_event_free(AvahiTimeEvent *e) {
204 AvahiTimeEventQueue *q;
209 avahi_prio_queue_remove(q->prioq, e->node);
215 void avahi_time_event_update(AvahiTimeEvent *e, const struct timeval *timeval) {
219 e->expiry = *timeval;
221 avahi_prio_queue_shuffle(e->queue->prioq, e->node);
223 update_timeout(e->queue);