watches versus sfds

Simon McVittie smcv at collabora.com
Tue Apr 3 11:57:30 UTC 2018


On Sun, 01 Apr 2018 at 14:56:49 -0700, Matt Wette wrote:
> I am writing a mainloop for Guile based on libdbus and epoll.

You might find these useful references:

* dbus/dbus-mainloop.c and dbus/dbus-socket-set-epoll.c in libdbus
  (a simple non-thread-safe main loop used by dbus-daemon; there is
  also a plain poll() backend)

* the dbus-gmain branch in the dbus-glib git repository
  (main loop glue to attach libdbus to GLib, now available as a
  subtree/submodule to decouple it from the rest of dbus-glib;
  intended to be thread-safe)

In particular, if you will ever support multi-threading, then you will
need to implement all the callbacks that dbus-gmain does, including
making a call to dbus_connection_set_wakeup_main_function().

Beware of threads. As far as we are aware, libdbus is thread-safe;
but its maintainers thought it was already thread-safe more than 10
years ago, and we've fixed thread-safety bugs as recently as this year,
so it seems unlikely that it is really completely thread-safe. Making
your own code thread-safe is also going to be a lot of work. My rule of
thumb is that if you can't point at each function, each global variable
and each struct member in turn, and say "this runs in the Foo thread" or
"this is protected by the Foobar lock" without having to think about it,
then your code is unlikely to be thread-safe.

> I have a procedure for adding watches.  Is there a limit on how many watches
> are assocated
> with a specific (unix) fd?

We don't guarantee any particular limit, and they can be toggled (enabled
and disabled) independently.

In practice there will not be very many, so don't put effort into making
your data structure scale up to very large numbers of watches per fd.
DBusLoop uses a hash table { fd => linked list of Watch* }.

(Implementation detail: There are currently no more than 2 watches per fd,
and usually exactly 2, but please don't rely on this; in principle we
could have more.)

    smcv


More information about the dbus mailing list