-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix an oversight in
openNursery()
implementation.
There is currently another oversight in recommended way of establishing a nursery for background operations of active objects. In this code snippet: class My { corral::Nursery* nursery_; corral::Task<void> doThing() { co_await corral::noncancellable(something); nursery_->start(somethingElseInBackground()); } public: corral::Task<void> run() { CORRAL_WITH_NURSERY(n) { nursery_ = &n; ScopeGuard guard([&]{ nursery_ = nullptr; }); CORRAL_SUSPEND_FOREVER(); }; } void beginThing() { nursery_->start(&My::doThing, this); } }; — `My::nursery_` would get zeroed out upon cancellation request, before the nursery and all tasks therein are actually cancelled. All tasks in the nursery will get cancelled as well, but if a task was busy doing any noncancellable stuff, it would resume normally. Any attempt to access a nursery thereafter would result in a segfault, since the nursery is already zeroed out. This breaks reasoning: it feels natural to assume that, if a task is running in a nursery, the nursery is available for submitting more tasks in there. Worse, even if we announced very loudly that the above assumption does not in fact hold, that would not provide much of any alternative for users to dispatch the piece of work they were going to submit there (`if (nursery_) nursery_->start(...); else /*what?*/`). This diff addresses this by adding another nursery flavor, `detail::BackreferencedNursery`, which holds a backreference to itself, and can zero it out when it's about to close. To prevent any potential footguns, such a nursery can only be constructed by `openNursery()` function. To simplify `openNursery()` implementation, `UnsafeNursery::Awaitable` has been moved to `Nursery` class, and templatized by nursery type, so it can handle both `UnsafeNursery` and `BackreferencedNursery`.
- Loading branch information
1 parent
8e103fa
commit 0db0585
Showing
4 changed files
with
109 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters