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;
51 DBusList *need_dispatch;
54 unsigned oom_watch_pending : 1;
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;
249 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
259 !_dbus_watch_get_oom_last_time (link->
data))
267 _dbus_pollable_set_enable (loop->pollable_set, fd, flags);
269 _dbus_pollable_set_disable (loop->pollable_set, fd);
273_dbus_loop_add_watch (DBusLoop *loop,
279 fd = _dbus_watch_get_pollable (watch);
282 watches = ensure_watch_table_entry (loop, fd);
290 gc_watch_table_entry (loop, watches, fd);
297 if (!_dbus_pollable_set_add (loop->pollable_set, fd,
301 _dbus_hash_table_remove_pollable (loop->watches, fd);
308 refresh_watches_for_fd (loop, watches, fd);
311 loop->callback_list_serial += 1;
312 loop->watch_count += 1;
317_dbus_loop_toggle_watch (DBusLoop *loop,
320 refresh_watches_for_fd (loop,
NULL, _dbus_watch_get_pollable (watch));
324_dbus_loop_remove_watch (DBusLoop *loop,
334 fd = _dbus_watch_get_pollable (watch);
337 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
350 loop->callback_list_serial += 1;
351 loop->watch_count -= 1;
356 if (gc_watch_table_entry (loop, watches, fd))
358 _dbus_pollable_set_remove (loop->pollable_set, fd);
368 _dbus_warn (
"could not find watch %p to remove", watch);
372_dbus_loop_add_timeout (DBusLoop *loop,
375 TimeoutCallback *tcb;
377 tcb = timeout_callback_new (timeout);
383 loop->callback_list_serial += 1;
384 loop->timeout_count += 1;
388 timeout_callback_free (tcb);
396_dbus_loop_remove_timeout (DBusLoop *loop,
405 TimeoutCallback *
this = link->
data;
407 if (this->timeout == timeout)
410 loop->callback_list_serial += 1;
411 loop->timeout_count -= 1;
412 timeout_callback_free (
this);
420 _dbus_warn (
"could not find timeout %p to remove", timeout);
429 TimeoutCallback *tcb,
435 long expiration_tv_usec;
437 long interval_milliseconds;
444 interval_seconds = interval / 1000L;
445 interval_milliseconds = interval % 1000L;
447 expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
448 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
449 if (expiration_tv_usec >= 1000000)
451 expiration_tv_usec -= 1000000;
452 expiration_tv_sec += 1;
455 sec_remaining = expiration_tv_sec - tv_sec;
456 msec_remaining = (expiration_tv_usec - 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,
569min_poll_timeout (
int a,
573 return a < 0 ? b : a;
575 return b < 0 ? a : b;
583_dbus_loop_iterate (DBusLoop *loop,
586#define N_STACK_DESCRIPTORS 64
588 DBusPollableEvent ready_fds[N_STACK_DESCRIPTORS];
598 orig_depth = loop->depth;
601 _dbus_verbose (
"Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
602 block, loop->depth, loop->timeout_count, loop->watch_count);
606 loop->timeouts ==
NULL)
610 if (loop->timeout_count > 0)
621 TimeoutCallback *tcb = link->
data;
629 tcb->last_tv_sec = tv_sec;
630 tcb->last_tv_usec = tv_usec;
634 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
636 timeout = min_poll_timeout (msecs_remaining, timeout);
639 _dbus_verbose (
" timeout added, %d remaining, aggregate timeout %ld\n",
640 msecs_remaining, timeout);
648 _dbus_verbose (
" skipping disabled timeout\n");
657 if (!block || loop->need_dispatch !=
NULL)
661 _dbus_verbose (
" timeout is 0 as we aren't blocking\n");
668 if (loop->oom_watch_pending)
669 timeout = min_poll_timeout (timeout, _dbus_get_oom_wait ());
672 _dbus_verbose (
" polling on %d descriptors timeout %ld\n",
_DBUS_N_ELEMENTS (ready_fds), timeout);
675 n_ready = _dbus_pollable_set_poll (loop->pollable_set, ready_fds,
679 if (loop->oom_watch_pending)
683 loop->oom_watch_pending =
FALSE;
694 fd = _dbus_hash_iter_get_pollable_key (&hash_iter);
703 if (_dbus_watch_get_oom_last_time (watch))
705 _dbus_watch_set_oom_last_time (watch,
FALSE);
711 refresh_watches_for_fd (loop, watches, fd);
718 initial_serial = loop->callback_list_serial;
720 if (loop->timeout_count > 0)
732 TimeoutCallback *tcb = link->
data;
734 if (initial_serial != loop->callback_list_serial)
737 if (loop->depth != orig_depth)
744 if (check_timeout (tv_sec, tv_usec,
745 tcb, &msecs_remaining))
748 tcb->last_tv_sec = tv_sec;
749 tcb->last_tv_usec = tv_usec;
752 _dbus_verbose (
" invoking timeout\n");
765 _dbus_verbose (
" timeout has not expired\n");
772 _dbus_verbose (
" skipping invocation of disabled timeout\n");
782 for (i = 0; i < n_ready; i++)
786 unsigned int condition;
793 if (initial_serial != loop->callback_list_serial)
796 if (loop->depth != orig_depth)
801 if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
803 cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
807 condition = ready_fds[i].flags;
816 watches = _dbus_hash_table_lookup_pollable (loop->watches,
840 _dbus_watch_set_oom_last_time (watch,
TRUE);
841 loop->oom_watch_pending =
TRUE;
846 _dbus_verbose (
" Invoked watch, oom = %d\n", oom);
853 if (initial_serial != loop->callback_list_serial ||
854 loop->depth != orig_depth)
857 refresh_watches_for_fd (loop,
NULL, ready_fds[i].fd);
865 refresh_watches_for_fd (loop, watches, ready_fds[i].fd);
871 _dbus_verbose (
" moving to next iteration\n");
874 if (_dbus_loop_dispatch (loop))
878 _dbus_verbose (
"Returning %d\n", retval);
885_dbus_loop_run (DBusLoop *loop)
891 _dbus_loop_ref (loop);
893 our_exit_depth = loop->depth;
896 _dbus_verbose (
"Running main loop, depth %d -> %d\n",
897 loop->depth - 1, loop->depth);
899 while (loop->depth != our_exit_depth)
900 _dbus_loop_iterate (loop,
TRUE);
902 _dbus_loop_unref (loop);
906_dbus_loop_quit (DBusLoop *loop)
912 _dbus_verbose (
"Quit main loop, depth %d -> %d\n",
913 loop->depth + 1, loop->depth);
917_dbus_get_oom_wait (
void)
919#ifdef DBUS_ENABLE_EMBEDDED_TESTS
928_dbus_wait_for_memory (
void)
930 _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.
struct DBusHashTable DBusHashTable
Public opaque hash table object.
#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.
_DBUS_GNUC_EXTENSION typedef long dbus_int64_t
A 64-bit signed integer.
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.
void * data
Data stored at this element.
Internals of DBusTimeout.
Implementation of DBusWatch.