158
158
#[ cfg( all( test, not( any( target_os = "emscripten" , target_os = "wasi" ) ) ) ) ]
159
159
mod tests;
160
160
161
- use core:: cell:: SyncUnsafeCell ;
162
- use core:: ffi:: CStr ;
163
- use core:: mem:: MaybeUninit ;
164
-
165
161
use crate :: any:: Any ;
166
162
use crate :: cell:: UnsafeCell ;
163
+ use crate :: ffi:: CStr ;
167
164
use crate :: marker:: PhantomData ;
168
165
use crate :: mem:: { self , ManuallyDrop , forget} ;
169
166
use crate :: num:: NonZero ;
@@ -1255,114 +1252,65 @@ impl ThreadId {
1255
1252
// Thread
1256
1253
////////////////////////////////////////////////////////////////////////////////
1257
1254
1255
+ /// The internal representation of a `Thread`'s name.
1256
+ enum ThreadName {
1257
+ Main ,
1258
+ Other ( ThreadNameString ) ,
1259
+ Unnamed ,
1260
+ }
1261
+
1258
1262
// This module ensures private fields are kept private, which is necessary to enforce the safety requirements.
1259
1263
mod thread_name_string {
1260
1264
use core:: str;
1261
1265
1266
+ use super :: ThreadName ;
1262
1267
use crate :: ffi:: { CStr , CString } ;
1263
1268
1264
1269
/// Like a `String` it's guaranteed UTF-8 and like a `CString` it's null terminated.
1265
1270
pub ( crate ) struct ThreadNameString {
1266
1271
inner : CString ,
1267
1272
}
1268
-
1269
- impl ThreadNameString {
1270
- pub fn as_str ( & self ) -> & str {
1271
- // SAFETY: `self.inner` is only initialised via `String`, which upholds the validity invariant of `str`.
1272
- unsafe { str:: from_utf8_unchecked ( self . inner . to_bytes ( ) ) }
1273
- }
1274
- }
1275
-
1276
1273
impl core:: ops:: Deref for ThreadNameString {
1277
1274
type Target = CStr ;
1278
1275
fn deref ( & self ) -> & CStr {
1279
1276
& self . inner
1280
1277
}
1281
1278
}
1282
-
1283
1279
impl From < String > for ThreadNameString {
1284
1280
fn from ( s : String ) -> Self {
1285
1281
Self {
1286
1282
inner : CString :: new ( s) . expect ( "thread name may not contain interior null bytes" ) ,
1287
1283
}
1288
1284
}
1289
1285
}
1286
+ impl ThreadName {
1287
+ pub fn as_cstr ( & self ) -> Option < & CStr > {
1288
+ match self {
1289
+ ThreadName :: Main => Some ( c"main" ) ,
1290
+ ThreadName :: Other ( other) => Some ( other) ,
1291
+ ThreadName :: Unnamed => None ,
1292
+ }
1293
+ }
1294
+
1295
+ pub fn as_str ( & self ) -> Option < & str > {
1296
+ // SAFETY: `as_cstr` can only return `Some` for a fixed CStr or a `ThreadNameString`,
1297
+ // which is guaranteed to be UTF-8.
1298
+ self . as_cstr ( ) . map ( |s| unsafe { str:: from_utf8_unchecked ( s. to_bytes ( ) ) } )
1299
+ }
1300
+ }
1290
1301
}
1291
1302
pub ( crate ) use thread_name_string:: ThreadNameString ;
1292
1303
1293
- static MAIN_THREAD_INFO : SyncUnsafeCell < ( MaybeUninit < ThreadId > , MaybeUninit < Parker > ) > =
1294
- SyncUnsafeCell :: new ( ( MaybeUninit :: uninit ( ) , MaybeUninit :: uninit ( ) ) ) ;
1295
-
1296
- /// The internal representation of a `Thread` that is not the main thread.
1297
- struct OtherInner {
1298
- name : Option < ThreadNameString > ,
1304
+ /// The internal representation of a `Thread` handle
1305
+ struct Inner {
1306
+ name : ThreadName , // Guaranteed to be UTF-8
1299
1307
id : ThreadId ,
1300
1308
parker : Parker ,
1301
1309
}
1302
1310
1303
- /// The internal representation of a `Thread` handle.
1304
- #[ derive( Clone ) ]
1305
- enum Inner {
1306
- /// Represents the main thread. May only be constructed by Thread::new_main.
1307
- Main ( & ' static ( ThreadId , Parker ) ) ,
1308
- /// Represents any other thread.
1309
- Other ( Pin < Arc < OtherInner > > ) ,
1310
- }
1311
-
1312
1311
impl Inner {
1313
- fn id ( & self ) -> ThreadId {
1314
- match self {
1315
- Self :: Main ( ( thread_id, _) ) => * thread_id,
1316
- Self :: Other ( other) => other. id ,
1317
- }
1318
- }
1319
-
1320
- fn cname ( & self ) -> Option < & CStr > {
1321
- match self {
1322
- Self :: Main ( _) => Some ( c"main" ) ,
1323
- Self :: Other ( other) => other. name . as_deref ( ) ,
1324
- }
1325
- }
1326
-
1327
- fn name ( & self ) -> Option < & str > {
1328
- match self {
1329
- Self :: Main ( _) => Some ( "main" ) ,
1330
- Self :: Other ( other) => other. name . as_ref ( ) . map ( ThreadNameString :: as_str) ,
1331
- }
1332
- }
1333
-
1334
- fn into_raw ( self ) -> * const ( ) {
1335
- match self {
1336
- // Just return the pointer to `MAIN_THREAD_INFO`.
1337
- Self :: Main ( ptr) => crate :: ptr:: from_ref ( ptr) . cast ( ) ,
1338
- Self :: Other ( arc) => {
1339
- // Safety: We only expose an opaque pointer, which maintains the `Pin` invariant.
1340
- let inner = unsafe { Pin :: into_inner_unchecked ( arc) } ;
1341
- Arc :: into_raw ( inner) as * const ( )
1342
- }
1343
- }
1344
- }
1345
-
1346
- /// # Safety
1347
- ///
1348
- /// See [`Thread::from_raw`].
1349
- unsafe fn from_raw ( ptr : * const ( ) ) -> Self {
1350
- // If the pointer is to `MAIN_THREAD_INFO`, we know it is the `Main` variant.
1351
- if crate :: ptr:: eq ( ptr. cast ( ) , & MAIN_THREAD_INFO ) {
1352
- Self :: Main ( unsafe { & * ptr. cast ( ) } )
1353
- } else {
1354
- // Safety: Upheld by caller
1355
- Self :: Other ( unsafe { Pin :: new_unchecked ( Arc :: from_raw ( ptr as * const OtherInner ) ) } )
1356
- }
1357
- }
1358
-
1359
- fn parker ( & self ) -> Pin < & Parker > {
1360
- match self {
1361
- Self :: Main ( ( _, parker_ref) ) => Pin :: static_ref ( parker_ref) ,
1362
- Self :: Other ( inner) => unsafe {
1363
- Pin :: map_unchecked ( inner. as_ref ( ) , |inner| & inner. parker )
1364
- } ,
1365
- }
1312
+ fn parker ( self : Pin < & Self > ) -> Pin < & Parker > {
1313
+ unsafe { Pin :: map_unchecked ( self , |inner| & inner. parker ) }
1366
1314
}
1367
1315
}
1368
1316
@@ -1386,55 +1334,41 @@ impl Inner {
1386
1334
/// docs of [`Builder`] and [`spawn`] for more details.
1387
1335
///
1388
1336
/// [`thread::current`]: current::current
1389
- pub struct Thread ( Inner ) ;
1337
+ pub struct Thread {
1338
+ inner : Pin < Arc < Inner > > ,
1339
+ }
1390
1340
1391
1341
impl Thread {
1392
1342
/// Used only internally to construct a thread object without spawning.
1393
1343
pub ( crate ) fn new ( id : ThreadId , name : String ) -> Thread {
1394
- Self :: new_inner ( id, Some ( ThreadNameString :: from ( name) ) )
1344
+ Self :: new_inner ( id, ThreadName :: Other ( name. into ( ) ) )
1395
1345
}
1396
1346
1397
1347
pub ( crate ) fn new_unnamed ( id : ThreadId ) -> Thread {
1398
- Self :: new_inner ( id, None )
1348
+ Self :: new_inner ( id, ThreadName :: Unnamed )
1399
1349
}
1400
1350
1401
- /// Used in runtime to construct main thread
1402
- ///
1403
- /// # Safety
1404
- ///
1405
- /// This must only ever be called once, and must be called on the main thread.
1406
- pub ( crate ) unsafe fn new_main ( thread_id : ThreadId ) -> Thread {
1407
- // Safety: As this is only called once and on the main thread, nothing else is accessing MAIN_THREAD_INFO
1408
- // as the only other read occurs in `main_thread_info` *after* the main thread has been constructed,
1409
- // and this function is the only one that constructs the main thread.
1410
- //
1411
- // Pre-main thread spawning cannot hit this either, as the caller promises that this is only called on the main thread.
1412
- let main_thread_info = unsafe { & mut * MAIN_THREAD_INFO . get ( ) } ;
1413
-
1414
- unsafe { Parker :: new_in_place ( ( & raw mut main_thread_info. 1 ) . cast ( ) ) } ;
1415
- main_thread_info. 0 . write ( thread_id) ;
1416
-
1417
- // Store a `'static` ref to the initialised ThreadId and Parker,
1418
- // to avoid having to repeatedly prove initialisation.
1419
- Self ( Inner :: Main ( unsafe { & * MAIN_THREAD_INFO . get ( ) . cast ( ) } ) )
1351
+ /// Constructs the thread handle for the main thread.
1352
+ pub ( crate ) fn new_main ( id : ThreadId ) -> Thread {
1353
+ Self :: new_inner ( id, ThreadName :: Main )
1420
1354
}
1421
1355
1422
- fn new_inner ( id : ThreadId , name : Option < ThreadNameString > ) -> Thread {
1356
+ fn new_inner ( id : ThreadId , name : ThreadName ) -> Thread {
1423
1357
// We have to use `unsafe` here to construct the `Parker` in-place,
1424
1358
// which is required for the UNIX implementation.
1425
1359
//
1426
1360
// SAFETY: We pin the Arc immediately after creation, so its address never
1427
1361
// changes.
1428
1362
let inner = unsafe {
1429
- let mut arc = Arc :: < OtherInner > :: new_uninit ( ) ;
1363
+ let mut arc = Arc :: < Inner > :: new_uninit ( ) ;
1430
1364
let ptr = Arc :: get_mut_unchecked ( & mut arc) . as_mut_ptr ( ) ;
1431
1365
( & raw mut ( * ptr) . name ) . write ( name) ;
1432
1366
( & raw mut ( * ptr) . id ) . write ( id) ;
1433
1367
Parker :: new_in_place ( & raw mut ( * ptr) . parker ) ;
1434
1368
Pin :: new_unchecked ( arc. assume_init ( ) )
1435
1369
} ;
1436
1370
1437
- Self ( Inner :: Other ( inner) )
1371
+ Thread { inner }
1438
1372
}
1439
1373
1440
1374
/// Like the public [`park`], but callable on any handle. This is used to
@@ -1443,7 +1377,7 @@ impl Thread {
1443
1377
/// # Safety
1444
1378
/// May only be called from the thread to which this handle belongs.
1445
1379
pub ( crate ) unsafe fn park ( & self ) {
1446
- unsafe { self . 0 . parker ( ) . park ( ) }
1380
+ unsafe { self . inner . as_ref ( ) . parker ( ) . park ( ) }
1447
1381
}
1448
1382
1449
1383
/// Like the public [`park_timeout`], but callable on any handle. This is
@@ -1452,7 +1386,7 @@ impl Thread {
1452
1386
/// # Safety
1453
1387
/// May only be called from the thread to which this handle belongs.
1454
1388
pub ( crate ) unsafe fn park_timeout ( & self , dur : Duration ) {
1455
- unsafe { self . 0 . parker ( ) . park_timeout ( dur) }
1389
+ unsafe { self . inner . as_ref ( ) . parker ( ) . park_timeout ( dur) }
1456
1390
}
1457
1391
1458
1392
/// Atomically makes the handle's token available if it is not already.
@@ -1488,7 +1422,7 @@ impl Thread {
1488
1422
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1489
1423
#[ inline]
1490
1424
pub fn unpark ( & self ) {
1491
- self . 0 . parker ( ) . unpark ( ) ;
1425
+ self . inner . as_ref ( ) . parker ( ) . unpark ( ) ;
1492
1426
}
1493
1427
1494
1428
/// Gets the thread's unique identifier.
@@ -1508,7 +1442,7 @@ impl Thread {
1508
1442
#[ stable( feature = "thread_id" , since = "1.19.0" ) ]
1509
1443
#[ must_use]
1510
1444
pub fn id ( & self ) -> ThreadId {
1511
- self . 0 . id ( )
1445
+ self . inner . id
1512
1446
}
1513
1447
1514
1448
/// Gets the thread's name.
@@ -1551,11 +1485,7 @@ impl Thread {
1551
1485
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1552
1486
#[ must_use]
1553
1487
pub fn name ( & self ) -> Option < & str > {
1554
- self . 0 . name ( )
1555
- }
1556
-
1557
- fn cname ( & self ) -> Option < & CStr > {
1558
- self . 0 . cname ( )
1488
+ self . inner . name . as_str ( )
1559
1489
}
1560
1490
1561
1491
/// Consumes the `Thread`, returning a raw pointer.
@@ -1579,7 +1509,9 @@ impl Thread {
1579
1509
/// ```
1580
1510
#[ unstable( feature = "thread_raw" , issue = "97523" ) ]
1581
1511
pub fn into_raw ( self ) -> * const ( ) {
1582
- self . 0 . into_raw ( )
1512
+ // Safety: We only expose an opaque pointer, which maintains the `Pin` invariant.
1513
+ let inner = unsafe { Pin :: into_inner_unchecked ( self . inner ) } ;
1514
+ Arc :: into_raw ( inner) as * const ( )
1583
1515
}
1584
1516
1585
1517
/// Constructs a `Thread` from a raw pointer.
@@ -1601,7 +1533,11 @@ impl Thread {
1601
1533
#[ unstable( feature = "thread_raw" , issue = "97523" ) ]
1602
1534
pub unsafe fn from_raw ( ptr : * const ( ) ) -> Thread {
1603
1535
// Safety: Upheld by caller.
1604
- unsafe { Thread ( Inner :: from_raw ( ptr) ) }
1536
+ unsafe { Thread { inner : Pin :: new_unchecked ( Arc :: from_raw ( ptr as * const Inner ) ) } }
1537
+ }
1538
+
1539
+ fn cname ( & self ) -> Option < & CStr > {
1540
+ self . inner . name . as_cstr ( )
1605
1541
}
1606
1542
}
1607
1543
0 commit comments