[systemd-devel] How to unlock a blocking sd_bus_wait() call

Lennart Poettering lennart at poettering.net
Thu Jan 7 11:01:04 PST 2016


On Thu, 07.01.16 14:49, Federico Di Pierro (nierro92 at gmail.com) wrote:

> Hi!
> I'm trying to understand if there's a way to properly unlock
> a sd_bus_wait() blocking call, as i need the thread where the call is
> performed to leave.
> The cycle is something like:
> 
> while (!quit) {
>          r = sd_bus_process(signal_bus, NULL);
>          if (r > 0) {
>                continue;
>          }
>          r = sd_bus_wait(signal_bus, (uint64_t) -1);
>          if (r < 0) {
>                 break;
>          }
>    }
> close_bus();
> pthread_exit(NULL);
> 
> The quit status is changed elsewhere.
> Is there a way to achieve this thing?

If you use sd_bus_wait() then you can only wait for bus events or a
timeout, nothing else. If you want to wait for other events too, then
don't use sd_bus_wait(). Instead, simply use poll()/ppoll()/epoll() or
something like that and use sd_bus_get_fd(), sd_bus_get_events(),
sd_bus_get_timeout() to figure out what sd-bus wants you to wait for.

Specifically, something like this should work:

        struct pollfd p;
        uint64_t usec;
        int r;

        p = (struct pollfd) {
                .fd = sd_bus_get_fd(bus),
                .events = sd_bus_get_events(bus),
        };

        sd_bus_get_timeout(bus, &usec);

        poll(1, &p, usec == (uint64_t) -1 ? -1 : (usec+999)/1000);

Of course, you should add more error checking.      

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list