C++ co-routines are coming soon...

Patrick Williams patrick at stwcx.xyz
Mon Nov 29 08:22:57 AEDT 2021


Hello,

I just pushed up some code I've been working on for initial community preview:
C++20 coroutine support in sdbusplus[1].  This code should be considered
"experimental" for the time-being but a good preview of where I plan to take the
sdbusplus bindings.  I know there are complaints about the current sdbusplus
support in one of three veins depending on your perspective of importance:

    1. The current sdbusplus server bindings in phosphor-dbus-interfaces are
       synchronous.
    2. There are no sdbusplus client bindings in phosphor-dbus-interfaces.
    3. The code using the sdbusplus Boost/ASIO interfaces is very callback
       intensive (and difficult to read).

I've been wanting to leverage C++20 coroutines for quite a while in order to
address these shortcomings but haven't gotten around to it due to lack of
understanding on my part (and lack of time to fix this) and lack of maturity in
the library support.  There is a proposal for C++23 (P2300)[2] which attempts to
fix the "maturity" problem and the authors of that have been using libunifex[3]
as their proving ground.  Using libunifex and becoming enlightened by Eric
Niebler's excellent talk at CppCon'21, I've finally been able to make some good
progress on this.

There will be more to come, but I wanted to give a taste of what a C++20
co-routine-based sdbusplus agent might look like.  This is an example of a
task that reacts to D-Bus "NameOwnerChanged" signals:

```
auto watch_events(sdbusplus::async::context_t& ctx)
    -> sdbusplus::execution::task<void>
{
    using namespace sdbusplus::async::match;
    auto m = match(ctx, rules::nameOwnerChanged());

    while (auto msg = co_await m.next())
    {
        std::string service, old_name, new_name;
        msg.read(service, old_name, new_name);
        if (!new_name.empty())
        {
            std::cout << new_name << " owns " << service << std::endl;
        }
        else
        {
            std::cout << service << " released" << std::endl;
        }
    };

    co_return;
}
```

I currently have `match` implemented and I plan to get `call` working very soon.
Sometime between now and late January I'll probably do an educational talk on
co-routines.

1. https://gerrit.openbmc-project.xyz/c/openbmc/sdbusplus/+/49117
2. https://github.com/cplusplus/papers/issues/1054
3. https://github.com/facebookexperimental/libunifex
4. https://www.youtube.com/watch?v=xLboNIf7BTg

-- 
Patrick Williams
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/openbmc/attachments/20211128/1d38faf0/attachment.sig>


More information about the openbmc mailing list