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>
29 #define MAINLOOP_SPEW 1
35 int callback_list_serial;
38 int depth; /**< number of recursive runs */
39 DBusList *need_dispatch;
53 DBusFreeFunction free_data_func;
59 DBusWatchFunction function;
61 /* last watch handle failed due to OOM */
62 unsigned int last_iteration_oom : 1;
69 DBusTimeoutFunction function;
70 unsigned long last_tv_sec;
71 unsigned long last_tv_usec;
74 #define WATCH_CALLBACK(callback) ((WatchCallback*)callback)
75 #define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
78 watch_callback_new (DBusWatch *watch,
79 DBusWatchFunction function,
81 DBusFreeFunction free_data_func)
85 cb = dbus_new (WatchCallback, 1);
90 cb->function = function;
91 cb->last_iteration_oom = FALSE;
92 cb->callback.refcount = 1;
93 cb->callback.type = CALLBACK_WATCH;
94 cb->callback.data = data;
95 cb->callback.free_data_func = free_data_func;
100 static TimeoutCallback*
101 timeout_callback_new (DBusTimeout *timeout,
102 DBusTimeoutFunction function,
104 DBusFreeFunction free_data_func)
108 cb = dbus_new (TimeoutCallback, 1);
112 cb->timeout = timeout;
113 cb->function = function;
114 _dbus_get_current_time (&cb->last_tv_sec,
116 cb->callback.refcount = 1;
117 cb->callback.type = CALLBACK_TIMEOUT;
118 cb->callback.data = data;
119 cb->callback.free_data_func = free_data_func;
125 callback_ref (Callback *cb)
127 _dbus_assert (cb->refcount > 0);
133 callback_unref (Callback *cb)
135 _dbus_assert (cb->refcount > 0);
139 if (cb->refcount == 0)
141 if (cb->free_data_func)
142 (* cb->free_data_func) (cb->data);
149 add_callback (DBusLoop *loop,
152 if (!_dbus_list_append (&loop->callbacks, cb))
155 loop->callback_list_serial += 1;
160 loop->watch_count += 1;
162 case CALLBACK_TIMEOUT:
163 loop->timeout_count += 1;
171 remove_callback (DBusLoop *loop,
174 Callback *cb = link->data;
179 loop->watch_count -= 1;
181 case CALLBACK_TIMEOUT:
182 loop->timeout_count -= 1;
187 _dbus_list_remove_link (&loop->callbacks, link);
188 loop->callback_list_serial += 1;
192 _dbus_loop_new (void)
196 loop = dbus_new0 (DBusLoop, 1);
206 _dbus_loop_ref (DBusLoop *loop)
208 _dbus_assert (loop != NULL);
209 _dbus_assert (loop->refcount > 0);
215 _dbus_loop_unref (DBusLoop *loop)
217 _dbus_assert (loop != NULL);
218 _dbus_assert (loop->refcount > 0);
221 if (loop->refcount == 0)
223 while (loop->need_dispatch)
225 DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch);
227 dbus_connection_unref (connection);
235 _dbus_loop_add_watch (DBusLoop *loop,
237 DBusWatchFunction function,
239 DBusFreeFunction free_data_func)
243 wcb = watch_callback_new (watch, function, data, free_data_func);
247 if (!add_callback (loop, (Callback*) wcb))
249 wcb->callback.free_data_func = NULL; /* don't want to have this side effect */
250 callback_unref ((Callback*) wcb);
258 _dbus_loop_remove_watch (DBusLoop *loop,
260 DBusWatchFunction function,
265 link = _dbus_list_get_first_link (&loop->callbacks);
268 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
269 Callback *this = link->data;
271 if (this->type == CALLBACK_WATCH &&
272 WATCH_CALLBACK (this)->watch == watch &&
273 this->data == data &&
274 WATCH_CALLBACK (this)->function == function)
276 remove_callback (loop, link);
284 _dbus_warn ("could not find watch %p function %p data %p to remove\n",
285 watch, function, data);
289 _dbus_loop_add_timeout (DBusLoop *loop,
290 DBusTimeout *timeout,
291 DBusTimeoutFunction function,
293 DBusFreeFunction free_data_func)
295 TimeoutCallback *tcb;
297 tcb = timeout_callback_new (timeout, function, data, free_data_func);
301 if (!add_callback (loop, (Callback*) tcb))
303 tcb->callback.free_data_func = NULL; /* don't want to have this side effect */
304 callback_unref ((Callback*) tcb);
312 _dbus_loop_remove_timeout (DBusLoop *loop,
313 DBusTimeout *timeout,
314 DBusTimeoutFunction function,
319 link = _dbus_list_get_first_link (&loop->callbacks);
322 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
323 Callback *this = link->data;
325 if (this->type == CALLBACK_TIMEOUT &&
326 TIMEOUT_CALLBACK (this)->timeout == timeout &&
327 this->data == data &&
328 TIMEOUT_CALLBACK (this)->function == function)
330 remove_callback (loop, link);
338 _dbus_warn ("could not find timeout %p function %p data %p to remove\n",
339 timeout, function, data);
342 /* Convolutions from GLib, there really must be a better way
346 check_timeout (unsigned long tv_sec,
347 unsigned long tv_usec,
348 TimeoutCallback *tcb,
353 unsigned long expiration_tv_sec;
354 unsigned long expiration_tv_usec;
355 long interval_seconds;
356 long interval_milliseconds;
359 interval = dbus_timeout_get_interval (tcb->timeout);
361 interval_seconds = interval / 1000;
362 interval_milliseconds = interval - interval_seconds * 1000;
364 expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
365 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
366 if (expiration_tv_usec >= 1000000)
368 expiration_tv_usec -= 1000000;
369 expiration_tv_sec += 1;
372 if (expiration_tv_sec < tv_sec ||
373 (expiration_tv_sec == tv_sec && expiration_tv_usec < tv_usec))
375 _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",
376 interval_seconds, interval_milliseconds,
377 tcb->last_tv_sec, tcb->last_tv_usec, tv_sec, tv_usec);
379 /* The system time has been set backwards, reset the timeout to "interval" in the future */
381 tcb->last_tv_sec = tv_sec;
382 tcb->last_tv_usec = tv_usec;
389 sec_remaining = expiration_tv_sec - tv_sec;
390 msec_remaining = (expiration_tv_usec - tv_usec) / 1000;
393 printf ("Interval is %ld seconds %ld msecs\n",
395 interval_milliseconds);
396 printf ("Now is %lu seconds %lu usecs\n",
398 printf ("Exp is %lu seconds %lu usecs\n",
399 expiration_tv_sec, expiration_tv_usec);
400 printf ("Pre-correction, remaining sec_remaining %ld msec_remaining %ld\n", sec_remaining, msec_remaining);
403 /* We do the following in a rather convoluted fashion to deal with
404 * the fact that we don't have an integral type big enough to hold
405 * the difference of two timevals in millseconds.
407 if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
411 if (msec_remaining < 0)
413 msec_remaining += 1000;
417 if (msec_remaining > _DBUS_INT_MAX)
419 /* Not going to fit in a 32-bit integer */
420 msec_remaining = _DBUS_INT_MAX;
424 *timeout = msec_remaining;
427 _dbus_verbose (" timeout expires in %d milliseconds\n", *timeout);
430 return msec_remaining == 0;
434 _dbus_loop_dispatch (DBusLoop *loop)
438 _dbus_verbose (" %d connections to dispatch\n", _dbus_list_get_length (&loop->need_dispatch));
441 if (loop->need_dispatch == NULL)
445 while (loop->need_dispatch != NULL)
447 DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch);
451 DBusDispatchStatus status;
453 status = dbus_connection_dispatch (connection);
455 if (status == DBUS_DISPATCH_COMPLETE)
457 dbus_connection_unref (connection);
462 if (status == DBUS_DISPATCH_NEED_MEMORY)
463 _dbus_wait_for_memory ();
472 _dbus_loop_queue_dispatch (DBusLoop *loop,
473 DBusConnection *connection)
475 if (_dbus_list_append (&loop->need_dispatch, connection))
477 dbus_connection_ref (connection);
484 /* Returns TRUE if we have any timeouts or ready file descriptors,
485 * which is just used in test code as a debug hack
489 _dbus_loop_iterate (DBusLoop *loop,
492 #define N_STACK_DESCRIPTORS 64
495 DBusPollFD stack_fds[N_STACK_DESCRIPTORS];
497 WatchCallback **watches_for_fds;
498 WatchCallback *stack_watches_for_fds[N_STACK_DESCRIPTORS];
504 dbus_bool_t oom_watch_pending;
510 watches_for_fds = NULL;
512 oom_watch_pending = FALSE;
513 orig_depth = loop->depth;
516 _dbus_verbose ("Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
517 block, loop->depth, loop->timeout_count, loop->watch_count);
520 if (loop->callbacks == NULL)
523 if (loop->watch_count > N_STACK_DESCRIPTORS)
525 fds = dbus_new0 (DBusPollFD, loop->watch_count);
529 _dbus_wait_for_memory ();
530 fds = dbus_new0 (DBusPollFD, loop->watch_count);
533 watches_for_fds = dbus_new (WatchCallback*, loop->watch_count);
534 while (watches_for_fds == NULL)
536 _dbus_wait_for_memory ();
537 watches_for_fds = dbus_new (WatchCallback*, loop->watch_count);
543 watches_for_fds = stack_watches_for_fds;
546 /* fill our array of fds and watches */
548 link = _dbus_list_get_first_link (&loop->callbacks);
551 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
552 Callback *cb = link->data;
553 if (cb->type == CALLBACK_WATCH)
556 WatchCallback *wcb = WATCH_CALLBACK (cb);
558 if (wcb->last_iteration_oom)
560 /* we skip this one this time, but reenable it next time,
561 * and have a timeout on this iteration
563 wcb->last_iteration_oom = FALSE;
564 oom_watch_pending = TRUE;
566 retval = TRUE; /* return TRUE here to keep the loop going,
567 * since we don't know the watch is inactive
571 _dbus_verbose (" skipping watch on fd %d as it was out of memory last time\n",
572 dbus_watch_get_fd (wcb->watch));
575 else if (dbus_watch_get_enabled (wcb->watch))
577 watches_for_fds[n_fds] = wcb;
581 flags = dbus_watch_get_flags (wcb->watch);
583 fds[n_fds].fd = dbus_watch_get_fd (wcb->watch);
584 fds[n_fds].revents = 0;
585 fds[n_fds].events = 0;
586 if (flags & DBUS_WATCH_READABLE)
587 fds[n_fds].events |= _DBUS_POLLIN;
588 if (flags & DBUS_WATCH_WRITABLE)
589 fds[n_fds].events |= _DBUS_POLLOUT;
594 _dbus_verbose (" polling watch on fd %d\n", fds[n_fds].fd);
600 _dbus_verbose (" skipping disabled watch on fd %d\n",
601 dbus_watch_get_fd (wcb->watch));
610 if (loop->timeout_count > 0)
612 unsigned long tv_sec;
613 unsigned long tv_usec;
615 _dbus_get_current_time (&tv_sec, &tv_usec);
617 link = _dbus_list_get_first_link (&loop->callbacks);
620 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
621 Callback *cb = link->data;
623 if (cb->type == CALLBACK_TIMEOUT &&
624 dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
626 TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
629 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
632 timeout = msecs_remaining;
634 timeout = MIN (msecs_remaining, timeout);
636 _dbus_assert (timeout >= 0);
639 break; /* it's not going to get shorter... */
646 /* Never block if we have stuff to dispatch */
647 if (!block || loop->need_dispatch != NULL)
651 _dbus_verbose (" timeout is 0 as we aren't blocking\n");
655 /* if a watch is OOM, don't wait longer than the OOM
656 * wait to re-enable it
658 if (oom_watch_pending)
659 timeout = MIN (timeout, _dbus_get_oom_wait ());
662 _dbus_verbose (" polling on %d descriptors timeout %ld\n", n_fds, timeout);
665 n_ready = _dbus_poll (fds, n_fds, timeout);
667 initial_serial = loop->callback_list_serial;
669 if (loop->timeout_count > 0)
671 unsigned long tv_sec;
672 unsigned long tv_usec;
674 _dbus_get_current_time (&tv_sec, &tv_usec);
676 /* It'd be nice to avoid this O(n) thingy here */
677 link = _dbus_list_get_first_link (&loop->callbacks);
680 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
681 Callback *cb = link->data;
683 if (initial_serial != loop->callback_list_serial)
686 if (loop->depth != orig_depth)
689 if (cb->type == CALLBACK_TIMEOUT &&
690 dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
692 TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
695 if (check_timeout (tv_sec, tv_usec,
696 tcb, &msecs_remaining))
698 /* Save last callback time and fire this timeout */
699 tcb->last_tv_sec = tv_sec;
700 tcb->last_tv_usec = tv_usec;
703 _dbus_verbose (" invoking timeout\n");
706 (* tcb->function) (tcb->timeout,
722 /* FIXME I think this "restart if we change the watches"
723 * approach could result in starving watches
724 * toward the end of the list.
726 if (initial_serial != loop->callback_list_serial)
729 if (loop->depth != orig_depth)
732 if (fds[i].revents != 0)
735 unsigned int condition;
737 wcb = watches_for_fds[i];
740 if (fds[i].revents & _DBUS_POLLIN)
741 condition |= DBUS_WATCH_READABLE;
742 if (fds[i].revents & _DBUS_POLLOUT)
743 condition |= DBUS_WATCH_WRITABLE;
744 if (fds[i].revents & _DBUS_POLLHUP)
745 condition |= DBUS_WATCH_HANGUP;
746 if (fds[i].revents & _DBUS_POLLERR)
747 condition |= DBUS_WATCH_ERROR;
749 /* condition may still be 0 if we got some
750 * weird POLLFOO thing like POLLWRBAND
753 if (condition != 0 &&
754 dbus_watch_get_enabled (wcb->watch))
756 if (!(* wcb->function) (wcb->watch,
758 ((Callback*)wcb)->data))
759 wcb->last_iteration_oom = TRUE;
762 _dbus_verbose (" Invoked watch, oom = %d\n",
763 wcb->last_iteration_oom);
775 if (fds && fds != stack_fds)
782 callback_unref (&watches_for_fds[i]->callback);
786 if (watches_for_fds != stack_watches_for_fds)
787 dbus_free (watches_for_fds);
790 if (_dbus_loop_dispatch (loop))
794 _dbus_verbose ("Returning %d\n", retval);
801 _dbus_loop_run (DBusLoop *loop)
805 _dbus_assert (loop->depth >= 0);
807 _dbus_loop_ref (loop);
809 our_exit_depth = loop->depth;
812 _dbus_verbose ("Running main loop, depth %d -> %d\n",
813 loop->depth - 1, loop->depth);
815 while (loop->depth != our_exit_depth)
816 _dbus_loop_iterate (loop, TRUE);
818 _dbus_loop_unref (loop);
822 _dbus_loop_quit (DBusLoop *loop)
824 _dbus_assert (loop->depth > 0);
828 _dbus_verbose ("Quit main loop, depth %d -> %d\n",
829 loop->depth + 1, loop->depth);
833 _dbus_get_oom_wait (void)
835 #ifdef DBUS_BUILD_TESTS
836 /* make tests go fast */
844 _dbus_wait_for_memory (void)
846 _dbus_verbose ("Waiting for more memory\n");
847 _dbus_sleep_milliseconds (_dbus_get_oom_wait ());