-
Notifications
You must be signed in to change notification settings - Fork 111
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
Add support for scoped threads #312
base: master
Are you sure you want to change the base?
Conversation
225fe95
to
c181071
Compare
Ping for review? |
@taiki-e pinging you since you approved PR 311, which was in a similar area. Can you TAL or help me find a reviewer? |
Thanks for the PR! It is difficult to implement scoped threads correctly and this implementation is also unsound. Consider the following test case (based on crossbeam-rs/crossbeam#844 (comment)). // tests/thread_api.rs
#[test]
fn scoped_thread_t() {
struct PrintOnDrop<'a>(&'a str);
impl Drop for PrintOnDrop<'_> {
fn drop(&mut self) {
thread::yield_now();
println!("{}", self.0);
}
}
loom::model(|| {
let a = "hello".to_string();
let r = &a;
thread::scope(|s| {
s.spawn(move || PrintOnDrop(r));
});
drop(a);
});
std::thread::sleep(std::time::Duration::from_secs(1));
} thread::scope must wait for all spawned threads to complete, but in the current implementation, thread::scope does not wait for the spawned thread, so the code in the spawned thread accesses the freed variable, or the program itself terminates before the spawned thread completes. $ cargo test --package loom --test thread_api --all-features -- scoped_thread_t --exact --nocapture
|
Add loom::thread::scope to mirror std::thread::scope provided by the standard library.
Fix a bug with thread::scope where the result of a spawned thread was being saved even when the scoped thread's handle had been dropped. This was causing the result to be dropped after the spawned thread was "finished", which was after the point at which the call to thread::scope had already completed. Add a regression test for this behavior.
c181071
to
ae21d3d
Compare
Thank you very much for the review. This is my first serious foray into unsafe Rust, so I'm not at all surprised that what I wrote was unsound! The example actually tickled a bug in the dropping of unused thread results. The spawned thread was completing but its result was being written to memory behind an |
Any chance this PR can be reviewed? It's blocking me adding |
The scope API here uses a lot of the loom internals directly. Would it be possible to move that part of the logic into a method analogous to @notgull The loom crate has very little reviewer bandwidth, but if this will let you use it for |
I don't have time to work on this now, so if someone wants to take over and do the refactor, go for it! |
Add
loom::thread::scope
andloom::thread::Scope
to mirror the API provided by the standard library for implementing scoped threads.Fixes #308