qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Zhao Liu <zhao1.liu@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Michael S . Tsirkin" <mst@redhat.com>,
	"Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>,
	"Junjie Mao" <junjie.mao@hotmail.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	qemu-devel@nongnu.org, qemu-rust@nongnu.org
Subject: Re: [RFC 07/13] rust: add bindings for timer
Date: Wed, 15 Jan 2025 15:09:13 +0800	[thread overview]
Message-ID: <Z4dfGcf1SbXaNSEM@intel.com> (raw)
In-Reply-To: <CABgObfYRAYPBidb+cUPCrtdAEXGZiSG2hf5CvJskXmTpR1ftpQ@mail.gmail.com>

On Tue, Jan 14, 2025 at 05:14:48PM +0100, Paolo Bonzini wrote:
> Date: Tue, 14 Jan 2025 17:14:48 +0100
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: Re: [RFC 07/13] rust: add bindings for timer
> 
> On Tue, Jan 14, 2025 at 4:18 PM Zhao Liu <zhao1.liu@intel.com> wrote:
> > ...Now I have a draft for timer binding:
> >
> > * timer binding:
> >
> > impl QEMUTimer {
> >     pub fn new() -> Self {
> >         Zeroable::ZERO
> >     }
> 
> Maybe Default too (not sure if you even need new())?

Yes, I find bindgen has already implemented Default for QEMUTimer:

#[repr(C)]
#[derive(Debug)]
pub struct QEMUTimer {
    pub expire_time: i64,
    pub timer_list: *mut QEMUTimerList,
    pub cb: QEMUTimerCB,
    pub opaque: *mut std::os::raw::c_void,
    pub next: *mut QEMUTimer,
    pub attributes: std::os::raw::c_int,
    pub scale: std::os::raw::c_int,
}

impl Default for QEMUTimer {
    fn default() -> Self {
        let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
        unsafe {
            ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
            s.assume_init()
        }
    }
}

HPETTimer just has a pointer to a QEMUTimer, so I need the new() in
init_timer_with_state() to create QEMUTimer within a Box<>. I'll use
Default instead in new().

> >     pub fn timer_init_full<'a, 'b: 'a, T, F>(
> 
> It's better to use longer names like 'timer and 'opaque. But it's also
> always possible to pass a longer lifetime, so 'opaque: 'timer is
> strictly speaking not needed: you can just use &'timer T which in turn
> means that lifetime elision applies. That said, I think I like the
> idea of using 'timer and 'opaque lifetimes here, for clarity.

Thanks, I'll change the lifetime names.

> >         &'a mut self,
> >         timer_list_group: Option<&mut QEMUTimerListGroup>,
> 
> I think QEMUTimerListGroup can (should) be shared because it's thread safe.

I understand here I can pass the immutable reference like (and that's
the updated timer_init_full()):

    pub fn timer_init_full<'timer, 'opaque: 'timer, T, F>(
        &'timer mut self,
        timer_list_group: Option<&QEMUTimerListGroup>,
        clk_type: QEMUClockType,
        scale: u32,
        attributes: u32,
        _f: F,
        opaque: &'opaque T,
    ) where
        F: for<'a> FnCall<(&'a T,)>,
    {
        let timer_cb: unsafe extern "C" fn(*mut c_void) = rust_timer_handler::<T, F>;

        // SAFETY: the opaque outlives the timer
        unsafe {
            timer_init_full(
                self,
                if let Some(g) = timer_list_group {
                    g as *const QEMUTimerListGroup as *mut QEMUTimerListGroup
                } else {
                    ::core::ptr::null_mut()
                },
                clk_type,
                scale as c_int,
                attributes as c_int,
                Some(timer_cb),
                opaque as *const T as *const c_void as *mut c_void,
            )
        }
    }

> >         clk_type: QEMUClockType,
> >         scale: u32,
> >         attributes: u32,
> >         _f: &F,
> 
> Better: "_f: &'static F", or even "_f: F" if it works.

"_f: F" can work since I passed a function reference (&cb, not a
function pointer).

With "_f: F", passing timer_handler directly is better.

