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