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
26 #include "timeeventq.h"
29 static gint compare(gconstpointer _a, gconstpointer _b) {
30 const AvahiTimeEvent *a = _a, *b = _b;
32 return avahi_timeval_compare(&a->expiry, &b->expiry);
35 static gboolean prepare_func(GSource *source, gint *timeout) {
36 AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
43 if (!q->prioq->root) {
48 e = q->prioq->root->data;
51 g_source_get_current_time(source, &now);
53 if (avahi_timeval_compare(&now, &e->expiry) >= 0) {
58 *timeout = (gint) (avahi_timeval_diff(&e->expiry, &now)/1000);
63 static gboolean check_func(GSource *source) {
64 AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
73 e = q->prioq->root->data;
76 g_source_get_current_time(source, &now);
78 return avahi_timeval_compare(&now, &e->expiry) >= 0;
81 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
82 AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
87 g_source_get_current_time(source, &now);
89 while (q->prioq->root) {
90 AvahiTimeEvent *e = q->prioq->root->data;
92 if (avahi_timeval_compare(&now, &e->expiry) < 0)
95 g_assert(e->callback);
96 e->callback(e, e->userdata);
102 AvahiTimeEventQueue* avahi_time_event_queue_new(GMainContext *context, gint priority) {
103 AvahiTimeEventQueue *q;
105 static GSourceFuncs source_funcs = {
114 q = (AvahiTimeEventQueue*) g_source_new(&source_funcs, sizeof(AvahiTimeEventQueue));
115 q->prioq = avahi_prio_queue_new(compare);
117 g_source_set_priority((GSource*) q, priority);
119 g_source_attach(&q->source, context);
124 void avahi_time_event_queue_free(AvahiTimeEventQueue *q) {
127 while (q->prioq->root)
128 avahi_time_event_queue_remove(q, q->prioq->root->data);
129 avahi_prio_queue_free(q->prioq);
131 g_source_destroy(&q->source);
132 g_source_unref(&q->source);
135 AvahiTimeEvent* avahi_time_event_queue_add(AvahiTimeEventQueue *q, const GTimeVal *timeval, void (*callback)(AvahiTimeEvent *e, void *userdata), void *userdata) {
143 e = g_new(AvahiTimeEvent, 1);
145 e->expiry = *timeval;
146 e->callback = callback;
147 e->userdata = userdata;
149 e->node = avahi_prio_queue_put(q->prioq, e);
154 void avahi_time_event_queue_remove(AvahiTimeEventQueue *q, AvahiTimeEvent *e) {
157 g_assert(e->queue == q);
159 avahi_prio_queue_remove(q->prioq, e->node);
163 void avahi_time_event_queue_update(AvahiTimeEventQueue *q, AvahiTimeEvent *e, const GTimeVal *timeval) {
166 g_assert(e->queue == q);
168 e->expiry = *timeval;
170 avahi_prio_queue_shuffle(q->prioq, e->node);
173 AvahiTimeEvent* avahi_time_event_queue_root(AvahiTimeEventQueue *q) {
176 return q->prioq->root ? q->prioq->root->data : NULL;
179 AvahiTimeEvent* avahi_time_event_next(AvahiTimeEvent *e) {
182 return e->node->next->data;