@@ -1245,9 +1245,16 @@ impl ThreadId {
1245
1245
// Thread
1246
1246
////////////////////////////////////////////////////////////////////////////////
1247
1247
1248
+ /// The internal representation of a `Thread`'s name.
1249
+ enum ThreadName {
1250
+ Main ,
1251
+ Other ( CString ) ,
1252
+ Unnamed ,
1253
+ }
1254
+
1248
1255
/// The internal representation of a `Thread` handle
1249
1256
struct Inner {
1250
- name : Option < CString > , // Guaranteed to be UTF-8
1257
+ name : ThreadName , // Guaranteed to be UTF-8
1251
1258
id : ThreadId ,
1252
1259
parker : Parker ,
1253
1260
}
@@ -1284,8 +1291,20 @@ pub struct Thread {
1284
1291
1285
1292
impl Thread {
1286
1293
// Used only internally to construct a thread object without spawning
1287
- // Panics if the name contains nuls.
1288
1294
pub ( crate ) fn new ( name : Option < CString > ) -> Thread {
1295
+ if let Some ( name) = name {
1296
+ Self :: new_inner ( ThreadName :: Other ( name) )
1297
+ } else {
1298
+ Self :: new_inner ( ThreadName :: Unnamed )
1299
+ }
1300
+ }
1301
+
1302
+ // Used in runtime to construct main thread
1303
+ pub ( crate ) fn new_main ( ) -> Thread {
1304
+ Self :: new_inner ( ThreadName :: Main )
1305
+ }
1306
+
1307
+ fn new_inner ( name : ThreadName ) -> Thread {
1289
1308
// We have to use `unsafe` here to construct the `Parker` in-place,
1290
1309
// which is required for the UNIX implementation.
1291
1310
//
@@ -1412,7 +1431,11 @@ impl Thread {
1412
1431
}
1413
1432
1414
1433
fn cname ( & self ) -> Option < & CStr > {
1415
- self . inner . name . as_deref ( )
1434
+ match & self . inner . name {
1435
+ ThreadName :: Main => Some ( c"main" ) ,
1436
+ ThreadName :: Other ( other) => Some ( & other) ,
1437
+ ThreadName :: Unnamed => None ,
1438
+ }
1416
1439
}
1417
1440
}
1418
1441
0 commit comments