> >         opaque: &'b T,
> >     ) where
> >         F: for<'c> FnCall<(&'c T,)> + 'b,
> 
> 'b ('opaque) is not needed here because the opaque is passed _into_
> the function (thus its lifetime is 'c). 'timer would make sense, but
> in fact the function itself is always 'static (see FnCall declaration)
> so it is unnecessary to add a lifetime to FnCall.

I see! Thank you for clarification.

> > fn timer_handler(timer_cell: &BqlRefCell<HPETTimer>) {
> >     timer_cell.borrow_mut().callback()
> > }
> >
> > impl HPETTimer {
> >     ...
> >
> >     fn init_timer_with_state(&mut self) {
> >         let index = self.index;
> >         let cb = |cell: &BqlRefCell<HPETTimer>| {
> >             timer_handler(cell);
> >         };
> 
> Why is the anonymous function needed?  Can you just pass "timer_handler"?

Yes, I should clean up this closure...

> > Is this timer binding as you expected? Hope I am on the right track. :-)
> 
> If the only correction is to the function declaration, that's as good
> as it can be for Rust! ;)
> 

Thank you! Now I have a updated timer_init_full() (like I pasted above).

Regards,
Zhao



  reply	other threads:[~2025-01-15  6:51 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-05  6:07 [RFC 00/13] rust: Reinvent the wheel for HPET timer in Rust Zhao Liu
2024-12-05  6:07 ` [RFC 01/13] bql: check that the BQL is not dropped within marked sections Zhao Liu
2024-12-05  6:07 ` [RFC 02/13] rust: cell: add BQL-enforcing RefCell variant Zhao Liu
2024-12-05  6:07 ` [RFC 03/13] rust/cell: add get_mut() method for BqlCell Zhao Liu
2024-12-05 15:55   ` Paolo Bonzini
2024-12-07 15:56     ` Zhao Liu
2024-12-07 19:49       ` Paolo Bonzini
2024-12-05  6:07 ` [RFC 04/13] rust: add bindings for gpio_{in|out} initialization Zhao Liu
2024-12-05 18:53   ` Paolo Bonzini
2024-12-08 16:27     ` Zhao Liu
2024-12-09 11:08       ` Paolo Bonzini
2025-01-16  3:04         ` Zhao Liu
2025-01-17  9:40           ` Paolo Bonzini
2025-01-17 11:14             ` Zhao Liu
2025-01-17 12:47               ` Paolo Bonzini
2024-12-05  6:07 ` [RFC 05/13] rust: add a bit operation binding for deposit64 Zhao Liu
2024-12-05 16:09   ` Paolo Bonzini
2024-12-07 16:01     ` Zhao Liu
2024-12-07 19:44       ` Paolo Bonzini
2024-12-05  6:07 ` [RFC 06/13] rust: add bindings for memattrs Zhao Liu
2024-12-05 18:15   ` Richard Henderson
2024-12-05 18:30     ` Paolo Bonzini
2024-12-05 23:51       ` Richard Henderson
2024-12-06  8:41         ` Zhao Liu
2024-12-06 14:07           ` Richard Henderson
2024-12-06 10:59       ` Peter Maydell
2024-12-06 14:28         ` Paolo Bonzini
2024-12-06 14:42           ` Peter Maydell
2024-12-06 19:13             ` Paolo Bonzini
2025-01-20 16:52               ` Zhao Liu
2025-01-20 17:19                 ` Paolo Bonzini
2025-01-23 15:10                   ` Zhao Liu
2025-01-23 15:33                     ` Paolo Bonzini
2024-12-07  9:21         ` Philippe Mathieu-Daudé
2024-12-08  9:30           ` Paolo Bonzini
2024-12-08 15:51             ` Zhao Liu
2024-12-05  6:07 ` [RFC 07/13] rust: add bindings for timer Zhao Liu
2024-12-05 18:18   ` Richard Henderson
2024-12-07 17:18     ` Zhao Liu
2024-12-05 18:46   ` Paolo Bonzini
2024-12-07 16:54     ` Zhao Liu
2025-01-14 15:36     ` Zhao Liu
2025-01-14 15:42       ` Zhao Liu
2025-01-14 16:14       ` Paolo Bonzini
2025-01-15  7:09         ` Zhao Liu [this message]
2024-12-05  6:07 ` [RFC 08/13] rust/qdev: add the macro to define bit property Zhao Liu
2024-12-05  6:07 ` [RFC 09/13] i386/fw_cfg: move hpet_cfg definition to hpet.c Zhao Liu
2024-12-05 12:04   ` Philippe Mathieu-Daudé
2024-12-05 12:46     ` Zhao Liu
2024-12-05 21:17       ` Philippe Mathieu-Daudé
2024-12-05 21:19         ` Paolo Bonzini
2024-12-07  9:16           ` Philippe Mathieu-Daudé
2024-12-07 15:36             ` Zhao Liu
2024-12-05 15:30   ` Paolo Bonzini
2024-12-07 15:28     ` Zhao Liu
2025-01-17 10:31     ` Zhao Liu
2025-01-17 10:15       ` Paolo Bonzini
2024-12-05  6:07 ` [RFC 10/13] rust/timer/hpet: define hpet_fw_cfg Zhao Liu
2024-12-05  6:07 ` [RFC 11/13] rust/timer/hpet: add basic HPET timer & state Zhao Liu
2024-12-05 20:22   ` Paolo Bonzini
2024-12-05 21:20     ` Paolo Bonzini
2024-12-09  7:46       ` Zhao Liu
2024-12-09  7:26     ` Zhao Liu
2024-12-05  6:07 ` [RFC 12/13] rust/timer/hpet: add qdev APIs support Zhao Liu
2024-12-05 18:58   ` Paolo Bonzini
2024-12-07 16:05     ` Zhao Liu
2024-12-05  6:07 ` [RFC 13/13] i386: enable rust hpet for pc when rust is enabled Zhao Liu
2024-12-05 15:20   ` Paolo Bonzini
2024-12-06  9:06     ` Zhao Liu
2024-12-05  6:22 ` [RFC 00/13] rust: Reinvent the wheel for HPET timer in Rust Zhao Liu
2024-12-05 16:28 ` Paolo Bonzini
2024-12-09  7:57   ` Zhao Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z4dfGcf1SbXaNSEM@intel.com \
    --to=zhao1.liu@intel.com \
    --cc=alex.bennee@linaro.org \
    --cc=junjie.mao@hotmail.com \
    --cc=manos.pitsidianakis@linaro.org \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-rust@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).