1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-mainloop.c Main loop utility
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-mainloop.h"
26 #include <dbus/dbus-list.h>
27 #include <dbus/dbus-sysdeps.h>
33 int callback_list_serial;
36 int depth; /**< number of recursive runs */
37 DBusList *need_dispatch;
50 DBusFreeFunction free_data_func;
56 DBusWatchFunction function;
58 /* last watch handle failed due to OOM */
59 unsigned int last_iteration_oom : 1;
66 DBusTimeoutFunction function;
67 unsigned long last_tv_sec;
68 unsigned long last_tv_usec;
71 #define WATCH_CALLBACK(callback) ((WatchCallback*)callback)
72 #define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
75 watch_callback_new (DBusWatch *watch,
76 DBusWatchFunction function,
78 DBusFreeFunction free_data_func)
82 cb = dbus_new (WatchCallback, 1);
87 cb->function = function;
88 cb->last_iteration_oom = FALSE;
89 cb->callback.type = CALLBACK_WATCH;
90 cb->callback.data = data;
91 cb->callback.free_data_func = free_data_func;
96 static TimeoutCallback*
97 timeout_callback_new (DBusTimeout *timeout,
98 DBusTimeoutFunction function,
100 DBusFreeFunction free_data_func)
104 cb = dbus_new (TimeoutCallback, 1);
108 cb->timeout = timeout;
109 cb->function = function;
110 _dbus_get_current_time (&cb->last_tv_sec,
112 cb->callback.type = CALLBACK_TIMEOUT;
113 cb->callback.data = data;
114 cb->callback.free_data_func = free_data_func;
120 callback_free (Callback *cb)
122 if (cb->free_data_func)
123 (* cb->free_data_func) (cb->data);
129 add_callback (DBusLoop *loop,
132 if (!_dbus_list_append (&loop->callbacks, cb))
135 loop->callback_list_serial += 1;
140 loop->watch_count += 1;
142 case CALLBACK_TIMEOUT:
143 loop->timeout_count += 1;
151 remove_callback (DBusLoop *loop,
154 Callback *cb = link->data;
159 loop->watch_count -= 1;
161 case CALLBACK_TIMEOUT:
162 loop->timeout_count -= 1;
167 _dbus_list_remove_link (&loop->callbacks, link);
168 loop->callback_list_serial += 1;
172 _dbus_loop_new (void)
176 loop = dbus_new0 (DBusLoop, 1);
186 _dbus_loop_ref (DBusLoop *loop)
188 _dbus_assert (loop != NULL);
189 _dbus_assert (loop->refcount > 0);
195 _dbus_loop_unref (DBusLoop *loop)
197 _dbus_assert (loop != NULL);
198 _dbus_assert (loop->refcount > 0);
201 if (loop->refcount == 0)
203 while (loop->need_dispatch)
205 DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch);
207 dbus_connection_unref (connection);
215 _dbus_loop_add_watch (DBusLoop *loop,
217 DBusWatchFunction function,
219 DBusFreeFunction free_data_func)
223 wcb = watch_callback_new (watch, function, data, free_data_func);
227 if (!add_callback (loop, (Callback*) wcb))
229 wcb->callback.free_data_func = NULL; /* don't want to have this side effect */
230 callback_free ((Callback*) wcb);
238 _dbus_loop_remove_watch (DBusLoop *loop,
240 DBusWatchFunction function,
245 link = _dbus_list_get_first_link (&loop->callbacks);
248 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
249 Callback *this = link->data;
251 if (this->type == CALLBACK_WATCH &&
252 WATCH_CALLBACK (this)->watch == watch &&
253 this->data == data &&
254 WATCH_CALLBACK (this)->function == function)
256 remove_callback (loop, link);
264 _dbus_warn ("could not find watch %p function %p data %p to remove\n",
265 watch, function, data);
269 _dbus_loop_add_timeout (DBusLoop *loop,
270 DBusTimeout *timeout,
271 DBusTimeoutFunction function,
273 DBusFreeFunction free_data_func)
275 TimeoutCallback *tcb;
277 tcb = timeout_callback_new (timeout, function, data, free_data_func);
281 if (!add_callback (loop, (Callback*) tcb))
283 tcb->callback.free_data_func = NULL; /* don't want to have this side effect */
284 callback_free ((Callback*) tcb);
292 _dbus_loop_remove_timeout (DBusLoop *loop,
293 DBusTimeout *timeout,
294 DBusTimeoutFunction function,
299 link = _dbus_list_get_first_link (&loop->callbacks);
302 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
303 Callback *this = link->data;
305 if (this->type == CALLBACK_TIMEOUT &&
306 TIMEOUT_CALLBACK (this)->timeout == timeout &&
307 this->data == data &&
308 TIMEOUT_CALLBACK (this)->function == function)
310 remove_callback (loop, link);
318 _dbus_warn ("could not find timeout %p function %p data %p to remove\n",
319 timeout, function, data);
322 /* Convolutions from GLib, there really must be a better way
326 check_timeout (unsigned long tv_sec,
327 unsigned long tv_usec,
328 TimeoutCallback *tcb,
333 unsigned long expiration_tv_sec;
334 unsigned long expiration_tv_usec;
335 long interval_seconds;
336 long interval_milliseconds;
339 interval = dbus_timeout_get_interval (tcb->timeout);
341 interval_seconds = interval / 1000;
342 interval_milliseconds = interval - interval_seconds * 1000;
344 expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
345 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
346 if (expiration_tv_usec >= 1000000)
348 expiration_tv_usec -= 1000000;
349 expiration_tv_sec += 1;
352 if (expiration_tv_sec < tv_sec ||
353 (expiration_tv_sec == tv_sec && expiration_tv_usec < tv_usec))
355 _dbus_verbose ("System clock went backward interval_seconds %ld interval_msecs %ld last_tv_sec %lu last_tv_usec %lu tv_sec %lu tv_usec %lu\n",
356 interval_seconds, interval_milliseconds,
357 tcb->last_tv_sec, tcb->last_tv_usec, tv_sec, tv_usec);
359 /* The system time has been set backwards, reset the timeout to "interval" in the future */
361 tcb->last_tv_sec = tv_sec;
362 tcb->last_tv_usec = tv_usec;
369 sec_remaining = expiration_tv_sec - tv_sec;
370 msec_remaining = (expiration_tv_usec - tv_usec) / 1000;
373 printf ("Interval is %ld seconds %ld msecs\n",
375 interval_milliseconds);
376 printf ("Now is %lu seconds %lu usecs\n",
378 printf ("Exp is %lu seconds %lu usecs\n",
379 expiration_tv_sec, expiration_tv_usec);
380 printf ("Pre-correction, remaining sec_remaining %ld msec_remaining %ld\n", sec_remaining, msec_remaining);
383 /* We do the following in a rather convoluted fashion to deal with
384 * the fact that we don't have an integral type big enough to hold
385 * the difference of two timevals in millseconds.
387 if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
391 if (msec_remaining < 0)
393 msec_remaining += 1000;
397 if (msec_remaining > _DBUS_INT_MAX)
399 /* Not going to fit in a 32-bit integer */
400 msec_remaining = _DBUS_INT_MAX;
404 *timeout = msec_remaining;
407 printf ("Timeout expires in %d milliseconds\n", *timeout);
410 return msec_remaining == 0;
414 _dbus_loop_dispatch (DBusLoop *loop)
417 while (loop->need_dispatch != NULL)
419 DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch);
423 DBusDispatchStatus status;
425 status = dbus_connection_dispatch (connection);
427 if (status == DBUS_DISPATCH_COMPLETE)
429 dbus_connection_unref (connection);
434 if (status == DBUS_DISPATCH_NEED_MEMORY)
435 _dbus_wait_for_memory ();
442 _dbus_loop_queue_dispatch (DBusLoop *loop,
443 DBusConnection *connection)
446 if (_dbus_list_append (&loop->need_dispatch, connection))
448 dbus_connection_ref (connection);
455 /* Returns TRUE if we have any timeouts or ready file descriptors,
456 * which is just used in test code as a debug hack
460 _dbus_loop_iterate (DBusLoop *loop,
466 WatchCallback **watches_for_fds;
472 dbus_bool_t oom_watch_pending;
478 watches_for_fds = NULL;
479 oom_watch_pending = FALSE;
480 orig_depth = loop->depth;
483 _dbus_verbose (" iterate %d timeouts %d watches\n",
484 loop->timeout_count, loop->watch_count);
487 if (loop->callbacks == NULL)
489 _dbus_loop_quit (loop);
493 /* count enabled watches */
495 link = _dbus_list_get_first_link (&loop->callbacks);
498 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
499 Callback *cb = link->data;
500 if (cb->type == CALLBACK_WATCH)
502 WatchCallback *wcb = WATCH_CALLBACK (cb);
504 if (!wcb->last_iteration_oom &&
505 dbus_watch_get_enabled (wcb->watch))
512 /* fill our array of fds and watches */
515 fds = dbus_new0 (DBusPollFD, n_fds);
518 _dbus_wait_for_memory ();
519 fds = dbus_new0 (DBusPollFD, n_fds);
522 watches_for_fds = dbus_new (WatchCallback*, n_fds);
523 while (watches_for_fds == NULL)
525 _dbus_wait_for_memory ();
526 watches_for_fds = dbus_new (WatchCallback*, n_fds);
530 link = _dbus_list_get_first_link (&loop->callbacks);
533 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
534 Callback *cb = link->data;
535 if (cb->type == CALLBACK_WATCH)
538 WatchCallback *wcb = WATCH_CALLBACK (cb);
540 if (wcb->last_iteration_oom)
542 /* we skip this one this time, but reenable it next time,
543 * and have a timeout on this iteration
545 wcb->last_iteration_oom = FALSE;
546 oom_watch_pending = TRUE;
548 else if (dbus_watch_get_enabled (wcb->watch))
550 watches_for_fds[i] = wcb;
552 flags = dbus_watch_get_flags (wcb->watch);
554 fds[i].fd = dbus_watch_get_fd (wcb->watch);
555 if (flags & DBUS_WATCH_READABLE)
556 fds[i].events |= _DBUS_POLLIN;
557 if (flags & DBUS_WATCH_WRITABLE)
558 fds[i].events |= _DBUS_POLLOUT;
567 _dbus_assert (i == n_fds);
571 if (loop->timeout_count > 0)
573 unsigned long tv_sec;
574 unsigned long tv_usec;
578 _dbus_get_current_time (&tv_sec, &tv_usec);
580 link = _dbus_list_get_first_link (&loop->callbacks);
583 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
584 Callback *cb = link->data;
586 if (cb->type == CALLBACK_TIMEOUT &&
587 dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
589 TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
592 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
595 timeout = msecs_remaining;
597 timeout = MIN (msecs_remaining, timeout);
599 _dbus_assert (timeout >= 0);
602 break; /* it's not going to get shorter... */
609 /* Never block if we have stuff to dispatch */
610 if (!block || loop->need_dispatch != NULL)
614 printf ("timeout is 0 as we aren't blocking\n");
618 /* if a watch is OOM, don't wait longer than the OOM
619 * wait to re-enable it
621 if (oom_watch_pending)
622 timeout = MIN (timeout, _dbus_get_oom_wait ());
624 n_ready = _dbus_poll (fds, n_fds, timeout);
626 initial_serial = loop->callback_list_serial;
628 if (loop->timeout_count > 0)
630 unsigned long tv_sec;
631 unsigned long tv_usec;
633 _dbus_get_current_time (&tv_sec, &tv_usec);
635 /* It'd be nice to avoid this O(n) thingy here */
636 link = _dbus_list_get_first_link (&loop->callbacks);
639 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
640 Callback *cb = link->data;
642 if (initial_serial != loop->callback_list_serial)
645 if (loop->depth != orig_depth)
648 if (cb->type == CALLBACK_TIMEOUT &&
649 dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
651 TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
654 if (check_timeout (tv_sec, tv_usec,
655 tcb, &msecs_remaining))
657 /* Save last callback time and fire this timeout */
658 tcb->last_tv_sec = tv_sec;
659 tcb->last_tv_usec = tv_usec;
662 printf (" invoking timeout\n");
665 (* tcb->function) (tcb->timeout,
679 /* FIXME I think this "restart if we change the watches"
680 * approach could result in starving watches
681 * toward the end of the list.
683 if (initial_serial != loop->callback_list_serial)
686 if (loop->depth != orig_depth)
689 if (fds[i].revents != 0)
692 unsigned int condition;
694 wcb = watches_for_fds[i];
697 if (fds[i].revents & _DBUS_POLLIN)
698 condition |= DBUS_WATCH_READABLE;
699 if (fds[i].revents & _DBUS_POLLOUT)
700 condition |= DBUS_WATCH_WRITABLE;
701 if (fds[i].revents & _DBUS_POLLHUP)
702 condition |= DBUS_WATCH_HANGUP;
703 if (fds[i].revents & _DBUS_POLLERR)
704 condition |= DBUS_WATCH_ERROR;
706 /* condition may still be 0 if we got some
707 * weird POLLFOO thing like POLLWRBAND
710 if (condition != 0 &&
711 dbus_watch_get_enabled (wcb->watch))
713 if (!(* wcb->function) (wcb->watch,
715 ((Callback*)wcb)->data))
716 wcb->last_iteration_oom = TRUE;
728 dbus_free (watches_for_fds);
730 if (loop->need_dispatch != NULL)
733 _dbus_loop_dispatch (loop);
740 _dbus_loop_run (DBusLoop *loop)
744 _dbus_loop_ref (loop);
746 our_exit_depth = loop->depth;
749 while (loop->depth != our_exit_depth)
750 _dbus_loop_iterate (loop, TRUE);
752 _dbus_loop_unref (loop);
756 _dbus_loop_quit (DBusLoop *loop)
758 _dbus_assert (loop->depth > 0);
764 _dbus_get_oom_wait (void)
766 #ifdef DBUS_BUILD_TESTS
767 /* make tests go fast */
775 _dbus_wait_for_memory (void)
777 _dbus_sleep_milliseconds (_dbus_get_oom_wait ());