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);
 
  260          !_dbus_watch_get_oom_last_time (link->
data))
 
  268    _dbus_pollable_set_enable (loop->pollable_set, fd, flags);
 
  270    _dbus_pollable_set_disable (loop->pollable_set, fd);
 
  274_dbus_loop_add_watch (DBusLoop  *loop,
 
  280  fd = _dbus_watch_get_pollable (watch);
 
  283  watches = ensure_watch_table_entry (loop, fd);
 
  291      gc_watch_table_entry (loop, watches, fd);
 
  298      if (!_dbus_pollable_set_add (loop->pollable_set, fd,
 
  302          _dbus_hash_table_remove_pollable (loop->watches, fd);
 
  309      refresh_watches_for_fd (loop, watches, fd);
 
  312  loop->callback_list_serial += 1;
 
  313  loop->watch_count += 1;
 
  318_dbus_loop_toggle_watch (DBusLoop          *loop,
 
  321  refresh_watches_for_fd (loop, 
NULL, _dbus_watch_get_pollable (watch));
 
  325_dbus_loop_remove_watch (DBusLoop         *loop,
 
  335  fd = _dbus_watch_get_pollable (watch);
 
  338  watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
 
  351              loop->callback_list_serial += 1;
 
  352              loop->watch_count -= 1;
 
  357              if (gc_watch_table_entry (loop, watches, fd))
 
  359                  _dbus_pollable_set_remove (loop->pollable_set, fd);
 
  369  _dbus_warn (
"could not find watch %p to remove", watch);
 
  373_dbus_loop_add_timeout (DBusLoop           *loop,
 
  376  TimeoutCallback *tcb;
 
  378  tcb = timeout_callback_new (timeout);
 
  384      loop->callback_list_serial += 1;
 
  385      loop->timeout_count += 1;
 
  389      timeout_callback_free (tcb);
 
  397_dbus_loop_remove_timeout (DBusLoop           *loop,
 
  406      TimeoutCallback *
this = link->
data;
 
  408      if (this->timeout == timeout)
 
  411          loop->callback_list_serial += 1;
 
  412          loop->timeout_count -= 1;
 
  413          timeout_callback_free (
this);
 
  421  _dbus_warn (
"could not find timeout %p to remove", timeout);
 
  430               TimeoutCallback *tcb,
 
  436  long expiration_tv_usec;
 
  438  long interval_milliseconds;
 
  445  interval_seconds = interval / 1000L;
 
  446  interval_milliseconds = interval % 1000L;
 
  448  expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
 
  449  expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
 
  450  if (expiration_tv_usec >= 1000000)
 
  452      expiration_tv_usec -= 1000000;
 
  453      expiration_tv_sec += 1;
 
  456  sec_remaining = expiration_tv_sec - tv_sec;
 
  457  msec_remaining = (expiration_tv_usec - tv_usec) / 1000L;
 
  460  _dbus_verbose (
"Interval is %ld seconds %ld msecs\n",
 
  462                 interval_milliseconds);
 
  463  _dbus_verbose (
"Now is  %lu seconds %lu usecs\n",
 
  465  _dbus_verbose (
"Last is %lu seconds %lu usecs\n",
 
  466                 tcb->last_tv_sec, tcb->last_tv_usec);
 
  467  _dbus_verbose (
"Exp is  %lu seconds %lu usecs\n",
 
  468                 expiration_tv_sec, expiration_tv_usec);
 
  469  _dbus_verbose (
"Pre-correction, sec_remaining %ld msec_remaining %ld\n",
 
  470                 sec_remaining, msec_remaining);
 
  477  if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
 
  483      if (msec_remaining < 0)
 
  485          msec_remaining += 1000;
 
  493        *timeout = sec_remaining * 1000 + msec_remaining;        
 
  496  if (*timeout > interval)
 
  499      _dbus_verbose (
"System clock set backward! Resetting timeout.\n");
 
  501      tcb->last_tv_sec = tv_sec;
 
  502      tcb->last_tv_usec = tv_usec;
 
  508  _dbus_verbose (
"  timeout expires in %d milliseconds\n", *timeout);
 
  511  return *timeout == 0;
 
  515_dbus_loop_dispatch (DBusLoop *loop)
 
  522  if (loop->need_dispatch == 
NULL)
 
  526  while (loop->need_dispatch != 
NULL)
 
  544                _dbus_wait_for_memory ();
 
  553_dbus_loop_queue_dispatch (DBusLoop       *loop,
 
  570min_poll_timeout (
int a,
 
  574    return a < 0 ? b : a;
 
  576    return b < 0 ? a : b;
 
  584_dbus_loop_iterate (DBusLoop     *loop,
 
  587#define N_STACK_DESCRIPTORS 64 
  589  DBusPollableEvent ready_fds[N_STACK_DESCRIPTORS];
 
  599  orig_depth = loop->depth;
 
  602  _dbus_verbose (
"Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
 
  603                 block, loop->depth, loop->timeout_count, loop->watch_count);
 
  607      loop->timeouts == 
NULL)
 
  611  if (loop->timeout_count > 0)
 
  622          TimeoutCallback *tcb = link->
data;
 
  630                  tcb->last_tv_sec = tv_sec;
 
  631                  tcb->last_tv_usec = tv_usec;
 
  635              check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
 
  637              timeout = min_poll_timeout (msecs_remaining, timeout);
 
  640              _dbus_verbose (
"  timeout added, %d remaining, aggregate timeout %ld\n",
 
  641                             msecs_remaining, timeout);
 
  649              _dbus_verbose (
"  skipping disabled timeout\n");
 
  658  if (!block || loop->need_dispatch != 
NULL)
 
  662      _dbus_verbose (
"  timeout is 0 as we aren't blocking\n");
 
  669  if (loop->oom_watch_pending)
 
  670    timeout = min_poll_timeout (timeout, _dbus_get_oom_wait ());
 
  673  _dbus_verbose (
"  polling on %d descriptors timeout %ld\n", 
_DBUS_N_ELEMENTS (ready_fds), timeout);
 
  676  n_ready = _dbus_pollable_set_poll (loop->pollable_set, ready_fds,
 
  680  if (loop->oom_watch_pending)
 
  684      loop->oom_watch_pending = 
FALSE;
 
  695          fd = _dbus_hash_iter_get_pollable_key (&hash_iter);
 
  704              if (_dbus_watch_get_oom_last_time (watch))
 
  706                  _dbus_watch_set_oom_last_time (watch, 
FALSE);
 
  712            refresh_watches_for_fd (loop, watches, fd);
 
  719  initial_serial = loop->callback_list_serial;
 
  721  if (loop->timeout_count > 0)
 
  733          TimeoutCallback *tcb = link->
data;
 
  735          if (initial_serial != loop->callback_list_serial)
 
  738          if (loop->depth != orig_depth)
 
  745              if (check_timeout (tv_sec, tv_usec,
 
  746                                 tcb, &msecs_remaining))
 
  749                  tcb->last_tv_sec = tv_sec;
 
  750                  tcb->last_tv_usec = tv_usec;
 
  753                  _dbus_verbose (
"  invoking timeout\n");
 
  766                  _dbus_verbose (
"  timeout has not expired\n");
 
  773              _dbus_verbose (
"  skipping invocation of disabled timeout\n");
 
  783      for (i = 0; i < n_ready; i++)
 
  787          unsigned int condition;
 
  794          if (initial_serial != loop->callback_list_serial)
 
  797          if (loop->depth != orig_depth)
 
  802          if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
 
  804              cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
 
  808          condition = ready_fds[i].flags;
 
  817          watches = _dbus_hash_table_lookup_pollable (loop->watches,
 
  841                      _dbus_watch_set_oom_last_time (watch, 
TRUE);
 
  842                      loop->oom_watch_pending = 
TRUE;
 
  847                  _dbus_verbose (
"  Invoked watch, oom = %d\n", oom);
 
  854                  if (initial_serial != loop->callback_list_serial ||
 
  855                      loop->depth != orig_depth)
 
  858                        refresh_watches_for_fd (loop, 
NULL, ready_fds[i].fd);
 
  866            refresh_watches_for_fd (loop, watches, ready_fds[i].fd);
 
  872  _dbus_verbose (
"  moving to next iteration\n");
 
  875  if (_dbus_loop_dispatch (loop))
 
  879  _dbus_verbose (
"Returning %d\n", retval);
 
  886_dbus_loop_run (DBusLoop *loop)
 
  892  _dbus_loop_ref (loop);
 
  894  our_exit_depth = loop->depth;
 
  897  _dbus_verbose (
"Running main loop, depth %d -> %d\n",
 
  898                 loop->depth - 1, loop->depth);
 
  900  while (loop->depth != our_exit_depth)
 
  901    _dbus_loop_iterate (loop, 
TRUE);
 
  903  _dbus_loop_unref (loop);
 
  907_dbus_loop_quit (DBusLoop *loop)
 
  913  _dbus_verbose (
"Quit main loop, depth %d -> %d\n",
 
  914                 loop->depth + 1, loop->depth);
 
  918_dbus_get_oom_wait (
void)
 
  920#ifdef DBUS_ENABLE_EMBEDDED_TESTS 
  929_dbus_wait_for_memory (
void)
 
  931  _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.