X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Ftimeeventq.c;h=2799bf242602171d69b96b878e61bb4f8de4d05b;hb=abf0d60e6dd933273887c1600e0793eac2706438;hp=b069ffd14cb2ea23a0441ffd5a5a2b6b2ad1167f;hpb=4f0a5e7572a4257894b4bfede42c26d65152609e;p=catta diff --git a/avahi-core/timeeventq.c b/avahi-core/timeeventq.c index b069ffd..2799bf2 100644 --- a/avahi-core/timeeventq.c +++ b/avahi-core/timeeventq.c @@ -1,18 +1,16 @@ -/* $Id$ */ - /*** This file is part of avahi. - + avahi is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + avahi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with avahi; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 @@ -42,8 +40,9 @@ struct AvahiTimeEvent { }; struct AvahiTimeEventQueue { - AvahiPoll *poll_api; + const AvahiPoll *poll_api; AvahiPrioQueue *prioq; + AvahiTimeout *timeout; }; static int compare(const void* _a, const void* _b) { @@ -58,26 +57,30 @@ static int compare(const void* _a, const void* _b) { return avahi_timeval_compare(&a->last_run, &b->last_run); } -static void expiration_event(AvahiPoll *poll_api, void *userdata); +static AvahiTimeEvent* time_event_queue_root(AvahiTimeEventQueue *q) { + assert(q); + + return q->prioq->root ? q->prioq->root->data : NULL; +} -static void update_wakeup(AvahiTimeEventQueue *q) { +static void update_timeout(AvahiTimeEventQueue *q) { + AvahiTimeEvent *e; assert(q); - if (q->prioq->root) { - AvahiTimeEvent *e = q->prioq->root->data; - q->poll_api->set_wakeup(q->poll_api, &e->expiry, expiration_event, q); - } else - q->poll_api->set_wakeup(q->poll_api, NULL, NULL, NULL); + if ((e = time_event_queue_root(q))) + q->poll_api->timeout_update(q->timeout, &e->expiry); + else + q->poll_api->timeout_update(q->timeout, NULL); } -void expiration_event(AvahiPoll *poll_api, void *userdata) { - struct timeval now; +static void expiration_event(AVAHI_GCC_UNUSED AvahiTimeout *timeout, void *userdata) { AvahiTimeEventQueue *q = userdata; AvahiTimeEvent *e; - gettimeofday(&now, NULL); - - if ((e = avahi_time_event_queue_root(q))) { + if ((e = time_event_queue_root(q))) { + struct timeval now; + + gettimeofday(&now, NULL); /* Check if expired */ if (avahi_timeval_compare(&now, &e->expiry) >= 0) { @@ -89,23 +92,29 @@ void expiration_event(AvahiPoll *poll_api, void *userdata) { /* Run it */ assert(e->callback); e->callback(e, e->userdata); + + update_timeout(q); + return; } } - update_wakeup(q); + avahi_log_debug(__FILE__": Strange, expiration_event() called, but nothing really happened."); + update_timeout(q); } static void fix_expiry_time(AvahiTimeEvent *e) { struct timeval now; assert(e); + return; /*** DO WE REALLY NEED THIS? ***/ + gettimeofday(&now, NULL); if (avahi_timeval_compare(&now, &e->expiry) > 0) e->expiry = now; } -AvahiTimeEventQueue* avahi_time_event_queue_new(AvahiPoll *poll_api) { +AvahiTimeEventQueue* avahi_time_event_queue_new(const AvahiPoll *poll_api) { AvahiTimeEventQueue *q; if (!(q = avahi_new(AvahiTimeEventQueue, 1))) { @@ -113,27 +122,39 @@ AvahiTimeEventQueue* avahi_time_event_queue_new(AvahiPoll *poll_api) { goto oom; } + q->poll_api = poll_api; + if (!(q->prioq = avahi_prio_queue_new(compare))) goto oom; - q->poll_api = poll_api; + if (!(q->timeout = poll_api->timeout_new(poll_api, NULL, expiration_event, q))) + goto oom; + return q; oom: - if (q) + if (q) { avahi_free(q); - + + if (q->prioq) + avahi_prio_queue_free(q->prioq); + } + return NULL; } void avahi_time_event_queue_free(AvahiTimeEventQueue *q) { + AvahiTimeEvent *e; + assert(q); - while (q->prioq->root) - avahi_time_event_free(q->prioq->root->data); + while ((e = time_event_queue_root(q))) + avahi_time_event_free(e); avahi_prio_queue_free(q->prioq); + q->poll_api->timeout_free(q->timeout); + avahi_free(q); } @@ -142,9 +163,9 @@ AvahiTimeEvent* avahi_time_event_new( const struct timeval *timeval, AvahiTimeEventCallback callback, void* userdata) { - + AvahiTimeEvent *e; - + assert(q); assert(callback); assert(userdata); @@ -153,7 +174,7 @@ AvahiTimeEvent* avahi_time_event_new( avahi_log_error(__FILE__": Out of memory"); return NULL; /* OOM */ } - + e->queue = q; e->callback = callback; e->userdata = userdata; @@ -164,9 +185,9 @@ AvahiTimeEvent* avahi_time_event_new( e->expiry.tv_sec = 0; e->expiry.tv_usec = 0; } - + fix_expiry_time(e); - + e->last_run.tv_sec = 0; e->last_run.tv_usec = 0; @@ -175,7 +196,7 @@ AvahiTimeEvent* avahi_time_event_new( return NULL; } - update_wakeup(q); + update_timeout(q); return e; } @@ -188,7 +209,7 @@ void avahi_time_event_free(AvahiTimeEvent *e) { avahi_prio_queue_remove(q->prioq, e->node); avahi_free(e); - update_wakeup(q); + update_timeout(q); } void avahi_time_event_update(AvahiTimeEvent *e, const struct timeval *timeval) { @@ -198,20 +219,7 @@ void avahi_time_event_update(AvahiTimeEvent *e, const struct timeval *timeval) { e->expiry = *timeval; fix_expiry_time(e); avahi_prio_queue_shuffle(e->queue->prioq, e->node); - - update_wakeup(e->queue); -} - -AvahiTimeEvent* avahi_time_event_queue_root(AvahiTimeEventQueue *q) { - assert(q); - return q->prioq->root ? q->prioq->root->data : NULL; + update_timeout(e->queue); } -AvahiTimeEvent* avahi_time_event_next(AvahiTimeEvent *e) { - assert(e); - - return e->node->next->data; -} - -