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