Skip to content

Commit 0b7cdf2

Browse files
committed
Address review comments
1 parent 58830de commit 0b7cdf2

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
@@ -1305,20 +1305,17 @@ impl ThreadId {
13051305
spin += 1;
13061306
}
13071307

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

0 commit comments

Comments
 (0)