1
1
//! Implements threads.
2
2
3
3
use std:: mem;
4
- use std:: num:: TryFromIntError ;
5
4
use std:: sync:: atomic:: Ordering :: Relaxed ;
6
5
use std:: task:: Poll ;
7
6
use std:: time:: { Duration , SystemTime } ;
@@ -127,26 +126,6 @@ impl Idx for ThreadId {
127
126
}
128
127
}
129
128
130
- impl TryFrom < u64 > for ThreadId {
131
- type Error = TryFromIntError ;
132
- fn try_from ( id : u64 ) -> Result < Self , Self :: Error > {
133
- u32:: try_from ( id) . map ( Self )
134
- }
135
- }
136
-
137
- impl TryFrom < i128 > for ThreadId {
138
- type Error = TryFromIntError ;
139
- fn try_from ( id : i128 ) -> Result < Self , Self :: Error > {
140
- u32:: try_from ( id) . map ( Self )
141
- }
142
- }
143
-
144
- impl From < u32 > for ThreadId {
145
- fn from ( id : u32 ) -> Self {
146
- Self ( id)
147
- }
148
- }
149
-
150
129
impl From < ThreadId > for u64 {
151
130
fn from ( t : ThreadId ) -> Self {
152
131
t. 0 . into ( )
@@ -448,6 +427,10 @@ pub enum TimeoutAnchor {
448
427
Absolute ,
449
428
}
450
429
430
+ /// An error signaling that the requested thread doesn't exist.
431
+ #[ derive( Debug , Copy , Clone ) ]
432
+ pub struct ThreadNotFound ;
433
+
451
434
/// A set of threads.
452
435
#[ derive( Debug ) ]
453
436
pub struct ThreadManager < ' tcx > {
@@ -509,6 +492,16 @@ impl<'tcx> ThreadManager<'tcx> {
509
492
}
510
493
}
511
494
495
+ pub fn thread_id_try_from ( & self , id : impl TryInto < u32 > ) -> Result < ThreadId , ThreadNotFound > {
496
+ if let Ok ( id) = id. try_into ( )
497
+ && usize:: try_from ( id) . is_ok_and ( |id| id < self . threads . len ( ) )
498
+ {
499
+ Ok ( ThreadId ( id) )
500
+ } else {
501
+ Err ( ThreadNotFound )
502
+ }
503
+ }
504
+
512
505
/// Check if we have an allocation for the given thread local static for the
513
506
/// active thread.
514
507
fn get_thread_local_alloc_id ( & self , def_id : DefId ) -> Option < StrictPointer > {
@@ -534,6 +527,7 @@ impl<'tcx> ThreadManager<'tcx> {
534
527
) -> & mut Vec < Frame < ' tcx , Provenance , FrameExtra < ' tcx > > > {
535
528
& mut self . threads [ self . active_thread ] . stack
536
529
}
530
+
537
531
pub fn all_stacks (
538
532
& self ,
539
533
) -> impl Iterator < Item = ( ThreadId , & [ Frame < ' tcx , Provenance , FrameExtra < ' tcx > > ] ) > {
@@ -868,6 +862,11 @@ trait EvalContextPrivExt<'tcx>: MiriInterpCxExt<'tcx> {
868
862
// Public interface to thread management.
869
863
impl < ' tcx > EvalContextExt < ' tcx > for crate :: MiriInterpCx < ' tcx > { }
870
864
pub trait EvalContextExt < ' tcx > : crate :: MiriInterpCxExt < ' tcx > {
865
+ #[ inline]
866
+ fn thread_id_try_from ( & self , id : impl TryInto < u32 > ) -> Result < ThreadId , ThreadNotFound > {
867
+ self . eval_context_ref ( ) . machine . threads . thread_id_try_from ( id)
868
+ }
869
+
871
870
/// Get a thread-specific allocation id for the given thread-local static.
872
871
/// If needed, allocate a new one.
873
872
fn get_or_create_thread_local_alloc (
@@ -1160,8 +1159,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1160
1159
/// Set the name of the current thread. The buffer must not include the null terminator.
1161
1160
#[ inline]
1162
1161
fn set_thread_name ( & mut self , thread : ThreadId , new_thread_name : Vec < u8 > ) {
1163
- let this = self . eval_context_mut ( ) ;
1164
- this. machine . threads . set_thread_name ( thread, new_thread_name) ;
1162
+ self . eval_context_mut ( ) . machine . threads . set_thread_name ( thread, new_thread_name) ;
1165
1163
}
1166
1164
1167
1165
#[ inline]
0 commit comments