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;
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,
176 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
185 if (!_dbus_hash_table_insert_pollable (loop->watches, fd, watches))
196 cull_watches_for_invalid_fd (DBusLoop *loop,
202 _dbus_warn (
"invalid request, socket fd %" DBUS_POLLABLE_FORMAT
" not open\n",
203 _dbus_pollable_printable (fd));
204 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
214 _dbus_hash_table_remove_pollable (loop->watches, fd);
218 gc_watch_table_entry (DBusLoop *loop,
227 if (*watches !=
NULL)
230 _dbus_hash_table_remove_pollable (loop->watches, fd);
235 refresh_watches_for_fd (DBusLoop *loop,
240 unsigned int flags = 0;
246 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
257 !_dbus_watch_get_oom_last_time (link->
data))
265 _dbus_socket_set_enable (loop->socket_set, fd, flags);
267 _dbus_socket_set_disable (loop->socket_set, fd);
271 _dbus_loop_add_watch (DBusLoop *loop,
277 fd = _dbus_watch_get_pollable (watch);
280 watches = ensure_watch_table_entry (loop, fd);
288 gc_watch_table_entry (loop, watches, fd);
295 if (!_dbus_socket_set_add (loop->socket_set, fd,
299 _dbus_hash_table_remove_pollable (loop->watches, fd);
306 refresh_watches_for_fd (loop, watches, fd);
309 loop->callback_list_serial += 1;
310 loop->watch_count += 1;
315 _dbus_loop_toggle_watch (DBusLoop *loop,
318 refresh_watches_for_fd (loop,
NULL, _dbus_watch_get_pollable (watch));
322 _dbus_loop_remove_watch (DBusLoop *loop,
332 fd = _dbus_watch_get_pollable (watch);
335 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
348 loop->callback_list_serial += 1;
349 loop->watch_count -= 1;
354 if (gc_watch_table_entry (loop, watches, fd))
356 _dbus_socket_set_remove (loop->socket_set, fd);
366 _dbus_warn (
"could not find watch %p to remove\n", watch);
370 _dbus_loop_add_timeout (DBusLoop *loop,
373 TimeoutCallback *tcb;
375 tcb = timeout_callback_new (timeout);
381 loop->callback_list_serial += 1;
382 loop->timeout_count += 1;
386 timeout_callback_free (tcb);
394 _dbus_loop_remove_timeout (DBusLoop *loop,
403 TimeoutCallback *
this = link->
data;
405 if (this->timeout == timeout)
408 loop->callback_list_serial += 1;
409 loop->timeout_count -= 1;
410 timeout_callback_free (
this);
418 _dbus_warn (
"could not find timeout %p to remove\n", timeout);
425 check_timeout (
long tv_sec,
427 TimeoutCallback *tcb,
432 long expiration_tv_sec;
433 long expiration_tv_usec;
434 long interval_seconds;
435 long interval_milliseconds;
442 interval_seconds = interval / 1000L;
443 interval_milliseconds = interval % 1000L;
445 expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
446 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
447 if (expiration_tv_usec >= 1000000)
449 expiration_tv_usec -= 1000000;
450 expiration_tv_sec += 1;
453 sec_remaining = expiration_tv_sec - tv_sec;
454 msec_remaining = (expiration_tv_usec - tv_usec) / 1000L;
457 _dbus_verbose (
"Interval is %ld seconds %ld msecs\n",
459 interval_milliseconds);
460 _dbus_verbose (
"Now is %lu seconds %lu usecs\n",
462 _dbus_verbose (
"Last is %lu seconds %lu usecs\n",
463 tcb->last_tv_sec, tcb->last_tv_usec);
464 _dbus_verbose (
"Exp is %lu seconds %lu usecs\n",
465 expiration_tv_sec, expiration_tv_usec);
466 _dbus_verbose (
"Pre-correction, sec_remaining %ld msec_remaining %ld\n",
467 sec_remaining, msec_remaining);
474 if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
480 if (msec_remaining < 0)
482 msec_remaining += 1000;
490 *timeout = sec_remaining * 1000 + msec_remaining;
493 if (*timeout > interval)
496 _dbus_verbose (
"System clock set backward! Resetting timeout.\n");
498 tcb->last_tv_sec = tv_sec;
499 tcb->last_tv_usec = tv_usec;
505 _dbus_verbose (
" timeout expires in %d milliseconds\n", *timeout);
508 return *timeout == 0;
512 _dbus_loop_dispatch (DBusLoop *loop)
519 if (loop->need_dispatch ==
NULL)
523 while (loop->need_dispatch !=
NULL)
541 _dbus_wait_for_memory ();
550 _dbus_loop_queue_dispatch (DBusLoop *loop,
567 _dbus_loop_iterate (DBusLoop *loop,
570 #define N_STACK_DESCRIPTORS 64
572 DBusSocketEvent ready_fds[N_STACK_DESCRIPTORS];
582 orig_depth = loop->depth;
585 _dbus_verbose (
"Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
586 block, loop->depth, loop->timeout_count, loop->watch_count);
590 loop->timeouts ==
NULL)
594 if (loop->timeout_count > 0)
605 TimeoutCallback *tcb = link->
data;
611 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
614 timeout = msecs_remaining;
616 timeout = MIN (msecs_remaining, timeout);
619 _dbus_verbose (
" timeout added, %d remaining, aggregate timeout %ld\n",
620 msecs_remaining, timeout);
631 _dbus_verbose (
" skipping disabled timeout\n");
640 if (!block || loop->need_dispatch !=
NULL)
644 _dbus_verbose (
" timeout is 0 as we aren't blocking\n");
651 if (loop->oom_watch_pending)
652 timeout = MIN (timeout, _dbus_get_oom_wait ());
655 _dbus_verbose (
" polling on %d descriptors timeout %ld\n",
_DBUS_N_ELEMENTS (ready_fds), timeout);
658 n_ready = _dbus_socket_set_poll (loop->socket_set, ready_fds,
662 if (loop->oom_watch_pending)
666 loop->oom_watch_pending =
FALSE;
677 fd = _dbus_hash_iter_get_pollable_key (&hash_iter);
686 if (_dbus_watch_get_oom_last_time (watch))
688 _dbus_watch_set_oom_last_time (watch,
FALSE);
694 refresh_watches_for_fd (loop, watches, fd);
701 initial_serial = loop->callback_list_serial;
703 if (loop->timeout_count > 0)
715 TimeoutCallback *tcb = link->
data;
717 if (initial_serial != loop->callback_list_serial)
720 if (loop->depth != orig_depth)
727 if (check_timeout (tv_sec, tv_usec,
728 tcb, &msecs_remaining))
731 tcb->last_tv_sec = tv_sec;
732 tcb->last_tv_usec = tv_usec;
735 _dbus_verbose (
" invoking timeout\n");
748 _dbus_verbose (
" timeout has not expired\n");
755 _dbus_verbose (
" skipping invocation of disabled timeout\n");
765 for (i = 0; i < n_ready; i++)
769 unsigned int condition;
776 if (initial_serial != loop->callback_list_serial)
779 if (loop->depth != orig_depth)
784 if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
786 cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
790 condition = ready_fds[i].flags;
799 watches = _dbus_hash_table_lookup_pollable (loop->watches,
823 _dbus_watch_set_oom_last_time (watch,
TRUE);
824 loop->oom_watch_pending =
TRUE;
829 _dbus_verbose (
" Invoked watch, oom = %d\n", oom);
836 if (initial_serial != loop->callback_list_serial ||
837 loop->depth != orig_depth)
840 refresh_watches_for_fd (loop,
NULL, ready_fds[i].fd);
848 refresh_watches_for_fd (loop, watches, ready_fds[i].fd);
854 _dbus_verbose (
" moving to next iteration\n");
857 if (_dbus_loop_dispatch (loop))
861 _dbus_verbose (
"Returning %d\n", retval);
868 _dbus_loop_run (DBusLoop *loop)
874 _dbus_loop_ref (loop);
876 our_exit_depth = loop->depth;
879 _dbus_verbose (
"Running main loop, depth %d -> %d\n",
880 loop->depth - 1, loop->depth);
882 while (loop->depth != our_exit_depth)
883 _dbus_loop_iterate (loop,
TRUE);
885 _dbus_loop_unref (loop);
889 _dbus_loop_quit (DBusLoop *loop)
895 _dbus_verbose (
"Quit main loop, depth %d -> %d\n",
896 loop->depth + 1, loop->depth);
900 _dbus_get_oom_wait (
void)
902 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
911 _dbus_wait_for_memory (
void)
913 _dbus_verbose (
"Waiting for more memory\n");
Internals of DBusTimeout.
DBusDispatchStatus
Indicates the status of incoming data on a DBusConnection.
Implementation of DBusWatch.
#define NULL
A null pointer, defined appropriately for C or C++.
More memory is needed to continue.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
dbus_bool_t _dbus_list_length_is_one(DBusList **list)
Check whether length is exactly one.
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
void _dbus_list_remove_link(DBusList **list, DBusList *link)
Removes a link from the list.
DBusConnection * dbus_connection_ref(DBusConnection *connection)
Increments the reference count of a DBusConnection.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void * data
Data stored at this element.
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero...
Implementation details of DBusConnection.
dbus_bool_t _dbus_hash_iter_next(DBusHashIter *iter)
Move the hash iterator forward one step, to the next hash entry.
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
#define _DBUS_INT_MAX
Maximum value of type "int".
DBUS_EXPORT dbus_bool_t dbus_timeout_get_enabled(DBusTimeout *timeout)
Returns whether a timeout is enabled or not.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
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.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
DBUS_EXPORT dbus_bool_t dbus_timeout_handle(DBusTimeout *timeout)
Calls the timeout handler for this timeout.
DBusDispatchStatus dbus_connection_dispatch(DBusConnection *connection)
Processes any incoming data.
All currently available data has been processed.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it...
void _dbus_get_monotonic_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
void * _dbus_list_pop_first(DBusList **list)
Removes the first value in the list and returns it.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
#define TRUE
Expands to "1".
void _dbus_hash_iter_init(DBusHashTable *table, DBusHashIter *iter)
Initializes a hash table iterator.
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
int _dbus_list_get_length(DBusList **list)
Gets the length of a list.
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
#define FALSE
Expands to "0".
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...
DBusWatch * _dbus_watch_ref(DBusWatch *watch)
Increments the reference count of a DBusWatch object.
DBUS_EXPORT unsigned int dbus_watch_get_flags(DBusWatch *watch)
Gets flags from DBusWatchFlags indicating what conditions should be monitored on the file descriptor...
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero...
Internals of DBusHashTable.
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
DBUS_EXPORT int dbus_timeout_get_interval(DBusTimeout *timeout)
Gets the timeout interval.