boostorg / leaf

Lightweight Error Augmentation Framework
Boost Software License 1.0
302 stars 48 forks source link

Coroutines support #5

Closed nicber closed 4 years ago

nicber commented 4 years ago

Thank you for building this library. I'm using it for embedded development and I'm very happy so far.

One question that has come up is about the support for coroutines. I know that leaf uses thread_local storage for its magic. What would happen if a coroutine is suspended and another takes its place, maybe even moving the first coroutine to another thread?

zajo commented 4 years ago

The error objects are stored on the stack in a leaf::context<E...>. When activate() is called, TL pointers of each of the E... types are pointed to the objects stored in the context. This effectively binds the context to the calling thread until deactivate() is called. If threads are switched while a context object is active, bad things will happen.

That said, the reason why the context type is part of the public interface is so that error objects can be transported between threads. See https://zajo.github.io/leaf/#tutorial-async.

nicber commented 4 years ago

So, if I understand correctly, everything should work fine as long as no coroutine switches threads.

I imagine that supporting that would be difficult considering the library's design.

zajo commented 4 years ago

If the thread doesn't change, obviously there's no problem. If it may, it might be possible to ensure that the relevant leaf::context objects are deactivated, but yes, this would work better with threads than with coroutines. Reopen the issue if you have more questions.