Skip to content

Commit aa7fcec

Browse files
committed
Address review comments
1 parent 970a62b commit aa7fcec

File tree

3 files changed

+18
-29
lines changed

3 files changed

+18
-29
lines changed

library/std/src/sys/thread_local/destructors/list.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ static DTORS: RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut u8)), System>> =
77
RefCell::new(Vec::new_in(System));
88

99
pub unsafe fn register(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
10-
let Ok(mut dtors) = DTORS.try_borrow_mut() else { rtabort!("unreachable") };
10+
let Ok(mut dtors) = DTORS.try_borrow_mut() else {
11+
rtabort!("the System allocator may not use TLS with destructors")
12+
};
1113
guard::enable();
1214
dtors.push((t, dtor));
1315
}

library/std/src/thread/current.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,13 @@ fn init_current(current: *mut ()) -> Thread {
269269
// BUSY exists solely for this check, but as it is in the slow path, the
270270
// extra TLS write above shouldn't matter. The alternative is nearly always
271271
// a stack overflow.
272-
273-
// If you came across this message, contact the author of your
274-
// allocator. If you are said author: A surprising amount of functions
275-
// inside the standard library (e.g. `Mutex`, `File` when using long
276-
// paths, even `panic!` when using unwinding), need memory allocation,
277-
// so you'll get circular dependencies all over the place when using
278-
// them. I (joboet) highly recommend using only APIs from core in your
279-
// allocator and implementing your own system abstractions. Still, if
280-
// you feel that a particular API should be entirely allocation-free,
281-
// feel free to open an issue on the Rust repository, we'll see what we
282-
// can do.
272+
//
273+
// If we reach this point it means our initialization routine ended up
274+
// calling current() either directly, or indirectly through the global
275+
// allocator, which is a bug either way as we may not call the global
276+
// allocator in current().
283277
rtabort!(
284-
"\n\
285-
Attempted to access thread-local data while allocating said data.\n\
286-
Do not access functions that allocate in the global allocator!\n\
287-
This is a bug in the global allocator.\n\
288-
"
278+
"init_current() was re-entrant, which indicates a bug in the Rust threading implementation"
289279
)
290280
} else {
291281
debug_assert_eq!(current, DESTROYED);

library/std/src/thread/mod.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,20 +1306,17 @@ impl ThreadId {
13061306
spin += 1;
13071307
}
13081308

1309-
let id;
13101309
// SAFETY: we have an exclusive lock on the counter.
13111310
unsafe {
1312-
id = (*COUNTER.get()).saturating_add(1);
1313-
(*COUNTER.get()) = id;
1314-
};
1315-
1316-
// Release the lock.
1317-
COUNTER_LOCKED.store(false, Ordering::Release);
1318-
1319-
if id == u64::MAX {
1320-
exhausted()
1321-
} else {
1322-
ThreadId(NonZero::new(id).unwrap())
1311+
if *COUNTER.get() == u64::MAX {
1312+
COUNTER_LOCKED.store(false, Ordering::Release);
1313+
exhausted()
1314+
} else {
1315+
let id = *COUNTER.get() + 1;
1316+
*COUNTER.get() = id;
1317+
COUNTER_LOCKED.store(false, Ordering::Release);
1318+
ThreadId(NonZero::new(id).unwrap())
1319+
}
13231320
}
13241321
}
13251322
}

0 commit comments

Comments
 (0)