26 #include "dbus-mainloop.h"
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 #include <dbus/dbus-hash.h>
31 #include <dbus/dbus-list.h>
32 #include <dbus/dbus-socket-set.h>
33 #include <dbus/dbus-watch.h>
35 #define MAINLOOP_SPEW 0
42 DBusSocketSet *socket_set;
44 int callback_list_serial;
51 unsigned oom_watch_pending : 1;
57 unsigned long last_tv_sec;
58 unsigned long last_tv_usec;
61 #define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
63 static TimeoutCallback*
72 cb->timeout = timeout;
79 timeout_callback_free (TimeoutCallback *cb)
85 free_watch_table_entry (
void *data)
107 _dbus_loop_new (
void)
116 free_watch_table_entry);
118 loop->socket_set = _dbus_socket_set_new (0);
120 if (loop->watches ==
NULL || loop->socket_set ==
NULL)
122 if (loop->watches !=
NULL)
125 if (loop->socket_set !=
NULL)
126 _dbus_socket_set_free (loop->socket_set);
138 _dbus_loop_ref (DBusLoop *loop)
149 _dbus_loop_unref (DBusLoop *loop)
155 if (loop->refcount == 0)
157 while (loop->need_dispatch)
165 _dbus_socket_set_free (loop->socket_set);
171 ensure_watch_table_entry (DBusLoop *loop,
196 cull_watches_for_invalid_fd (DBusLoop *loop,
202 _dbus_warn (
"invalid request, socket fd %d not open\n", fd);
217 gc_watch_table_entry (DBusLoop *loop,
226 if (*watches !=
NULL)
234 refresh_watches_for_fd (DBusLoop *loop,
239 unsigned int flags = 0;
256 !_dbus_watch_get_oom_last_time (link->
data))
264 _dbus_socket_set_enable (loop->socket_set, fd, flags);
266 _dbus_socket_set_disable (loop->socket_set, fd);
270 _dbus_loop_add_watch (DBusLoop *loop,
279 watches = ensure_watch_table_entry (loop, fd);
287 gc_watch_table_entry (loop, watches, fd);
294 if (!_dbus_socket_set_add (loop->socket_set, fd,
305 refresh_watches_for_fd (loop, watches, fd);
308 loop->callback_list_serial += 1;
309 loop->watch_count += 1;
314 _dbus_loop_toggle_watch (DBusLoop *loop,
321 _dbus_loop_remove_watch (DBusLoop *loop,
347 loop->callback_list_serial += 1;
348 loop->watch_count -= 1;
353 if (gc_watch_table_entry (loop, watches, fd))
355 _dbus_socket_set_remove (loop->socket_set, fd);
365 _dbus_warn (
"could not find watch %p to remove\n", watch);
369 _dbus_loop_add_timeout (DBusLoop *loop,
372 TimeoutCallback *tcb;
374 tcb = timeout_callback_new (timeout);
380 loop->callback_list_serial += 1;
381 loop->timeout_count += 1;
385 timeout_callback_free (tcb);
393 _dbus_loop_remove_timeout (DBusLoop *loop,
402 TimeoutCallback *
this = link->
data;
404 if (this->timeout == timeout)
407 loop->callback_list_serial += 1;
408 loop->timeout_count -= 1;
409 timeout_callback_free (
this);
417 _dbus_warn (
"could not find timeout %p to remove\n", timeout);
424 check_timeout (
unsigned long tv_sec,
425 unsigned long tv_usec,
426 TimeoutCallback *tcb,
431 unsigned long expiration_tv_sec;
432 unsigned long expiration_tv_usec;
433 long interval_seconds;
434 long interval_milliseconds;
441 interval_seconds = interval / 1000L;
442 interval_milliseconds = interval % 1000L;
444 expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
445 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
446 if (expiration_tv_usec >= 1000000)
448 expiration_tv_usec -= 1000000;
449 expiration_tv_sec += 1;
452 sec_remaining = expiration_tv_sec - tv_sec;
456 msec_remaining = ((long) expiration_tv_usec - (
long) tv_usec) / 1000L;
459 _dbus_verbose (
"Interval is %ld seconds %ld msecs\n",
461 interval_milliseconds);
462 _dbus_verbose (
"Now is %lu seconds %lu usecs\n",
464 _dbus_verbose (
"Last is %lu seconds %lu usecs\n",
465 tcb->last_tv_sec, tcb->last_tv_usec);
466 _dbus_verbose (
"Exp is %lu seconds %lu usecs\n",
467 expiration_tv_sec, expiration_tv_usec);
468 _dbus_verbose (
"Pre-correction, sec_remaining %ld msec_remaining %ld\n",
469 sec_remaining, msec_remaining);
476 if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
482 if (msec_remaining < 0)
484 msec_remaining += 1000;
492 *timeout = sec_remaining * 1000 + msec_remaining;
495 if (*timeout > interval)
498 _dbus_verbose (
"System clock set backward! Resetting timeout.\n");
500 tcb->last_tv_sec = tv_sec;
501 tcb->last_tv_usec = tv_usec;
507 _dbus_verbose (
" timeout expires in %d milliseconds\n", *timeout);
510 return *timeout == 0;
514 _dbus_loop_dispatch (DBusLoop *loop)
521 if (loop->need_dispatch ==
NULL)
525 while (loop->need_dispatch !=
NULL)
543 _dbus_wait_for_memory ();
552 _dbus_loop_queue_dispatch (DBusLoop *loop,
569 _dbus_loop_iterate (DBusLoop *loop,
572 #define N_STACK_DESCRIPTORS 64
574 DBusSocketEvent ready_fds[N_STACK_DESCRIPTORS];
584 orig_depth = loop->depth;
587 _dbus_verbose (
"Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
588 block, loop->depth, loop->timeout_count, loop->watch_count);
592 loop->timeouts ==
NULL)
596 if (loop->timeout_count > 0)
598 unsigned long tv_sec;
599 unsigned long tv_usec;
607 TimeoutCallback *tcb = link->
data;
613 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
616 timeout = msecs_remaining;
618 timeout = MIN (msecs_remaining, timeout);
621 _dbus_verbose (
" timeout added, %d remaining, aggregate timeout %ld\n",
622 msecs_remaining, timeout);
633 _dbus_verbose (
" skipping disabled timeout\n");
642 if (!block || loop->need_dispatch !=
NULL)
646 _dbus_verbose (
" timeout is 0 as we aren't blocking\n");
653 if (loop->oom_watch_pending)
654 timeout = MIN (timeout, _dbus_get_oom_wait ());
657 _dbus_verbose (
" polling on %d descriptors timeout %ld\n",
_DBUS_N_ELEMENTS (ready_fds), timeout);
660 n_ready = _dbus_socket_set_poll (loop->socket_set, ready_fds,
664 if (loop->oom_watch_pending)
668 loop->oom_watch_pending =
FALSE;
688 if (_dbus_watch_get_oom_last_time (watch))
690 _dbus_watch_set_oom_last_time (watch,
FALSE);
696 refresh_watches_for_fd (loop, watches, fd);
703 initial_serial = loop->callback_list_serial;
705 if (loop->timeout_count > 0)
707 unsigned long tv_sec;
708 unsigned long tv_usec;
717 TimeoutCallback *tcb = link->
data;
719 if (initial_serial != loop->callback_list_serial)
722 if (loop->depth != orig_depth)
729 if (check_timeout (tv_sec, tv_usec,
730 tcb, &msecs_remaining))
733 tcb->last_tv_sec = tv_sec;
734 tcb->last_tv_usec = tv_usec;
737 _dbus_verbose (
" invoking timeout\n");
750 _dbus_verbose (
" timeout has not expired\n");
757 _dbus_verbose (
" skipping invocation of disabled timeout\n");
767 for (i = 0; i < n_ready; i++)
771 unsigned int condition;
778 if (initial_serial != loop->callback_list_serial)
781 if (loop->depth != orig_depth)
786 if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
788 cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
792 condition = ready_fds[i].flags;
825 _dbus_watch_set_oom_last_time (watch,
TRUE);
826 loop->oom_watch_pending =
TRUE;
831 _dbus_verbose (
" Invoked watch, oom = %d\n", oom);
838 if (initial_serial != loop->callback_list_serial ||
839 loop->depth != orig_depth)
842 refresh_watches_for_fd (loop,
NULL, ready_fds[i].fd);
850 refresh_watches_for_fd (loop, watches, ready_fds[i].fd);
856 _dbus_verbose (
" moving to next iteration\n");
859 if (_dbus_loop_dispatch (loop))
863 _dbus_verbose (
"Returning %d\n", retval);
870 _dbus_loop_run (DBusLoop *loop)
876 _dbus_loop_ref (loop);
878 our_exit_depth = loop->depth;
881 _dbus_verbose (
"Running main loop, depth %d -> %d\n",
882 loop->depth - 1, loop->depth);
884 while (loop->depth != our_exit_depth)
885 _dbus_loop_iterate (loop,
TRUE);
887 _dbus_loop_unref (loop);
891 _dbus_loop_quit (DBusLoop *loop)
897 _dbus_verbose (
"Quit main loop, depth %d -> %d\n",
898 loop->depth + 1, loop->depth);
902 _dbus_get_oom_wait (
void)
904 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
913 _dbus_wait_for_memory (
void)
915 _dbus_verbose (
"Waiting for more memory\n");