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-pollable-set.h>
33#include <dbus/dbus-timeout.h>
34#include <dbus/dbus-watch.h>
36#define MAINLOOP_SPEW 0
43 DBusPollableSet *pollable_set;
45 int callback_list_serial;
52 unsigned oom_watch_pending : 1;
62#define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
64static TimeoutCallback*
73 cb->timeout = timeout;
80timeout_callback_free (TimeoutCallback *cb)
86free_watch_table_entry (
void *data)
117 free_watch_table_entry);
119 loop->pollable_set = _dbus_pollable_set_new (0);
121 if (loop->watches ==
NULL || loop->pollable_set ==
NULL)
123 if (loop->watches !=
NULL)
126 if (loop->pollable_set !=
NULL)
127 _dbus_pollable_set_free (loop->pollable_set);
139_dbus_loop_ref (DBusLoop *loop)
150_dbus_loop_unref (DBusLoop *loop)
156 if (loop->refcount == 0)
158 while (loop->need_dispatch)
166 _dbus_pollable_set_free (loop->pollable_set);
172ensure_watch_table_entry (DBusLoop *loop,
177 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
186 if (!_dbus_hash_table_insert_pollable (loop->watches, fd, watches))
197cull_watches_for_invalid_fd (DBusLoop *loop,
203 _dbus_warn (
"invalid request, socket fd %" DBUS_POLLABLE_FORMAT
" not open",
204 _dbus_pollable_printable (fd));
205 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
215 _dbus_hash_table_remove_pollable (loop->watches, fd);
219gc_watch_table_entry (DBusLoop *loop,
228 if (*watches !=
NULL)
231 _dbus_hash_table_remove_pollable (loop->watches, fd);
236refresh_watches_for_fd (DBusLoop *loop,
241 unsigned int flags = 0;
247 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
258 !_dbus_watch_get_oom_last_time (link->
data))
266 _dbus_pollable_set_enable (loop->pollable_set, fd, flags);
268 _dbus_pollable_set_disable (loop->pollable_set, fd);
272_dbus_loop_add_watch (DBusLoop *loop,
278 fd = _dbus_watch_get_pollable (watch);
281 watches = ensure_watch_table_entry (loop, fd);
289 gc_watch_table_entry (loop, watches, fd);
296 if (!_dbus_pollable_set_add (loop->pollable_set, fd,
300 _dbus_hash_table_remove_pollable (loop->watches, fd);
307 refresh_watches_for_fd (loop, watches, fd);
310 loop->callback_list_serial += 1;
311 loop->watch_count += 1;
316_dbus_loop_toggle_watch (DBusLoop *loop,
319 refresh_watches_for_fd (loop,
NULL, _dbus_watch_get_pollable (watch));
323_dbus_loop_remove_watch (DBusLoop *loop,
333 fd = _dbus_watch_get_pollable (watch);
336 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
349 loop->callback_list_serial += 1;
350 loop->watch_count -= 1;
355 if (gc_watch_table_entry (loop, watches, fd))
357 _dbus_pollable_set_remove (loop->pollable_set, fd);
367 _dbus_warn (
"could not find watch %p to remove", watch);
371_dbus_loop_add_timeout (DBusLoop *loop,
374 TimeoutCallback *tcb;
376 tcb = timeout_callback_new (timeout);
382 loop->callback_list_serial += 1;
383 loop->timeout_count += 1;
387 timeout_callback_free (tcb);
395_dbus_loop_remove_timeout (DBusLoop *loop,
404 TimeoutCallback *
this = link->
data;
406 if (this->timeout == timeout)
409 loop->callback_list_serial += 1;
410 loop->timeout_count -= 1;
411 timeout_callback_free (
this);
419 _dbus_warn (
"could not find timeout %p to remove", timeout);
426check_timeout (
long tv_sec,
428 TimeoutCallback *tcb,
433 long expiration_tv_sec;
434 long expiration_tv_usec;
435 long interval_seconds;
436 long interval_milliseconds;
443 interval_seconds = interval / 1000L;
444 interval_milliseconds = interval % 1000L;
446 expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
447 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
448 if (expiration_tv_usec >= 1000000)
450 expiration_tv_usec -= 1000000;
451 expiration_tv_sec += 1;
454 sec_remaining = expiration_tv_sec - tv_sec;
455 msec_remaining = (expiration_tv_usec - tv_usec) / 1000L;
458 _dbus_verbose (
"Interval is %ld seconds %ld msecs\n",
460 interval_milliseconds);
461 _dbus_verbose (
"Now is %lu seconds %lu usecs\n",
463 _dbus_verbose (
"Last is %lu seconds %lu usecs\n",
464 tcb->last_tv_sec, tcb->last_tv_usec);
465 _dbus_verbose (
"Exp is %lu seconds %lu usecs\n",
466 expiration_tv_sec, expiration_tv_usec);
467 _dbus_verbose (
"Pre-correction, sec_remaining %ld msec_remaining %ld\n",
468 sec_remaining, msec_remaining);
475 if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
481 if (msec_remaining < 0)
483 msec_remaining += 1000;
491 *timeout = sec_remaining * 1000 + msec_remaining;
494 if (*timeout > interval)
497 _dbus_verbose (
"System clock set backward! Resetting timeout.\n");
499 tcb->last_tv_sec = tv_sec;
500 tcb->last_tv_usec = tv_usec;
506 _dbus_verbose (
" timeout expires in %d milliseconds\n", *timeout);
509 return *timeout == 0;
513_dbus_loop_dispatch (DBusLoop *loop)
520 if (loop->need_dispatch ==
NULL)
524 while (loop->need_dispatch !=
NULL)
542 _dbus_wait_for_memory ();
551_dbus_loop_queue_dispatch (DBusLoop *loop,
568_dbus_loop_iterate (DBusLoop *loop,
571#define N_STACK_DESCRIPTORS 64
573 DBusPollableEvent ready_fds[N_STACK_DESCRIPTORS];
583 orig_depth = loop->depth;
586 _dbus_verbose (
"Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
587 block, loop->depth, loop->timeout_count, loop->watch_count);
591 loop->timeouts ==
NULL)
595 if (loop->timeout_count > 0)
606 TimeoutCallback *tcb = link->
data;
614 tcb->last_tv_sec = tv_sec;
615 tcb->last_tv_usec = tv_usec;
619 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
622 timeout = msecs_remaining;
624 timeout = MIN (msecs_remaining, timeout);
627 _dbus_verbose (
" timeout added, %d remaining, aggregate timeout %ld\n",
628 msecs_remaining, timeout);
636 _dbus_verbose (
" skipping disabled timeout\n");
645 if (!block || loop->need_dispatch !=
NULL)
649 _dbus_verbose (
" timeout is 0 as we aren't blocking\n");
656 if (loop->oom_watch_pending)
657 timeout = MIN (timeout, _dbus_get_oom_wait ());
660 _dbus_verbose (
" polling on %d descriptors timeout %ld\n",
_DBUS_N_ELEMENTS (ready_fds), timeout);
663 n_ready = _dbus_pollable_set_poll (loop->pollable_set, ready_fds,
667 if (loop->oom_watch_pending)
671 loop->oom_watch_pending =
FALSE;
682 fd = _dbus_hash_iter_get_pollable_key (&hash_iter);
691 if (_dbus_watch_get_oom_last_time (watch))
693 _dbus_watch_set_oom_last_time (watch,
FALSE);
699 refresh_watches_for_fd (loop, watches, fd);
706 initial_serial = loop->callback_list_serial;
708 if (loop->timeout_count > 0)
720 TimeoutCallback *tcb = link->
data;
722 if (initial_serial != loop->callback_list_serial)
725 if (loop->depth != orig_depth)
732 if (check_timeout (tv_sec, tv_usec,
733 tcb, &msecs_remaining))
736 tcb->last_tv_sec = tv_sec;
737 tcb->last_tv_usec = tv_usec;
740 _dbus_verbose (
" invoking timeout\n");
753 _dbus_verbose (
" timeout has not expired\n");
760 _dbus_verbose (
" skipping invocation of disabled timeout\n");
770 for (i = 0; i < n_ready; i++)
774 unsigned int condition;
781 if (initial_serial != loop->callback_list_serial)
784 if (loop->depth != orig_depth)
789 if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
791 cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
795 condition = ready_fds[i].flags;
804 watches = _dbus_hash_table_lookup_pollable (loop->watches,
828 _dbus_watch_set_oom_last_time (watch,
TRUE);
829 loop->oom_watch_pending =
TRUE;
834 _dbus_verbose (
" Invoked watch, oom = %d\n", oom);
841 if (initial_serial != loop->callback_list_serial ||
842 loop->depth != orig_depth)
845 refresh_watches_for_fd (loop,
NULL, ready_fds[i].fd);
853 refresh_watches_for_fd (loop, watches, ready_fds[i].fd);
859 _dbus_verbose (
" moving to next iteration\n");
862 if (_dbus_loop_dispatch (loop))
866 _dbus_verbose (
"Returning %d\n", retval);
873_dbus_loop_run (DBusLoop *loop)
879 _dbus_loop_ref (loop);
881 our_exit_depth = loop->depth;
884 _dbus_verbose (
"Running main loop, depth %d -> %d\n",
885 loop->depth - 1, loop->depth);
887 while (loop->depth != our_exit_depth)
888 _dbus_loop_iterate (loop,
TRUE);
890 _dbus_loop_unref (loop);
894_dbus_loop_quit (DBusLoop *loop)
900 _dbus_verbose (
"Quit main loop, depth %d -> %d\n",
901 loop->depth + 1, loop->depth);
905_dbus_get_oom_wait (
void)
907#ifdef DBUS_ENABLE_EMBEDDED_TESTS
916_dbus_wait_for_memory (
void)
918 _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_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
void _dbus_get_monotonic_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
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.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
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.