-
Notifications
You must be signed in to change notification settings - Fork 514
Add Parker for thread parking #235
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
Conversation
|
I wonder if it would be worth to enforce the single caller of struct Parker(Unparker(Arc<Inner>));This would require an arc allocation, but compared to the mutex / condvar, perhaps that isn't too bad? disclaimer: I don't know if this is a good idea. I'm also working on trying to validate this w/ syncbox. |
|
@carllerche My thinking is that if someone needs this synchronization primitive, they will usually care more about optimization opportunities than how bulletproof the interface is. As such, it might be better to leave the choice of allocation vs no allocation to them. For example, in Tokio, we'll have Having just a single |
|
@stjepang seems good to me. We can try to remove SeqCst later. |
|
I'll have a closer look today night. Thanks! |
|
@jeehoonkang Note: there's no need to review the actual implementation since it's just a copy-paste from |
|
I would like to use this :) |
|
One question is: should this use In general, it would be nice for |
|
I think it's in good shape, but I think the |
I doubt By the way, folks over here at RustFest are preparing a PR that moves
Ah, I guess you're right. There's probably no point in overoptimizing this if there won't be any measurable difference. I've split the interface into |
|
Ok, let's merge this so that @carllerche can start using it. bors r+ |
235: Add Parker for thread parking r=stjepang a=stjepang This is just an extracted copy of the current implementation of `thread::park()` and `thread::unpark()`. `Parker` is a low-level thread synchronization primitive useful for building others (like locks etc.). It would be useful in tokio-rs/tokio#528 and might remove some cruft and unnecessary TLS access from `context.rs` in `crossbeam-channel`. I've also added a fast-path check for `timeout == Duration::from_secs(0)`, which Tokio relies on. An interesting peculiarity of `Parker` is that it's not split into `Parker` and `Unparker`, which means any thread can call `park()` at any time. However, if multiple threads call `park()` at the same time, expect deadlocks or panics - that is the user's problem. Splitting the primitive into an owned `Parker` and shared `Unparker` would require us to wrap the inner structure into an `Arc`, which comes at a cost of allocation and indirection. Since this is a very low-level primitive, I chose not to do that. The reason why this is in `crossbeam-utils` is because it's a really simple primitive with no dependencies. Also, `tokio-executor` and `tokio-threadpool` really don't want to pull in the whole `crossbeam` in order to use this. cc @carllerche Co-authored-by: Stjepan Glavina <[email protected]>
Build succeeded |
This is just an extracted copy of the current implementation of
thread::park()andthread::unpark().Parkeris a low-level thread synchronization primitive useful for building others (like locks etc.). It would be useful in tokio-rs/tokio#528 and might remove some cruft and unnecessary TLS access fromcontext.rsincrossbeam-channel.I've also added a fast-path check for
timeout == Duration::from_secs(0), which Tokio relies on.An interesting peculiarity of
Parkeris that it's not split intoParkerandUnparker, which means any thread can callpark()at any time. However, if multiple threads callpark()at the same time, expect deadlocks or panics - that is the user's problem. Splitting the primitive into an ownedParkerand sharedUnparkerwould require us to wrap the inner structure into anArc, which comes at a cost of allocation and indirection. Since this is a very low-level primitive, I chose not to do that.The reason why this is in
crossbeam-utilsis because it's a really simple primitive with no dependencies. Also,tokio-executorandtokio-threadpoolreally don't want to pull in the wholecrossbeamin order to use this.cc @carllerche