@@ -1286,6 +1286,18 @@ mod thread_name_string {
1286
1286
1287
1287
use thread_name_string:: ThreadNameString ;
1288
1288
1289
+ /// Store the ID of the main thread.
1290
+ ///
1291
+ /// The thread handle for the main thread is created lazily, and this might even
1292
+ /// happen pre-main. Since not every platform has a way to identify the main
1293
+ /// thread when that happens – macOS's `pthread_main_np` function being a notable
1294
+ /// exception – we cannot assign it the right name right then. Instead, in our
1295
+ /// runtime startup code, we remember the thread ID of the main thread (through
1296
+ /// this modules `set` function) and use it to identify the main thread from then
1297
+ /// on. This works reliably and has the additional advantage that we can report
1298
+ /// the right thread name on main even after the thread handle has been destroyed.
1299
+ /// Note however that this also means that the name reported in pre-main functions
1300
+ /// will be incorrect, but that's just something we have to live with.
1289
1301
pub ( crate ) mod main_thread {
1290
1302
cfg_if:: cfg_if! {
1291
1303
if #[ cfg( target_has_atomic = "64" ) ] {
@@ -1331,21 +1343,35 @@ pub(crate) mod main_thread {
1331
1343
}
1332
1344
}
1333
1345
1346
+ /// Run a function with the current thread's name.
1347
+ ///
1348
+ /// Modulo thread local accesses, this function is safe to call from signal
1349
+ /// handlers and in similar circumstances where allocations are not possible.
1334
1350
pub ( crate ) fn with_current_name < F , R > ( f : F ) -> R
1335
1351
where
1336
1352
F : FnOnce ( Option < & str > ) -> R ,
1337
1353
{
1338
1354
try_with_current ( |thread| {
1339
1355
if let Some ( thread) = thread {
1356
+ // If there is a current thread handle, try to use the name stored
1357
+ // there.
1340
1358
if let Some ( name) = & thread. inner . name {
1341
1359
return f ( Some ( name. as_str ( ) ) ) ;
1342
1360
} else if Some ( thread. inner . id ) == main_thread:: get ( ) {
1361
+ // The main thread doesn't store its name in the handle, we must
1362
+ // identify it through its ID. Since we already have the `Thread`,
1363
+ // we can retrieve the ID from it instead of going through another
1364
+ // thread local.
1343
1365
return f ( Some ( "main" ) ) ;
1344
1366
}
1345
1367
} else if let Some ( main) = main_thread:: get ( )
1346
1368
&& let Some ( id) = current:: id:: get ( )
1347
1369
&& id == main
1348
1370
{
1371
+ // The main thread doesn't always have a thread handle, we must
1372
+ // identify it through its ID instead. The checks are ordered so
1373
+ // that the current ID is only loaded if it is actually needed,
1374
+ // since loading it from TLS might need multiple expensive accesses.
1349
1375
return f ( Some ( "main" ) ) ;
1350
1376
}
1351
1377
0 commit comments