Need help understanding dbus-glib event loop

Simon McVittie simon.mcvittie at collabora.co.uk
Tue Jan 19 12:41:02 PST 2010


On Tue, 19 Jan 2010 at 11:38:18 -0800, Bruce, Henry wrote:
> I'm using dbus-glib to add IPC support to an SDK. All is working well,
> but I've found that signals only get through to the client if it is
> running a g_main_loop. It seems as if the signals need an event pump
> and the main loop is providing that service.

You need some sort of event loop in order to do asynchronous IPC. dbus-glib
is a GLib library, like Gtk, so it uses the GLib main loop by default.

> Is there any way that I can receive signals without a g_main_loop? I'd
> like to avoid forcing clients to run a main_loop. I could hide a main_loop
> in my C++ binding but this seems icky.

Your clients will have to run some sort of event loop, and it'll have to be
whichever one the DBusGConnection or DBusConnection has been set up to use
(dbus_bus_get() automatically sets up that bus connection to use the GLib
main loop).

You could do what dbus-glib and QtDBus do: use libdbus directly, and provide
DBusWatch/DBusTimeout implementations to hook a DBusConnection into whichever
event loop you've chosen (or potentially, whichever event loop the application
developer has chosen).

Avahi might interest you, since it contains several examples of libdbus
main-loop glue for different event loops, including one which polls in a
thread.

I wouldn't recommend running dbus-glib in a thread (if that's what you're
thinking of doing) unless you're able to audit it for thread-safety and
provide patches where necessary; in theory it's meant to be thread-safe, but
in practice it probably isn't.

libdbus, the lower-level library used by dbus-glib, QtDBus etc., is likely to
be more thread-safe (various people have contributed thread-safety fixes to it,
I believe).

Regards,
    Simon


More information about the dbus mailing list