4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <avahi-common/timeval.h>
30 #include <avahi-common/malloc.h>
32 #include "timeeventq.h"
35 struct AvahiTimeEvent {
36 AvahiTimeEventQueue *queue;
37 AvahiPrioQueueNode *node;
38 struct timeval expiry;
39 struct timeval last_run;
40 AvahiTimeEventCallback callback;
44 struct AvahiTimeEventQueue {
45 const AvahiPoll *poll_api;
46 AvahiPrioQueue *prioq;
47 AvahiTimeout *timeout;
50 static int compare(const void* _a, const void* _b) {
51 const AvahiTimeEvent *a = _a, *b = _b;
54 if ((ret = avahi_timeval_compare(&a->expiry, &b->expiry)) != 0)
57 /* If both exevents are scheduled for the same time, put the entry
58 * that has been run earlier the last time first. */
59 return avahi_timeval_compare(&a->last_run, &b->last_run);
62 static AvahiTimeEvent* time_event_queue_root(AvahiTimeEventQueue *q) {
65 return q->prioq->root ? q->prioq->root->data : NULL;
68 static void update_timeout(AvahiTimeEventQueue *q) {
72 if ((e = time_event_queue_root(q)))
73 q->poll_api->timeout_update(q->timeout, &e->expiry);
75 q->poll_api->timeout_update(q->timeout, NULL);
78 static void expiration_event(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void *userdata) {
79 AvahiTimeEventQueue *q = userdata;
82 if ((e = time_event_queue_root(q))) {
85 gettimeofday(&now, NULL);
87 /* Check if expired */
88 if (avahi_timeval_compare(&now, &e->expiry) >= 0) {
90 /* Make sure to move the entry away from the front */
92 avahi_prio_queue_shuffle(q->prioq, e->node);
96 e->callback(e, e->userdata);
103 avahi_log_debug(__FILE__": Strange, expiration_event() called, but nothing really happened.");
107 static void fix_expiry_time(AvahiTimeEvent *e) {
111 return; /*** DO WE REALLY NEED THIS? ***/
113 gettimeofday(&now, NULL);
115 if (avahi_timeval_compare(&now, &e->expiry) > 0)
119 AvahiTimeEventQueue* avahi_time_event_queue_new(const AvahiPoll *poll_api) {
120 AvahiTimeEventQueue *q;
122 if (!(q = avahi_new(AvahiTimeEventQueue, 1))) {
123 avahi_log_error(__FILE__": Out of memory");
127 q->poll_api = poll_api;
129 if (!(q->prioq = avahi_prio_queue_new(compare)))
132 if (!(q->timeout = poll_api->timeout_new(poll_api, NULL, expiration_event, q)))
143 avahi_prio_queue_free(q->prioq);
149 void avahi_time_event_queue_free(AvahiTimeEventQueue *q) {
154 while ((e = time_event_queue_root(q)))
155 avahi_time_event_free(e);
156 avahi_prio_queue_free(q->prioq);
158 q->poll_api->timeout_free(q->timeout);
163 AvahiTimeEvent* avahi_time_event_new(
164 AvahiTimeEventQueue *q,
165 const struct timeval *timeval,
166 AvahiTimeEventCallback callback,
175 if (!(e = avahi_new(AvahiTimeEvent, 1))) {
176 avahi_log_error(__FILE__": Out of memory");
177 return NULL; /* OOM */
181 e->callback = callback;
182 e->userdata = userdata;
185 e->expiry = *timeval;
187 e->expiry.tv_sec = 0;
188 e->expiry.tv_usec = 0;
193 e->last_run.tv_sec = 0;
194 e->last_run.tv_usec = 0;
196 if (!(e->node = avahi_prio_queue_put(q->prioq, e))) {
205 void avahi_time_event_free(AvahiTimeEvent *e) {
206 AvahiTimeEventQueue *q;
211 avahi_prio_queue_remove(q->prioq, e->node);
217 void avahi_time_event_update(AvahiTimeEvent *e, const struct timeval *timeval) {
221 e->expiry = *timeval;
223 avahi_prio_queue_shuffle(e->queue->prioq, e->node);
225 update_timeout(e->queue);