28#include "dbus-mainloop.h"
30#ifndef DOXYGEN_SHOULD_SKIP_THIS
32#include <dbus/dbus-hash.h>
33#include <dbus/dbus-list.h>
34#include <dbus/dbus-pollable-set.h>
35#include <dbus/dbus-timeout.h>
36#include <dbus/dbus-watch.h>
38#define MAINLOOP_SPEW 0
45 DBusPollableSet *pollable_set;
47 int callback_list_serial;
54 unsigned oom_watch_pending : 1;
60 dbus_int64_t last_tv_sec;
64#define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
66static TimeoutCallback*
75 cb->timeout = timeout;
82timeout_callback_free (TimeoutCallback *cb)
88free_watch_table_entry (
void *data)
119 free_watch_table_entry);
121 loop->pollable_set = _dbus_pollable_set_new (0);
123 if (loop->watches ==
NULL || loop->pollable_set ==
NULL)
125 if (loop->watches !=
NULL)
128 if (loop->pollable_set !=
NULL)
129 _dbus_pollable_set_free (loop->pollable_set);
141_dbus_loop_ref (DBusLoop *loop)
152_dbus_loop_unref (DBusLoop *loop)
158 if (loop->refcount == 0)
160 while (loop->need_dispatch)
168 _dbus_pollable_set_free (loop->pollable_set);
174ensure_watch_table_entry (DBusLoop *loop,
179 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
188 if (!_dbus_hash_table_insert_pollable (loop->watches, fd, watches))
199cull_watches_for_invalid_fd (DBusLoop *loop,
205 _dbus_warn (
"invalid request, socket fd %" DBUS_POLLABLE_FORMAT
" not open",
206 _dbus_pollable_printable (fd));
207 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
217 _dbus_hash_table_remove_pollable (loop->watches, fd);
221gc_watch_table_entry (DBusLoop *loop,
230 if (*watches !=
NULL)
233 _dbus_hash_table_remove_pollable (loop->watches, fd);
238refresh_watches_for_fd (DBusLoop *loop,
243 unsigned int flags = 0;
244 dbus_bool_t interested =
FALSE;
249 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
260 !_dbus_watch_get_oom_last_time (link->
data))
268 _dbus_pollable_set_enable (loop->pollable_set, fd, flags);
270 _dbus_pollable_set_disable (loop->pollable_set, fd);
274_dbus_loop_add_watch (DBusLoop *loop,
280 fd = _dbus_watch_get_pollable (watch);
283 watches = ensure_watch_table_entry (loop, fd);
291 gc_watch_table_entry (loop, watches, fd);
298 if (!_dbus_pollable_set_add (loop->pollable_set, fd,
302 _dbus_hash_table_remove_pollable (loop->watches, fd);
309 refresh_watches_for_fd (loop, watches, fd);
312 loop->callback_list_serial += 1;
313 loop->watch_count += 1;
318_dbus_loop_toggle_watch (DBusLoop *loop,
321 refresh_watches_for_fd (loop,
NULL, _dbus_watch_get_pollable (watch));
325_dbus_loop_remove_watch (DBusLoop *loop,
335 fd = _dbus_watch_get_pollable (watch);
338 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
351 loop->callback_list_serial += 1;
352 loop->watch_count -= 1;
357 if (gc_watch_table_entry (loop, watches, fd))
359 _dbus_pollable_set_remove (loop->pollable_set, fd);
369 _dbus_warn (
"could not find watch %p to remove", watch);
373_dbus_loop_add_timeout (DBusLoop *loop,
376 TimeoutCallback *tcb;
378 tcb = timeout_callback_new (timeout);
384 loop->callback_list_serial += 1;
385 loop->timeout_count += 1;
389 timeout_callback_free (tcb);
397_dbus_loop_remove_timeout (DBusLoop *loop,
406 TimeoutCallback *
this = link->
data;
408 if (this->timeout == timeout)
411 loop->callback_list_serial += 1;
412 loop->timeout_count -= 1;
413 timeout_callback_free (
this);
421 _dbus_warn (
"could not find timeout %p to remove", timeout);
428check_timeout (dbus_int64_t tv_sec,
430 TimeoutCallback *tcb,
433 dbus_int64_t sec_remaining;
435 dbus_int64_t expiration_tv_sec;
436 long expiration_tv_usec;
437 dbus_int64_t interval_seconds;
438 long interval_milliseconds;
445 interval_seconds = interval / 1000L;
446 interval_milliseconds = interval % 1000L;
448 expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
449 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
450 if (expiration_tv_usec >= 1000000)
452 expiration_tv_usec -= 1000000;
453 expiration_tv_sec += 1;
456 sec_remaining = expiration_tv_sec - tv_sec;
457 msec_remaining = (expiration_tv_usec - tv_usec) / 1000L;
460 _dbus_verbose (
"Interval is %ld seconds %ld msecs\n",
462 interval_milliseconds);
463 _dbus_verbose (
"Now is %lu seconds %lu usecs\n",
465 _dbus_verbose (
"Last is %lu seconds %lu usecs\n",
466 tcb->last_tv_sec, tcb->last_tv_usec);
467 _dbus_verbose (
"Exp is %lu seconds %lu usecs\n",
468 expiration_tv_sec, expiration_tv_usec);
469 _dbus_verbose (
"Pre-correction, sec_remaining %ld msec_remaining %ld\n",
470 sec_remaining, msec_remaining);
477 if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
483 if (msec_remaining < 0)
485 msec_remaining += 1000;
493 *timeout = sec_remaining * 1000 + msec_remaining;
496 if (*timeout > interval)
499 _dbus_verbose (
"System clock set backward! Resetting timeout.\n");
501 tcb->last_tv_sec = tv_sec;
502 tcb->last_tv_usec = tv_usec;
508 _dbus_verbose (
" timeout expires in %d milliseconds\n", *timeout);
511 return *timeout == 0;
515_dbus_loop_dispatch (DBusLoop *loop)
522 if (loop->need_dispatch ==
NULL)
526 while (loop->need_dispatch !=
NULL)
544 _dbus_wait_for_memory ();
553_dbus_loop_queue_dispatch (DBusLoop *loop,
570_dbus_loop_iterate (DBusLoop *loop,
573#define N_STACK_DESCRIPTORS 64
575 DBusPollableEvent ready_fds[N_STACK_DESCRIPTORS];
585 orig_depth = loop->depth;
588 _dbus_verbose (
"Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
589 block, loop->depth, loop->timeout_count, loop->watch_count);
593 loop->timeouts ==
NULL)
597 if (loop->timeout_count > 0)
608 TimeoutCallback *tcb = link->
data;
616 tcb->last_tv_sec = tv_sec;
617 tcb->last_tv_usec = tv_usec;
621 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
624 timeout = msecs_remaining;
626 timeout = MIN (msecs_remaining, timeout);
629 _dbus_verbose (
" timeout added, %d remaining, aggregate timeout %ld\n",
630 msecs_remaining, timeout);
638 _dbus_verbose (
" skipping disabled timeout\n");
647 if (!block || loop->need_dispatch !=
NULL)
651 _dbus_verbose (
" timeout is 0 as we aren't blocking\n");
658 if (loop->oom_watch_pending)
659 timeout = MIN (timeout, _dbus_get_oom_wait ());
662 _dbus_verbose (
" polling on %d descriptors timeout %ld\n",
_DBUS_N_ELEMENTS (ready_fds), timeout);
665 n_ready = _dbus_pollable_set_poll (loop->pollable_set, ready_fds,
669 if (loop->oom_watch_pending)
673 loop->oom_watch_pending =
FALSE;
684 fd = _dbus_hash_iter_get_pollable_key (&hash_iter);
693 if (_dbus_watch_get_oom_last_time (watch))
695 _dbus_watch_set_oom_last_time (watch,
FALSE);
701 refresh_watches_for_fd (loop, watches, fd);
708 initial_serial = loop->callback_list_serial;
710 if (loop->timeout_count > 0)
722 TimeoutCallback *tcb = link->
data;
724 if (initial_serial != loop->callback_list_serial)
727 if (loop->depth != orig_depth)
734 if (check_timeout (tv_sec, tv_usec,
735 tcb, &msecs_remaining))
738 tcb->last_tv_sec = tv_sec;
739 tcb->last_tv_usec = tv_usec;
742 _dbus_verbose (
" invoking timeout\n");
755 _dbus_verbose (
" timeout has not expired\n");
762 _dbus_verbose (
" skipping invocation of disabled timeout\n");
772 for (i = 0; i < n_ready; i++)
776 unsigned int condition;
783 if (initial_serial != loop->callback_list_serial)
786 if (loop->depth != orig_depth)
791 if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
793 cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
797 condition = ready_fds[i].flags;
806 watches = _dbus_hash_table_lookup_pollable (loop->watches,
830 _dbus_watch_set_oom_last_time (watch,
TRUE);
831 loop->oom_watch_pending =
TRUE;
836 _dbus_verbose (
" Invoked watch, oom = %d\n", oom);
843 if (initial_serial != loop->callback_list_serial ||
844 loop->depth != orig_depth)
847 refresh_watches_for_fd (loop,
NULL, ready_fds[i].fd);
855 refresh_watches_for_fd (loop, watches, ready_fds[i].fd);
861 _dbus_verbose (
" moving to next iteration\n");
864 if (_dbus_loop_dispatch (loop))
868 _dbus_verbose (
"Returning %d\n", retval);
875_dbus_loop_run (DBusLoop *loop)
881 _dbus_loop_ref (loop);
883 our_exit_depth = loop->depth;
886 _dbus_verbose (
"Running main loop, depth %d -> %d\n",
887 loop->depth - 1, loop->depth);
889 while (loop->depth != our_exit_depth)
890 _dbus_loop_iterate (loop,
TRUE);
892 _dbus_loop_unref (loop);
896_dbus_loop_quit (DBusLoop *loop)
902 _dbus_verbose (
"Quit main loop, depth %d -> %d\n",
903 loop->depth + 1, loop->depth);
907_dbus_get_oom_wait (
void)
909#ifdef DBUS_ENABLE_EMBEDDED_TESTS
918_dbus_wait_for_memory (
void)
920 _dbus_verbose (
"Waiting for more memory\n");
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.
DBusDispatchStatus dbus_connection_dispatch(DBusConnection *connection)
Processes any incoming data.
DBusDispatchStatus
Indicates the status of incoming data on a DBusConnection.
DBusConnection * dbus_connection_ref(DBusConnection *connection)
Increments the reference count of a DBusConnection.
@ DBUS_DISPATCH_NEED_MEMORY
More memory is needed to continue.
@ DBUS_DISPATCH_COMPLETE
All currently available data has been processed.
int _dbus_hash_table_get_n_entries(DBusHashTable *table)
Gets the number of hash entries in a hash table.
void * _dbus_hash_iter_get_value(DBusHashIter *iter)
Gets the value of the current entry.
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero.
dbus_bool_t _dbus_hash_iter_next(DBusHashIter *iter)
Move the hash iterator forward one step, to the next hash entry.
void _dbus_hash_iter_init(DBusHashTable *table, DBusHashIter *iter)
Initializes a hash table iterator.
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_INT_MAX
Maximum value of type "int".
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
dbus_bool_t _dbus_list_length_is_one(DBusList **list)
Check whether length is exactly one.
void _dbus_list_remove_link(DBusList **list, DBusList *link)
Removes a link from the list.
void * _dbus_list_pop_first(DBusList **list)
Removes the first value in the list and returns it.
int _dbus_list_get_length(DBusList **list)
Gets the length of a list.
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
void _dbus_get_monotonic_time(dbus_int64_t *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
void _dbus_timeout_restarted(DBusTimeout *timeout)
Mark timeout as restarted (setting timestamps is responsibility of the event loop).
dbus_bool_t _dbus_timeout_needs_restart(DBusTimeout *timeout)
Returns whether a timeout needs restart time counting in the event loop.
DBUS_EXPORT dbus_bool_t dbus_timeout_handle(DBusTimeout *timeout)
Calls the timeout handler for this timeout.
DBUS_EXPORT dbus_bool_t dbus_timeout_get_enabled(DBusTimeout *timeout)
Returns whether a timeout is enabled or not.
DBUS_EXPORT int dbus_timeout_get_interval(DBusTimeout *timeout)
Gets the timeout interval.
DBusWatch * _dbus_watch_ref(DBusWatch *watch)
Increments the reference count of a DBusWatch object.
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it.
DBUS_EXPORT dbus_bool_t dbus_watch_handle(DBusWatch *watch, unsigned int flags)
Called to notify the D-Bus library when a previously-added watch is ready for reading or writing,...
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
DBUS_EXPORT unsigned int dbus_watch_get_flags(DBusWatch *watch)
Gets flags from DBusWatchFlags indicating what conditions should be monitored on the file descriptor.
Implementation details of DBusConnection.
Internals of DBusHashTable.
void * data
Data stored at this element.
Internals of DBusTimeout.
Implementation of DBusWatch.