]> git.lizzy.rs Git - rust.git/commitdiff
Change representation and conversion of ThreadId and BlockSetId.
authorVytautas Astrauskas <astrauv@amazon.com>
Mon, 27 Apr 2020 04:01:03 +0000 (21:01 -0700)
committerVytautas Astrauskas <astrauv@amazon.com>
Mon, 27 Apr 2020 21:26:36 +0000 (14:26 -0700)
src/shims/sync.rs
src/thread.rs

index 97afbbe98f6a001973d624306b83dbd4177c501f..b0605b4e8146b3fdc76b5267695b1e394e53e7de 100644 (file)
@@ -158,7 +158,7 @@ fn mutex_get_or_create_blockset<'mir, 'tcx: 'mir>(
         mutex_set_blockset(ecx, mutex_op, blockset.to_u32_scalar())?;
         Ok(blockset)
     } else {
-        Ok(blockset.into())
+        Ok(BlockSetId::new(blockset))
     }
 }
 
@@ -233,7 +233,7 @@ fn rwlock_get_or_create_writer_blockset<'mir, 'tcx: 'mir>(
         rwlock_set_writer_blockset(ecx, rwlock_op, blockset.to_u32_scalar())?;
         Ok(blockset)
     } else {
-        Ok(blockset.into())
+        Ok(BlockSetId::new(blockset))
     }
 }
 
@@ -264,7 +264,7 @@ fn rwlock_get_or_create_reader_blockset<'mir, 'tcx: 'mir>(
         rwlock_set_reader_blockset(ecx, rwlock_op, blockset.to_u32_scalar())?;
         Ok(blockset)
     } else {
-        Ok(blockset.into())
+        Ok(BlockSetId::new(blockset))
     }
 }
 
index f9094d771e6dd87cfbb19833ad48916f69ff1fba..749d6bf955f27a66a6b35d8780f7da7142cb91c8 100644 (file)
@@ -30,7 +30,7 @@ pub enum SchedulingAction {
 
 /// A thread identifier.
 #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
-pub struct ThreadId(usize);
+pub struct ThreadId(u32);
 
 /// The main thread. When it terminates, the whole application terminates.
 const MAIN_THREAD: ThreadId = ThreadId(0);
@@ -43,22 +43,22 @@ pub fn to_u128(self) -> u128 {
 
 impl Idx for ThreadId {
     fn new(idx: usize) -> Self {
-        ThreadId(idx)
+        ThreadId(u32::try_from(idx).unwrap())
     }
     fn index(self) -> usize {
-        self.0
+        usize::try_from(self.0).unwrap()
     }
 }
 
 impl From<u64> for ThreadId {
     fn from(id: u64) -> Self {
-        Self(usize::try_from(id).unwrap())
+        Self(u32::try_from(id).unwrap())
     }
 }
 
 impl From<u32> for ThreadId {
     fn from(id: u32) -> Self {
-        Self(usize::try_from(id).unwrap())
+        Self(u32::try_from(id).unwrap())
     }
 }
 
@@ -73,13 +73,11 @@ pub fn to_u32_scalar<'tcx>(&self) -> Scalar<Tag> {
 #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
 pub struct BlockSetId(NonZeroU32);
 
-impl From<u32> for BlockSetId {
-    fn from(id: u32) -> Self {
+impl BlockSetId {
+    /// Panics if `id` is 0.
+    pub fn new(id: u32) -> Self {
         Self(NonZeroU32::new(id).expect("0 is not a valid blockset id"))
     }
-}
-
-impl BlockSetId {
     pub fn to_u32_scalar<'tcx>(&self) -> Scalar<Tag> {
         Scalar::from_u32(self.0.get())
     }
@@ -325,7 +323,7 @@ fn get_thread_name(&self) -> InterpResult<'tcx, &[u8]> {
     /// Allocate a new blockset id.
     fn create_blockset(&mut self) -> BlockSetId {
         self.blockset_counter = self.blockset_counter.checked_add(1).unwrap();
-        self.blockset_counter.into()
+        BlockSetId::new(self.blockset_counter)
     }
 
     /// Block the currently active thread and put it into the given blockset.