Skip to content

Commit

Permalink
Rollup merge of rust-lang#137996 - safinaskar:2025-03-04-revert-paral…
Browse files Browse the repository at this point in the history
…lel, r=compiler-errors

Revert "compiler/rustc_data_structures/src/sync/worker_local.rs: delete "unsafe impl Sync""

Revert "compiler/rustc_data_structures/src/sync/worker_local.rs: delete "unsafe impl Sync""

This reverts commit 0240690.

See rust-lang#136858 (comment)

cc "Parallel Rustc Front-end" rust-lang#113349

r? SparrowLii

`@rustbot` label: +WG-compiler-parallel
`@rustbot` label: +C-cleanup
  • Loading branch information
workingjubilee authored Mar 4, 2025
2 parents 4e102f8 + c6c4ea4 commit f7b31e9
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions compiler/rustc_data_structures/src/sync/worker_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ pub struct WorkerLocal<T> {
registry: Registry,
}

// This is safe because the `deref` call will return a reference to a `T` unique to each thread
// or it will panic for threads without an associated local. So there isn't a need for `T` to do
// it's own synchronization. The `verify` method on `RegistryId` has an issue where the id
// can be reused, but `WorkerLocal` has a reference to `Registry` which will prevent any reuse.
unsafe impl<T: Send> Sync for WorkerLocal<T> {}

impl<T> WorkerLocal<T> {
/// Creates a new worker local where the `initial` closure computes the
/// value this worker local should take for each thread in the registry.
Expand All @@ -132,11 +138,6 @@ impl<T> Deref for WorkerLocal<T> {
fn deref(&self) -> &T {
// This is safe because `verify` will only return values less than
// `self.registry.thread_limit` which is the size of the `self.locals` array.

// The `deref` call will return a reference to a `T` unique to each thread
// or it will panic for threads without an associated local. So there isn't a need for `T` to do
// it's own synchronization. The `verify` method on `RegistryId` has an issue where the id
// can be reused, but `WorkerLocal` has a reference to `Registry` which will prevent any reuse.
unsafe { &self.locals.get_unchecked(self.registry.id().verify()).0 }
}
}
Expand Down

0 comments on commit f7b31e9

Please sign in to comment.