[PATCH 06/16] rust: do not use OnceLock for properties

Paolo Bonzini posted 16 patches 1 week ago
There is a newer version of this series
[PATCH 06/16] rust: do not use OnceLock for properties
Posted by Paolo Bonzini 1 week ago
Properties are initialized lazily but always accessed within the big QEMU lock.

There is no need to have a OnceLock around them, and also OnceCell/OnceLock
were only stabilized in 1.70.0; so remove it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 rust/qemu-api/src/device_class.rs | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/rust/qemu-api/src/device_class.rs b/rust/qemu-api/src/device_class.rs
index b6b68cf9ce2..87892b50c63 100644
--- a/rust/qemu-api/src/device_class.rs
+++ b/rust/qemu-api/src/device_class.rs
@@ -2,8 +2,6 @@
 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
 // SPDX-License-Identifier: GPL-2.0-or-later
 
-use std::sync::OnceLock;
-
 use crate::bindings::Property;
 
 #[macro_export]
@@ -73,12 +71,15 @@ macro_rules! define_property {
 }
 
 #[repr(C)]
-pub struct Properties<const N: usize>(pub OnceLock<[Property; N]>, pub fn() -> [Property; N]);
+pub struct Properties<const N: usize>(pub Option<[Property; N]>, pub fn() -> [Property; N]);
 
 impl<const N: usize> Properties<N> {
     pub fn as_mut_ptr(&mut self) -> *mut Property {
-        _ = self.0.get_or_init(self.1);
-        self.0.get_mut().unwrap().as_mut_ptr()
+        match self.0 {
+            None => { self.0 = Some(self.1()); },
+            Some(_) => {},
+        }
+        self.0.as_mut().unwrap().as_mut_ptr()
     }
 }
 
@@ -104,7 +105,7 @@ const fn _calc_prop_len() -> usize {
         }
 
         #[no_mangle]
-        pub static mut $ident: $crate::device_class::Properties<PROP_LEN> = $crate::device_class::Properties(::std::sync::OnceLock::new(), _make_properties);
+        pub static mut $ident: $crate::device_class::Properties<PROP_LEN> = $crate::device_class::Properties(None, _make_properties);
     };
 }
 
-- 
2.46.2
Re: [PATCH 06/16] rust: do not use OnceLock for properties
Posted by Zhao Liu 4 days, 6 hours ago
On Tue, Oct 15, 2024 at 03:17:24PM +0200, Paolo Bonzini wrote:
> Date: Tue, 15 Oct 2024 15:17:24 +0200
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH 06/16] rust: do not use OnceLock for properties
> X-Mailer: git-send-email 2.46.2
> 
> Properties are initialized lazily but always accessed within the big QEMU lock.
> 
> There is no need to have a OnceLock around them, and also OnceCell/OnceLock
> were only stabilized in 1.70.0; so remove it.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  rust/qemu-api/src/device_class.rs | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>