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
22 #include "timeeventq.h"
25 static gint compare(gconstpointer _a, gconstpointer _b) {
26 const AvahiTimeEvent *a = _a, *b = _b;
28 return avahi_timeval_compare(&a->expiry, &b->expiry);
31 static gboolean prepare_func(GSource *source, gint *timeout) {
32 AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
39 if (!q->prioq->root) {
44 e = q->prioq->root->data;
47 g_source_get_current_time(source, &now);
49 if (avahi_timeval_compare(&now, &e->expiry) >= 0) {
54 *timeout = (gint) (avahi_timeval_diff(&e->expiry, &now)/1000);
59 static gboolean check_func(GSource *source) {
60 AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
69 e = q->prioq->root->data;
72 g_source_get_current_time(source, &now);
74 return avahi_timeval_compare(&now, &e->expiry) >= 0;
77 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
78 AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
83 g_source_get_current_time(source, &now);
85 while (q->prioq->root) {
86 AvahiTimeEvent *e = q->prioq->root->data;
88 if (avahi_timeval_compare(&now, &e->expiry) < 0)
91 g_assert(e->callback);
92 e->callback(e, e->userdata);
98 AvahiTimeEventQueue* avahi_time_event_queue_new(GMainContext *context, gint priority) {
99 AvahiTimeEventQueue *q;
101 static GSourceFuncs source_funcs = {
110 q = (AvahiTimeEventQueue*) g_source_new(&source_funcs, sizeof(AvahiTimeEventQueue));
111 q->prioq = avahi_prio_queue_new(compare);
113 g_source_set_priority((GSource*) q, priority);
115 g_source_attach(&q->source, context);
120 void avahi_time_event_queue_free(AvahiTimeEventQueue *q) {
123 while (q->prioq->root)
124 avahi_time_event_queue_remove(q, q->prioq->root->data);
125 avahi_prio_queue_free(q->prioq);
127 g_source_destroy(&q->source);
128 g_source_unref(&q->source);
131 AvahiTimeEvent* avahi_time_event_queue_add(AvahiTimeEventQueue *q, const GTimeVal *timeval, void (*callback)(AvahiTimeEvent *e, void *userdata), void *userdata) {
139 e = g_new(AvahiTimeEvent, 1);
141 e->expiry = *timeval;
142 e->callback = callback;
143 e->userdata = userdata;
145 e->node = avahi_prio_queue_put(q->prioq, e);
150 void avahi_time_event_queue_remove(AvahiTimeEventQueue *q, AvahiTimeEvent *e) {
153 g_assert(e->queue == q);
155 avahi_prio_queue_remove(q->prioq, e->node);
159 void avahi_time_event_queue_update(AvahiTimeEventQueue *q, AvahiTimeEvent *e, const GTimeVal *timeval) {
162 g_assert(e->queue == q);
164 e->expiry = *timeval;
166 avahi_prio_queue_shuffle(q->prioq, e->node);
169 AvahiTimeEvent* avahi_time_event_queue_root(AvahiTimeEventQueue *q) {
172 return q->prioq->root ? q->prioq->root->data : NULL;
175 AvahiTimeEvent* avahi_time_event_next(AvahiTimeEvent *e) {
178 return e->node->next->data;