facebookexperimental / facebookexperimental/libunifex

Is there something equivalent to thread-local storage, but for coroutines?

Open
#597 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
1.7k
Forks
210
PR merge metrics
No merged PRs in 30d

Description

Let's call it something like `LocalContext` for now. And let's make the simplification we're dealing with a `unifex::task`.

The basic requirements I am looking for are:

1. When 2 unrelated tasks interleave on the same executor, their `LocalContext`s are isolated from one another.
2. When there's a parent-child relationship between 2 tasks (e.g. taskA `co_await`s on `taskB`), taskA's context is propagated onto `taskB`'s contexts.
3. `LocalContext`s resources are managed by the containing tasks (i.e. they get destroyed when the underlying coroutine gets destroyed).

Python has [ContextVars](https://docs.python.org/3/library/contextvars.html) that is more or less what I describe above. It has the added advantage that it works also in cases where you need plain thread local storage.

I did a little bit of search some time ago, but didn't see anything in the works or being proposed.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.