Add the binding of timer_mod_ns(), and re-implement Timer::modify()
based on Timer::modify_ns().

Signed-off-by: Zhao Liu <[email protected]>
---
 rust/util/src/timer.rs | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/rust/util/src/timer.rs b/rust/util/src/timer.rs
index c3a9de6b72df..88f120c54185 100644
--- a/rust/util/src/timer.rs
+++ b/rust/util/src/timer.rs
@@ -10,7 +10,7 @@
 use common::{callbacks::FnCall, Opaque};

 use crate::bindings::{
-    self, qemu_clock_get_ns, timer_del, timer_init_full, timer_mod, 
QEMUClockType,
+    self, qemu_clock_get_ns, timer_del, timer_init_full, timer_mod_ns, 
QEMUClockType,
 };

 /// A safe wrapper around [`bindings::QEMUTimer`].
@@ -86,10 +86,18 @@ pub fn init_full<'timer, 'opaque: 'timer, T, F>(
         }
     }

-    pub fn modify(&self, expire_time: i64) {
+    pub fn modify_ns(&self, expire_time: i64) {
         // SAFETY: the only way to obtain a Timer safely is via methods that
         // take a Pin<&mut Self>, therefore the timer is pinned
-        unsafe { timer_mod(self.as_mut_ptr(), expire_time) }
+        unsafe { timer_mod_ns(self.as_mut_ptr(), expire_time) }
+    }
+
+    pub fn modify(&self, expire_time: i64) {
+        // SAFETY: the only way to obtain a Timer safely is via methods that
+        // take a Pin<&mut Self>, therefore the timer is pinned and is valid
+        let scale = unsafe { *self.as_ptr() }.scale as i64;
+        // Following C's behavior, scale expire_time even when it is negative.
+        self.modify_ns(expire_time * scale);
     }

     pub fn delete(&self) {
--
2.34.1



Reply via email to