Skip to content

Commit 642e960

Browse files
committed
Address review comments
1 parent 643451e commit 642e960

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
@@ -249,23 +249,13 @@ fn init_current(current: *mut ()) -> Thread {
249249
// BUSY exists solely for this check, but as it is in the slow path, the
250250
// extra TLS write above shouldn't matter. The alternative is nearly always
251251
// a stack overflow.
252-
253-
// If you came across this message, contact the author of your
254-
// allocator. If you are said author: A surprising amount of functions
255-
// inside the standard library (e.g. `Mutex`, `File` when using long
256-
// paths, even `panic!` when using unwinding), need memory allocation,
257-
// so you'll get circular dependencies all over the place when using
258-
// them. I (joboet) highly recommend using only APIs from core in your
259-
// allocator and implementing your own system abstractions. Still, if
260-
// you feel that a particular API should be entirely allocation-free,
261-
// feel free to open an issue on the Rust repository, we'll see what we
262-
// can do.
252+
//
253+
// If we reach this point it means our initialization routine ended up
254+
// calling current() either directly, or indirectly through the global
255+
// allocator, which is a bug either way as we may not call the global
256+
// allocator in current().
263257
rtabort!(
264-
"\n\
265-
Attempted to access thread-local data while allocating said data.\n\
266-
Do not access functions that allocate in the global allocator!\n\
267-
This is a bug in the global allocator.\n\
268-
"
258+
"init_current() was re-entrant, which indicates a bug in the Rust threading implementation"
269259
)
270260
} else {
271261
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
@@ -1303,20 +1303,17 @@ impl ThreadId {
13031303
spin += 1;
13041304
}
13051305

1306-
let id;
13071306
// SAFETY: we have an exclusive lock on the counter.
13081307
unsafe {
1309-
id = (*COUNTER.get()).saturating_add(1);
1310-
(*COUNTER.get()) = id;
1311-
};
1312-
1313-
// Release the lock.
1314-
COUNTER_LOCKED.store(false, Ordering::Release);
1315-
1316-
if id == u64::MAX {
1317-
exhausted()
1318-
} else {
1319-
ThreadId(NonZero::new(id).unwrap())
1308+
if *COUNTER.get() == u64::MAX {
1309+
COUNTER_LOCKED.store(false, Ordering::Release);
1310+
exhausted()
1311+
} else {
1312+
let id = *COUNTER.get() + 1;
1313+
*COUNTER.get() = id;
1314+
COUNTER_LOCKED.store(false, Ordering::Release);
1315+
ThreadId(NonZero::new(id).unwrap())
1316+
}
13201317
}
13211318
}
13221319
}

0 commit comments

Comments
 (0)