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

Iñigo Martínez inigomartinez at gmail.com
Thu Jan 7 12:15:00 PST 2016


What about the second solution ?

Do you think that it is a good idea a thread using sd_bus_wait that
can be cancelled and cleaned later ?

2016-01-07 20:01 GMT+01:00 Lennart Poettering <lennart at poettering.net>:
> 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
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel


More information about the systemd-devel mailing list