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;
33 if ((ret = avahi_timeval_compare(&a->expiry, &b->expiry)) != 0)
36 /* If both exevents are scheduled for the same time, put the entry
37 * that has been run earlier the last time first. */
38 return avahi_timeval_compare(&a->last_run, &b->last_run);
41 static gboolean prepare_func(GSource *source, gint *timeout) {
42 AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
49 if (!q->prioq->root) {
54 e = q->prioq->root->data;
57 g_source_get_current_time(source, &now);
59 if (avahi_timeval_compare(&now, &e->expiry) >= 0 && /* Time elapsed */
60 avahi_timeval_compare(&now, &e->last_run) != 0 /* Not yet run */) {
65 *timeout = (gint) (avahi_timeval_diff(&e->expiry, &now)/1000);
67 /* Wait at least 1 msec */
74 static gboolean check_func(GSource *source) {
75 AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
84 e = q->prioq->root->data;
87 g_source_get_current_time(source, &now);
90 avahi_timeval_compare(&now, &e->expiry) >= 0 && /* Time elapsed */
91 avahi_timeval_compare(&now, &e->last_run) != 0; /* Not yet run */
94 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
95 AvahiTimeEventQueue *q = (AvahiTimeEventQueue*) source;
100 g_source_get_current_time(source, &now);
102 while (q->prioq->root) {
103 AvahiTimeEvent *e = q->prioq->root->data;
105 /* Not yet expired */
106 if (avahi_timeval_compare(&now, &e->expiry) < 0)
110 if (avahi_timeval_compare(&now, &e->last_run) == 0)
113 /* Make sure to move the entry away from the front */
115 avahi_prio_queue_shuffle(q->prioq, e->node);
118 g_assert(e->callback);
119 e->callback(e, e->userdata);
125 static void fix_expiry_time(AvahiTimeEvent *e) {
129 g_source_get_current_time(&e->queue->source, &now);
131 if (avahi_timeval_compare(&now, &e->expiry) > 0)
136 AvahiTimeEventQueue* avahi_time_event_queue_new(GMainContext *context, gint priority) {
137 AvahiTimeEventQueue *q;
139 static GSourceFuncs source_funcs = {
148 q = (AvahiTimeEventQueue*) g_source_new(&source_funcs, sizeof(AvahiTimeEventQueue));
149 q->prioq = avahi_prio_queue_new(compare);
151 g_source_set_priority((GSource*) q, priority);
153 g_source_attach(&q->source, context);
158 void avahi_time_event_queue_free(AvahiTimeEventQueue *q) {
161 while (q->prioq->root)
162 avahi_time_event_queue_remove(q, q->prioq->root->data);
163 avahi_prio_queue_free(q->prioq);
165 g_source_destroy(&q->source);
166 g_source_unref(&q->source);
169 AvahiTimeEvent* avahi_time_event_queue_add(AvahiTimeEventQueue *q, const GTimeVal *timeval, AvahiTimeEventCallback callback, gpointer userdata) {
177 e = g_new(AvahiTimeEvent, 1);
179 e->callback = callback;
180 e->userdata = userdata;
182 e->expiry = *timeval;
185 e->last_run.tv_sec = 0;
186 e->last_run.tv_usec = 0;
188 e->node = avahi_prio_queue_put(q->prioq, e);
193 void avahi_time_event_queue_remove(AvahiTimeEventQueue *q, AvahiTimeEvent *e) {
196 g_assert(e->queue == q);
198 avahi_prio_queue_remove(q->prioq, e->node);
202 void avahi_time_event_queue_update(AvahiTimeEventQueue *q, AvahiTimeEvent *e, const GTimeVal *timeval) {
205 g_assert(e->queue == q);
208 e->expiry = *timeval;
211 avahi_prio_queue_shuffle(q->prioq, e->node);
214 AvahiTimeEvent* avahi_time_event_queue_root(AvahiTimeEventQueue *q) {
217 return q->prioq->root ? q->prioq->root->data : NULL;
220 AvahiTimeEvent* avahi_time_event_next(AvahiTimeEvent *e) {
223 return e->node->next->data;