Tighten Send and Sync requirements#443
Conversation
rayon-core/src/scope/mod.rs
Outdated
| // the `Scope` is not send or sync, and we only give out | ||
| // pointers to it from within a worker thread | ||
| debug_assert!(!WorkerThread::current().is_null()); | ||
| debug_assert!(!worker_thread.is_null()); |
There was a problem hiding this comment.
Note, Scope now does implement Sync, so this comment isn't true anymore. We need to evaluate whether that's actually safe. I think here, this !is_null() should probably be a full assert!, and perhaps we assert that the current registry matches that saved in the Scope too.
rayon-core/src/scope/mod.rs
Outdated
| debug_assert!(!worker_thread.is_null()); | ||
|
|
||
| let worker_thread = &*worker_thread; | ||
| worker_thread.push(job_ref); |
There was a problem hiding this comment.
Maybe this can just switch to scope.registry.inject_or_push(job_ref), so if the scope does get carried out of the threadpool, it still spawns back to the right pool. The 'scope lifetime should still ensure the right stuff...
|
|
||
| let worker_thread = &*worker_thread; | ||
| worker_thread.push(job_ref); | ||
| self.registry.inject_or_push(job_ref); |
There was a problem hiding this comment.
Let's add a comment here saying that it would be unsafe to just invoke push before we don't know that the scope was not somehow sent to another thread.
nikomatsakis
left a comment
There was a problem hiding this comment.
Left a nit with a minor documentation change, but otherwise looks good.
This is a **breaking change** to `rayon-core`, necessary for soundness. - `Scope::spawn` now requires `Send` for the closure. Fixes rayon-rs#430. - `ThreadPool::install` now requires `Send` for the return value. - (unstable) `internal::Task` now requires `Send + Sync` instead of just `Send`, so its use of `Arc` properly implements `Send` and `Sync` too.
This adds `Registry::in_worker()`, akin to the free `fn in_worker()` but for a particular thread pool. If this is called from a thread that's part of that pool, it just executes the `op` directly. `ThreadPool::install()` now simply forwards to `Registry::in_worker()`. Fixes rayon-rs#446.
|
Added a fix for #446, since that also needed the change to |
This is a breaking change to
rayon-core, necessary for soundness.Scope::spawnnow requiresSendfor the closure. Fixes Scope::spawn doesn't requireSend#430.ThreadPool::installnow requiresSendfor the return value.(unstable)
internal::Tasknow requiresSend + Syncinstead of justSend, so its use ofArcproperly implementsSendandSynctoo.