Browse Source

rust: migration: implement ToMigrationState for Timer

Timer is a complex struct, allow adding it to a struct that
uses #[derive(ToMigrationState)]; similar to vmstate_timer, only
the expiration time has to be preserved.

In fact, because it is thread-safe, ToMigrationStateShared can
also be implemented without needing a cell or mutex that wraps
the timer.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
pull/316/head
Paolo Bonzini 9 months ago
parent
commit
9947bf9351
  1. 1
      rust/hw/timer/hpet/src/device.rs
  2. 31
      rust/migration/src/migratable.rs

1
rust/hw/timer/hpet/src/device.rs

@ -250,7 +250,6 @@ impl HPETTimerRegisters {
}
/// HPET Timer Abstraction
#[repr(C)]
#[derive(Debug)]
pub struct HPETTimer {
/// timer N index within the timer block (`HPETState`)

31
rust/migration/src/migratable.rs

@ -140,6 +140,26 @@ macro_rules! impl_for_primitive {
impl_for_primitive!(u8, u16, u32, u64, i8, i16, i32, i64, bool);
impl ToMigrationState for util::timer::Timer {
type Migrated = i64;
fn snapshot_migration_state(&self, target: &mut i64) -> Result<(), InvalidError> {
// SAFETY: as_ptr() is unsafe to ensure that the caller reasons about
// the pinning of the data inside the Opaque<>. Here all we do is
// access a field.
*target = self.expire_time_ns().unwrap_or(-1);
Ok(())
}
fn restore_migrated_state_mut(
&mut self,
source: Self::Migrated,
version_id: u8,
) -> Result<(), InvalidError> {
self.restore_migrated_state(source, version_id)
}
}
impl<T: ToMigrationState, const N: usize> ToMigrationState for [T; N]
where
[T::Migrated; N]: Default,
@ -237,6 +257,17 @@ pub trait ToMigrationStateShared: ToMigrationState {
) -> Result<(), InvalidError>;
}
impl ToMigrationStateShared for util::timer::Timer {
fn restore_migrated_state(&self, source: i64, _version_id: u8) -> Result<(), InvalidError> {
if source >= 0 {
self.modify_ns(source as u64);
} else {
self.delete();
}
Ok(())
}
}
impl<T: ToMigrationStateShared, const N: usize> ToMigrationStateShared for [T; N]
where
[T::Migrated; N]: Default,

Loading…
Cancel
Save