11//! Implements threads.
22
33use std:: mem;
4- use std:: num:: TryFromIntError ;
54use std:: sync:: atomic:: Ordering :: Relaxed ;
65use std:: task:: Poll ;
76use std:: time:: { Duration , SystemTime } ;
@@ -127,26 +126,6 @@ impl Idx for ThreadId {
127126 }
128127}
129128
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-
150129impl From < ThreadId > for u64 {
151130 fn from ( t : ThreadId ) -> Self {
152131 t. 0 . into ( )
@@ -448,6 +427,10 @@ pub enum TimeoutAnchor {
448427 Absolute ,
449428}
450429
430+ /// An error signaling that the requested thread doesn't exist.
431+ #[ derive( Debug , Copy , Clone ) ]
432+ pub struct ThreadNotFound ;
433+
451434/// A set of threads.
452435#[ derive( Debug ) ]
453436pub struct ThreadManager < ' tcx > {
@@ -509,6 +492,16 @@ impl<'tcx> ThreadManager<'tcx> {
509492 }
510493 }
511494
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+
512505 /// Check if we have an allocation for the given thread local static for the
513506 /// active thread.
514507 fn get_thread_local_alloc_id ( & self , def_id : DefId ) -> Option < StrictPointer > {
@@ -534,6 +527,7 @@ impl<'tcx> ThreadManager<'tcx> {
534527 ) -> & mut Vec < Frame < ' tcx , Provenance , FrameExtra < ' tcx > > > {
535528 & mut self . threads [ self . active_thread ] . stack
536529 }
530+
537531 pub fn all_stacks (
538532 & self ,
539533 ) -> impl Iterator < Item = ( ThreadId , & [ Frame < ' tcx , Provenance , FrameExtra < ' tcx > > ] ) > {
@@ -868,6 +862,11 @@ trait EvalContextPrivExt<'tcx>: MiriInterpCxExt<'tcx> {
868862// Public interface to thread management.
869863impl < ' tcx > EvalContextExt < ' tcx > for crate :: MiriInterpCx < ' tcx > { }
870864pub 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+
871870 /// Get a thread-specific allocation id for the given thread-local static.
872871 /// If needed, allocate a new one.
873872 fn get_or_create_thread_local_alloc (
@@ -1160,8 +1159,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11601159 /// Set the name of the current thread. The buffer must not include the null terminator.
11611160 #[ inline]
11621161 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) ;
11651163 }
11661164
11671165 #[ inline]
0 commit comments