From ee9223ff973bc3006da81611fb1cd36850d2fab8 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Thu, 23 Nov 2023 20:10:44 -0500 Subject: Enforce NonZeroUsize on thread count This allows avoiding some if != 0 checks when allocating worker-local datasets. --- compiler/rustc_data_structures/src/sync/worker_local.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'compiler/rustc_data_structures/src/sync') diff --git a/compiler/rustc_data_structures/src/sync/worker_local.rs b/compiler/rustc_data_structures/src/sync/worker_local.rs index ffafdba13ce..b34d3dd9044 100644 --- a/compiler/rustc_data_structures/src/sync/worker_local.rs +++ b/compiler/rustc_data_structures/src/sync/worker_local.rs @@ -1,6 +1,7 @@ use parking_lot::Mutex; use std::cell::Cell; use std::cell::OnceCell; +use std::num::NonZeroUsize; use std::ops::Deref; use std::ptr; use std::sync::Arc; @@ -30,7 +31,7 @@ impl RegistryId { } struct RegistryData { - thread_limit: usize, + thread_limit: NonZeroUsize, threads: Mutex, } @@ -60,7 +61,7 @@ thread_local! { impl Registry { /// Creates a registry which can hold up to `thread_limit` threads. - pub fn new(thread_limit: usize) -> Self { + pub fn new(thread_limit: NonZeroUsize) -> Self { Registry(Arc::new(RegistryData { thread_limit, threads: Mutex::new(0) })) } @@ -73,7 +74,7 @@ impl Registry { /// Panics if the thread limit is hit or if the thread already has an associated registry. pub fn register(&self) { let mut threads = self.0.threads.lock(); - if *threads < self.0.thread_limit { + if *threads < self.0.thread_limit.get() { REGISTRY.with(|registry| { if registry.get().is_some() { drop(threads); @@ -126,7 +127,9 @@ impl WorkerLocal { { let registry = Registry::current(); WorkerLocal { - locals: (0..registry.0.thread_limit).map(|i| CacheAligned(initial(i))).collect(), + locals: (0..registry.0.thread_limit.get()) + .map(|i| CacheAligned(initial(i))) + .collect(), registry, } } -- cgit 1.4.1-3-g733a5