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_STATIC_DESCRIPTORS 64
495 DBusPollFD static_fds[N_STATIC_DESCRIPTORS];
497 WatchCallback **watches_for_fds;
498 WatchCallback *static_watches_for_fds[N_STATIC_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 /* count enabled watches */
525 link = _dbus_list_get_first_link (&loop->callbacks);
528 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
529 Callback *cb = link->data;
530 if (cb->type == CALLBACK_WATCH)
532 WatchCallback *wcb = WATCH_CALLBACK (cb);
534 if (!wcb->last_iteration_oom &&
535 dbus_watch_get_enabled (wcb->watch))
542 /* fill our array of fds and watches */
545 if (n_fds > N_STATIC_DESCRIPTORS)
547 fds = dbus_new0 (DBusPollFD, n_fds);
551 _dbus_wait_for_memory ();
552 fds = dbus_new0 (DBusPollFD, n_fds);
555 watches_for_fds = dbus_new (WatchCallback*, n_fds);
556 while (watches_for_fds == NULL)
558 _dbus_wait_for_memory ();
559 watches_for_fds = dbus_new (WatchCallback*, n_fds);
564 memset (static_fds, '\0', sizeof (static_fds[0]) * n_fds);
565 memset (static_watches_for_fds, '\0', sizeof (static_watches_for_fds[0]) * n_fds);
568 watches_for_fds = static_watches_for_fds;
572 link = _dbus_list_get_first_link (&loop->callbacks);
575 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
576 Callback *cb = link->data;
577 if (cb->type == CALLBACK_WATCH)
580 WatchCallback *wcb = WATCH_CALLBACK (cb);
582 if (wcb->last_iteration_oom)
584 /* we skip this one this time, but reenable it next time,
585 * and have a timeout on this iteration
587 wcb->last_iteration_oom = FALSE;
588 oom_watch_pending = TRUE;
590 retval = TRUE; /* return TRUE here to keep the loop going,
591 * since we don't know the watch is inactive
594 _dbus_verbose (" skipping watch on fd %d as it was out of memory last time\n",
595 dbus_watch_get_fd (wcb->watch));
597 else if (dbus_watch_get_enabled (wcb->watch))
599 watches_for_fds[i] = wcb;
603 flags = dbus_watch_get_flags (wcb->watch);
605 fds[i].fd = dbus_watch_get_fd (wcb->watch);
606 if (flags & DBUS_WATCH_READABLE)
607 fds[i].events |= _DBUS_POLLIN;
608 if (flags & DBUS_WATCH_WRITABLE)
609 fds[i].events |= _DBUS_POLLOUT;
618 _dbus_assert (i == n_fds);
622 if (loop->timeout_count > 0)
624 unsigned long tv_sec;
625 unsigned long tv_usec;
627 _dbus_get_current_time (&tv_sec, &tv_usec);
629 link = _dbus_list_get_first_link (&loop->callbacks);
632 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
633 Callback *cb = link->data;
635 if (cb->type == CALLBACK_TIMEOUT &&
636 dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
638 TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
641 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
644 timeout = msecs_remaining;
646 timeout = MIN (msecs_remaining, timeout);
648 _dbus_assert (timeout >= 0);
651 break; /* it's not going to get shorter... */
658 /* Never block if we have stuff to dispatch */
659 if (!block || loop->need_dispatch != NULL)
663 _dbus_verbose (" timeout is 0 as we aren't blocking\n");
667 /* if a watch is OOM, don't wait longer than the OOM
668 * wait to re-enable it
670 if (oom_watch_pending)
671 timeout = MIN (timeout, _dbus_get_oom_wait ());
674 _dbus_verbose (" polling on %d descriptors timeout %ld\n", n_fds, timeout);
677 n_ready = _dbus_poll (fds, n_fds, timeout);
679 initial_serial = loop->callback_list_serial;
681 if (loop->timeout_count > 0)
683 unsigned long tv_sec;
684 unsigned long tv_usec;
686 _dbus_get_current_time (&tv_sec, &tv_usec);
688 /* It'd be nice to avoid this O(n) thingy here */
689 link = _dbus_list_get_first_link (&loop->callbacks);
692 DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
693 Callback *cb = link->data;
695 if (initial_serial != loop->callback_list_serial)
698 if (loop->depth != orig_depth)
701 if (cb->type == CALLBACK_TIMEOUT &&
702 dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
704 TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
707 if (check_timeout (tv_sec, tv_usec,
708 tcb, &msecs_remaining))
710 /* Save last callback time and fire this timeout */
711 tcb->last_tv_sec = tv_sec;
712 tcb->last_tv_usec = tv_usec;
715 _dbus_verbose (" invoking timeout\n");
718 (* tcb->function) (tcb->timeout,
734 /* FIXME I think this "restart if we change the watches"
735 * approach could result in starving watches
736 * toward the end of the list.
738 if (initial_serial != loop->callback_list_serial)
741 if (loop->depth != orig_depth)
744 if (fds[i].revents != 0)
747 unsigned int condition;
749 wcb = watches_for_fds[i];
752 if (fds[i].revents & _DBUS_POLLIN)
753 condition |= DBUS_WATCH_READABLE;
754 if (fds[i].revents & _DBUS_POLLOUT)
755 condition |= DBUS_WATCH_WRITABLE;
756 if (fds[i].revents & _DBUS_POLLHUP)
757 condition |= DBUS_WATCH_HANGUP;
758 if (fds[i].revents & _DBUS_POLLERR)
759 condition |= DBUS_WATCH_ERROR;
761 /* condition may still be 0 if we got some
762 * weird POLLFOO thing like POLLWRBAND
765 if (condition != 0 &&
766 dbus_watch_get_enabled (wcb->watch))
768 if (!(* wcb->function) (wcb->watch,
770 ((Callback*)wcb)->data))
771 wcb->last_iteration_oom = TRUE;
774 _dbus_verbose (" Invoked watch, oom = %d\n",
775 wcb->last_iteration_oom);
787 if (fds && fds != static_fds)
794 callback_unref (&watches_for_fds[i]->callback);
798 if (watches_for_fds != static_watches_for_fds)
799 dbus_free (watches_for_fds);
802 if (_dbus_loop_dispatch (loop))
806 _dbus_verbose ("Returning %d\n", retval);
813 _dbus_loop_run (DBusLoop *loop)
817 _dbus_assert (loop->depth >= 0);
819 _dbus_loop_ref (loop);
821 our_exit_depth = loop->depth;
824 _dbus_verbose ("Running main loop, depth %d -> %d\n",
825 loop->depth - 1, loop->depth);
827 while (loop->depth != our_exit_depth)
828 _dbus_loop_iterate (loop, TRUE);
830 _dbus_loop_unref (loop);
834 _dbus_loop_quit (DBusLoop *loop)
836 _dbus_assert (loop->depth > 0);
840 _dbus_verbose ("Quit main loop, depth %d -> %d\n",
841 loop->depth + 1, loop->depth);
845 _dbus_get_oom_wait (void)
847 #ifdef DBUS_BUILD_TESTS
848 /* make tests go fast */
856 _dbus_wait_for_memory (void)
858 _dbus_verbose ("Waiting for more memory\n");
859 _dbus_sleep_milliseconds (_dbus_get_oom_wait ());