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");
Internals of DBusTimeout.
int _dbus_hash_table_get_n_entries(DBusHashTable *table)
Gets the number of hash entries in a hash table.
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.
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it...
#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.
Implementation details of DBusConnection.
dbus_bool_t _dbus_hash_table_remove_int(DBusHashTable *table, int key)
Removes the hash entry for the given key.
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero...
#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".
void * _dbus_hash_table_lookup_int(DBusHashTable *table, int key)
Looks up the value for a given integer in a hash table of type DBUS_HASH_INT.
void _dbus_get_monotonic_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
DBUS_EXPORT dbus_bool_t dbus_timeout_get_enabled(DBusTimeout *timeout)
Returns whether a timeout is enabled or not.
int _dbus_hash_iter_get_int_key(DBusHashIter *iter)
Gets the key for the current entry.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
DBUS_EXPORT int dbus_watch_get_socket(DBusWatch *watch)
Returns a socket to be watched, on UNIX this will return -1 if our transport is not socket-based so d...
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.
dbus_bool_t _dbus_hash_iter_next(DBusHashIter *iter)
Move the hash iterator forward one step, to the next hash entry.
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().
dbus_bool_t _dbus_hash_table_insert_int(DBusHashTable *table, int key, void *value)
Creates a hash entry with the given key and value.
#define TRUE
Expands to "1".
DBusWatch * _dbus_watch_ref(DBusWatch *watch)
Increments the reference count of a DBusWatch object.
int _dbus_list_get_length(DBusList **list)
Gets the length of a list.
void _dbus_hash_iter_init(DBusHashTable *table, DBusHashIter *iter)
Initializes a hash table iterator.
void * _dbus_hash_iter_get_value(DBusHashIter *iter)
Gets the value of the current entry.
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...
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
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.
DBUS_EXPORT int dbus_timeout_get_interval(DBusTimeout *timeout)
Gets the timeout interval.