libdbus - simple mainloop?
Havoc Pennington
hp at pobox.com
Wed Jun 16 12:37:28 PDT 2010
Hi,
On Wed, Jun 16, 2010 at 5:23 AM, Thomas Themel <thomas at themel.com> wrote:
> I tried calling dbus_connection_flush() after the send, but that hangs,
> probably because the main loop in another thread is holding the lock on the
> connection.
Not clear what you mean here. Is all your usage of libdbus from a
single thread, or not? If not then what is in each thread?
read_write_dispatch should write out the signal, but it won't if you
send the signal from a different thread because read_write_dispatch()
won't know to wake up from poll() to send the signal. So you'd need to
send the signal from your read_write_dispatch() thread.
With a main loop instead of read_write_dispatch() the "wakeup main
function" which can be set on DBusConnection is used to solve this
problem. However, plain read_write_dispatch() is not that clever.
Your simplest approach might be to use a simple embeddable main loop
like libev. libev is just one file that you #include and can ship with
your app.
If you wanted to fix libdbus to improve this, you could make
_dbus_connection_wakeup_mainloop() in dbus-connection.c also wake up
read_write_dispatch(). However, I'm not sure how DBusConnection would
know whether it should expect a main loop or expect
read_write_dispatch() and so it's unclear which it would want to wake
up. Wakeup is normally implemented with a pipe (poll() has to include
read end of the pipe, to wakeup you write a byte to the pipe). On
Linux you could also use the unportable eventfd() with some efficiency
win. Maybe it would be fine to wakeup read_write_dispatch if no wakeup
main function is set, otherwise use the function, not sure.
Another possible approach is to read_write_dispatch() with a short
timeout and then you would be busy looping a bit but you'd send the
signals with latency = the timeout length.
Havoc
More information about the dbus
mailing list