Support for asynchronous parking#517
Open
Wulf0x67E7 wants to merge 14 commits into
Open
Conversation
Collaborator
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Added support for asynchronous parking to parking_lot_core behind a feature flag, and in the process also made all the (un)park-methods reuse their queue manipulation logic, as well as added a future extension trait and methods to util.
Motivation
I was recently playing around with synchronization primitives and sync/async interop. While trying to implement something using parking_lot_core, I wanted to add async support, but realized that parking lot didn't support what I needed for that. I then thought that extending it myself would be a fun and doable project.
Changes
Added feature "async"
Added optional dependency "futures" (couldn't work out how to make it a feature dependent dev-dependency)
Added function
parking_lot_core::park_taskAdded
ImmediateFutureutility trait, struct, and functionsRefactored
parkandpark_taskto reuse shared queue manipulation logicRefactored the various
unparkmethods to also reuse the shared queue manipulation logicExtended the testing tools to also work with async and mixed sync/async operations and made it pass both normally and under miri
Added documentation and comments for the new/refactored code
[025af7e] Added dependency
scopeguard = { version = "1.1.0", default-features = false }Issues
park_taskhas even stricter safety requirements thenpark, namely that the resulting future can not be dropped or forgotten before being unparked or timed out.Because of that, when usingpark_taskto implement e.g. an asynchronous-aware Mutex its unsafety cannot be fully mitigated unless you yourself are the one synchronously polling it to completion, which kinda defeats the point.Notes
park_task, unlike dropping or forgetting,is actually fine and won't even block other things from making progress becauseImmediateFutureensures it cant yield while still holding the queue lock.Thinking about it now, leakingpark_tasksParkDataat the beginning and then only "un-leaking" and dropping it at the end could fix the extra unsafety, but would require allocation and, obviously, risk leaking memory.park_taskpanic safe could also be applied topark.