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
38 #ifdef DBUS_ENABLE_VERBOSE_MODE
40 watch_flags_to_string (
int flags)
42 const char *watch_type;
46 watch_type =
"readwrite";
47 else if (flags & DBUS_WATCH_READABLE)
49 else if (flags & DBUS_WATCH_WRITABLE)
52 watch_type =
"not read or write";
63 DBusSocketSet *socket_set;
65 int callback_list_serial;
72 unsigned oom_watch_pending : 1;
78 unsigned long last_tv_sec;
79 unsigned long last_tv_usec;
82 #define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
84 static TimeoutCallback*
93 cb->timeout = timeout;
100 timeout_callback_free (TimeoutCallback *cb)
106 free_watch_table_entry (
void *data)
128 _dbus_loop_new (
void)
137 free_watch_table_entry);
139 loop->socket_set = _dbus_socket_set_new (0);
141 if (loop->watches ==
NULL || loop->socket_set ==
NULL)
143 if (loop->watches !=
NULL)
146 if (loop->socket_set !=
NULL)
147 _dbus_socket_set_free (loop->socket_set);
159 _dbus_loop_ref (DBusLoop *loop)
170 _dbus_loop_unref (DBusLoop *loop)
176 if (loop->refcount == 0)
178 while (loop->need_dispatch)
186 _dbus_socket_set_free (loop->socket_set);
192 ensure_watch_table_entry (DBusLoop *loop,
217 cull_watches_for_invalid_fd (DBusLoop *loop,
223 _dbus_warn (
"invalid request, socket fd %d not open\n", fd);
238 gc_watch_table_entry (DBusLoop *loop,
247 if (*watches !=
NULL)
255 refresh_watches_for_fd (DBusLoop *loop,
260 unsigned int flags = 0;
277 !_dbus_watch_get_oom_last_time (link->
data))
285 _dbus_socket_set_enable (loop->socket_set, fd, flags);
287 _dbus_socket_set_disable (loop->socket_set, fd);
291 _dbus_loop_add_watch (DBusLoop *loop,
300 watches = ensure_watch_table_entry (loop, fd);
308 gc_watch_table_entry (loop, watches, fd);
315 if (!_dbus_socket_set_add (loop->socket_set, fd,
326 refresh_watches_for_fd (loop, watches, fd);
329 loop->callback_list_serial += 1;
330 loop->watch_count += 1;
335 _dbus_loop_toggle_watch (DBusLoop *loop,
342 _dbus_loop_remove_watch (DBusLoop *loop,
368 loop->callback_list_serial += 1;
369 loop->watch_count -= 1;
374 if (gc_watch_table_entry (loop, watches, fd))
376 _dbus_socket_set_remove (loop->socket_set, fd);
386 _dbus_warn (
"could not find watch %p to remove\n", watch);
390 _dbus_loop_add_timeout (DBusLoop *loop,
393 TimeoutCallback *tcb;
395 tcb = timeout_callback_new (timeout);
401 loop->callback_list_serial += 1;
402 loop->timeout_count += 1;
406 timeout_callback_free (tcb);
414 _dbus_loop_remove_timeout (DBusLoop *loop,
423 TimeoutCallback *
this = link->
data;
425 if (this->timeout == timeout)
428 loop->callback_list_serial += 1;
429 loop->timeout_count -= 1;
430 timeout_callback_free (
this);
438 _dbus_warn (
"could not find timeout %p to remove\n", timeout);
445 check_timeout (
unsigned long tv_sec,
446 unsigned long tv_usec,
447 TimeoutCallback *tcb,
452 unsigned long expiration_tv_sec;
453 unsigned long expiration_tv_usec;
454 long interval_seconds;
455 long interval_milliseconds;
462 interval_seconds = interval / 1000L;
463 interval_milliseconds = interval % 1000L;
465 expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
466 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
467 if (expiration_tv_usec >= 1000000)
469 expiration_tv_usec -= 1000000;
470 expiration_tv_sec += 1;
473 sec_remaining = expiration_tv_sec - tv_sec;
477 msec_remaining = ((long) expiration_tv_usec - (
long) tv_usec) / 1000L;
480 _dbus_verbose (
"Interval is %ld seconds %ld msecs\n",
482 interval_milliseconds);
483 _dbus_verbose (
"Now is %lu seconds %lu usecs\n",
485 _dbus_verbose (
"Last is %lu seconds %lu usecs\n",
486 tcb->last_tv_sec, tcb->last_tv_usec);
487 _dbus_verbose (
"Exp is %lu seconds %lu usecs\n",
488 expiration_tv_sec, expiration_tv_usec);
489 _dbus_verbose (
"Pre-correction, sec_remaining %ld msec_remaining %ld\n",
490 sec_remaining, msec_remaining);
497 if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
503 if (msec_remaining < 0)
505 msec_remaining += 1000;
513 *timeout = sec_remaining * 1000 + msec_remaining;
516 if (*timeout > interval)
519 _dbus_verbose (
"System clock set backward! Resetting timeout.\n");
521 tcb->last_tv_sec = tv_sec;
522 tcb->last_tv_usec = tv_usec;
528 _dbus_verbose (
" timeout expires in %d milliseconds\n", *timeout);
531 return *timeout == 0;
535 _dbus_loop_dispatch (DBusLoop *loop)
542 if (loop->need_dispatch ==
NULL)
546 while (loop->need_dispatch !=
NULL)
564 _dbus_wait_for_memory ();
573 _dbus_loop_queue_dispatch (DBusLoop *loop,
590 _dbus_loop_iterate (DBusLoop *loop,
593 #define N_STACK_DESCRIPTORS 64
595 DBusSocketEvent ready_fds[N_STACK_DESCRIPTORS];
605 orig_depth = loop->depth;
608 _dbus_verbose (
"Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
609 block, loop->depth, loop->timeout_count, loop->watch_count);
613 loop->timeouts ==
NULL)
617 if (loop->timeout_count > 0)
619 unsigned long tv_sec;
620 unsigned long tv_usec;
628 TimeoutCallback *tcb = link->
data;
634 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
637 timeout = msecs_remaining;
639 timeout = MIN (msecs_remaining, timeout);
642 _dbus_verbose (
" timeout added, %d remaining, aggregate timeout %ld\n",
643 msecs_remaining, timeout);
654 _dbus_verbose (
" skipping disabled timeout\n");
663 if (!block || loop->need_dispatch !=
NULL)
667 _dbus_verbose (
" timeout is 0 as we aren't blocking\n");
674 if (loop->oom_watch_pending)
675 timeout = MIN (timeout, _dbus_get_oom_wait ());
678 _dbus_verbose (
" polling on %d descriptors timeout %ld\n", n_fds, timeout);
681 n_ready = _dbus_socket_set_poll (loop->socket_set, ready_fds,
685 if (loop->oom_watch_pending)
689 loop->oom_watch_pending =
FALSE;
709 if (_dbus_watch_get_oom_last_time (watch))
711 _dbus_watch_set_oom_last_time (watch,
FALSE);
717 refresh_watches_for_fd (loop, watches, fd);
724 initial_serial = loop->callback_list_serial;
726 if (loop->timeout_count > 0)
728 unsigned long tv_sec;
729 unsigned long tv_usec;
738 TimeoutCallback *tcb = link->
data;
740 if (initial_serial != loop->callback_list_serial)
743 if (loop->depth != orig_depth)
750 if (check_timeout (tv_sec, tv_usec,
751 tcb, &msecs_remaining))
754 tcb->last_tv_sec = tv_sec;
755 tcb->last_tv_usec = tv_usec;
758 _dbus_verbose (
" invoking timeout\n");
771 _dbus_verbose (
" timeout has not expired\n");
778 _dbus_verbose (
" skipping invocation of disabled timeout\n");
788 for (i = 0; i < n_ready; i++)
792 unsigned int condition;
799 if (initial_serial != loop->callback_list_serial)
802 if (loop->depth != orig_depth)
807 if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
809 cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
813 condition = ready_fds[i].flags;
846 _dbus_watch_set_oom_last_time (watch,
TRUE);
847 loop->oom_watch_pending =
TRUE;
852 _dbus_verbose (
" Invoked watch, oom = %d\n", oom);
859 if (initial_serial != loop->callback_list_serial ||
860 loop->depth != orig_depth)
863 refresh_watches_for_fd (loop,
NULL, ready_fds[i].fd);
871 refresh_watches_for_fd (loop, watches, ready_fds[i].fd);
877 _dbus_verbose (
" moving to next iteration\n");
880 if (_dbus_loop_dispatch (loop))
884 _dbus_verbose (
"Returning %d\n", retval);
891 _dbus_loop_run (DBusLoop *loop)
897 _dbus_loop_ref (loop);
899 our_exit_depth = loop->depth;
902 _dbus_verbose (
"Running main loop, depth %d -> %d\n",
903 loop->depth - 1, loop->depth);
905 while (loop->depth != our_exit_depth)
906 _dbus_loop_iterate (loop,
TRUE);
908 _dbus_loop_unref (loop);
912 _dbus_loop_quit (DBusLoop *loop)
918 _dbus_verbose (
"Quit main loop, depth %d -> %d\n",
919 loop->depth + 1, loop->depth);
923 _dbus_get_oom_wait (
void)
925 #ifdef DBUS_BUILD_TESTS
934 _dbus_wait_for_memory (
void)
936 _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.