Skip to content

Commit ee9223f

Browse files
Enforce NonZeroUsize on thread count
This allows avoiding some if != 0 checks when allocating worker-local datasets.
1 parent a4a5c97 commit ee9223f

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

compiler/rustc_data_structures/src/sync/worker_local.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use parking_lot::Mutex;
22
use std::cell::Cell;
33
use std::cell::OnceCell;
4+
use std::num::NonZeroUsize;
45
use std::ops::Deref;
56
use std::ptr;
67
use std::sync::Arc;
@@ -30,7 +31,7 @@ impl RegistryId {
3031
}
3132

3233
struct RegistryData {
33-
thread_limit: usize,
34+
thread_limit: NonZeroUsize,
3435
threads: Mutex<usize>,
3536
}
3637

@@ -60,7 +61,7 @@ thread_local! {
6061

6162
impl Registry {
6263
/// Creates a registry which can hold up to `thread_limit` threads.
63-
pub fn new(thread_limit: usize) -> Self {
64+
pub fn new(thread_limit: NonZeroUsize) -> Self {
6465
Registry(Arc::new(RegistryData { thread_limit, threads: Mutex::new(0) }))
6566
}
6667

@@ -73,7 +74,7 @@ impl Registry {
7374
/// Panics if the thread limit is hit or if the thread already has an associated registry.
7475
pub fn register(&self) {
7576
let mut threads = self.0.threads.lock();
76-
if *threads < self.0.thread_limit {
77+
if *threads < self.0.thread_limit.get() {
7778
REGISTRY.with(|registry| {
7879
if registry.get().is_some() {
7980
drop(threads);
@@ -126,7 +127,9 @@ impl<T> WorkerLocal<T> {
126127
{
127128
let registry = Registry::current();
128129
WorkerLocal {
129-
locals: (0..registry.0.thread_limit).map(|i| CacheAligned(initial(i))).collect(),
130+
locals: (0..registry.0.thread_limit.get())
131+
.map(|i| CacheAligned(initial(i)))
132+
.collect(),
130133
registry,
131134
}
132135
}

compiler/rustc_interface/src/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
107107
use rustc_query_impl::QueryCtxt;
108108
use rustc_query_system::query::{deadlock, QueryContext};
109109

110-
let registry = sync::Registry::new(threads);
110+
let registry = sync::Registry::new(std::num::NonZeroUsize::new(threads).unwrap());
111111

112112
if !sync::is_dyn_thread_safe() {
113113
return run_in_thread_with_globals(edition, || {

0 commit comments

Comments
 (0)