-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Threadsafe futures #1514
Threadsafe futures #1514
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the general structure that this is going to want to take is that spawning a future creates two main allocations. One is a Rc
(or something like that) which holds the future itself. Another is an Arc
which has some shared state. When creating a waker the Arc
is what's shared, and its notify
implementation will probably look like:
if !self.notified.swap(true) {
// run an atomic notify at the address of `&self.notified`
}
In the poll loop I think it would look roughly like:
let promise = wait_async_on(&self.notified);
while self.notified.swap(true) {
// do the poll
}
promise.then(&poll_again);
Does it mean that the state with |
Oh what I gisted was just a straw man, it may be that two bits are still needed rather than one for information exchange. |
@alexcrichton the pr is not tested yet, I pushed changes to check the design. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks along the right lines yeah! There's probably a lot of devils in the details, but I think the general direction here is where we want to go.
@alexcrichton I think I hit the wall here: #1525 |
Yeah it may be some time before snippets work on Node, so it's probably best to not block this on that. Perhaps the polyfill could be implemented in Rust though? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some great progress!
@alexcrichton thank you for a great review! I fixed most of the places, but still have some questions, like usage of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is some comments on the polyfill at least, I'll try to get to the futures implementation later this week!
@alexcrichton I fixed most of the places, but still have some where I'm not sure, like what to do with closures, and the contents of the struct Waker {
value: AtomicI32,
notified: AtomicBool,
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delays, travel got the best of me! But yes the notified
field can be removed and just value
needs to be there, and we can use flags in value
to indicate whether polling needs to happen and such.
Updated to the latest master (I hope I didn't miss anything). Removed |
Not implemented yet, and the one there doesn't work with atomics! (we'll get around to this soon-ish)
Use the atomics support now implemented!
* Remove now-unneeded `State` enum * Remove timeout argument from polyfill since we don't need it * Call `Atomics.waitAsync` if it's available instead of using our polyfill * Remove some extraneous dead code from the polyfill * Add a `val: i32` argument to the polyfill * Simplify the flow of futures with `Package` since `waitAsync` handles all the heavy lifting for us. * Remove `Arc<Package>` and just use `Package` * Remove `RefCell` from inside of `Package` now that it is no longer needed.
Turns out it's the exact same for both before and after atomics, so let's use the same definition!
* Use "legacy" instead of "stable" since `futures 0.1` is quicly becoming "legacy" * Rename "atomics" to "legacy_atomics" to leave room for the libstd-based futures atomics version. * Rename "polyfill" to "wait_async_polyfill" to specify what it's polyfilling.
Ok thanks for the update @ibaryshnikov! I've added a few more commits which cleaned up a few things (good chunks of the old If CI is green and you're good with this I think it's good to merge! |
@alexcrichton I'm good with this. Thanks for moving this forvard! And thanks for your comments, I've learned a lot from this pr!) |
Ok let's merge then, thanks so much again @ibaryshnikov! |
I think I found a usecase which is incomatible with this PR: #3048 |
Closes #1379
Changes:
wait_async
, which does the same asAtomics::waitAsync
impl Notify for Package
, also removedSend + Sync
markers forPackage
Waker
struct instead, which implementsNotify
Waker
instead ofPackage
to notify the executor