diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2025-02-14 10:18:34 +0100 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2025-03-06 12:44:46 +0100 |
commit | 0b9d05e3c98fe168f3502ccc422b9171467314fa (patch) | |
tree | d485a70bce3fd8e77fcbbeeeeca3552425e11979 /docs/devel | |
parent | cff666a3aee566889fcc1ab7167ca0a727af7167 (diff) | |
download | qemu-0b9d05e3c98fe168f3502ccc422b9171467314fa.zip qemu-0b9d05e3c98fe168f3502ccc422b9171467314fa.tar.gz qemu-0b9d05e3c98fe168f3502ccc422b9171467314fa.tar.bz2 |
rust: cell: add wrapper for FFI types
Inspired by the same-named type in Linux. This type provides the compiler
with a correct view of what goes on with FFI types. In addition, it
separates the glue code from the bindgen-generated code, allowing
traits such as Send, Sync or Zeroable to be specified independently
for C and Rust structs.
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'docs/devel')
-rw-r--r-- | docs/devel/rust.rst | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst index 5d8aa3a..784c3e4 100644 --- a/docs/devel/rust.rst +++ b/docs/devel/rust.rst @@ -296,15 +296,33 @@ of ``&mut self``; access to internal fields must use *interior mutability* to go from a shared reference to a ``&mut``. Whenever C code provides you with an opaque ``void *``, avoid converting it -to a Rust mutable reference, and use a shared reference instead. Rust code -will then have to use QEMU's ``BqlRefCell`` and ``BqlCell`` type, which -enforce that locking rules for the "Big QEMU Lock" are respected. These cell -types are also known to the ``vmstate`` crate, which is able to "look inside" -them when building an in-memory representation of a ``struct``'s layout. -Note that the same is not true of a ``RefCell`` or ``Mutex``. - -In the future, similar cell types might also be provided for ``AioContext``-based -locking as well. +to a Rust mutable reference, and use a shared reference instead. The +``qemu_api::cell`` module provides wrappers that can be used to tell the +Rust compiler about interior mutability, and optionally to enforce locking +rules for the "Big QEMU Lock". In the future, similar cell types might +also be provided for ``AioContext``-based locking as well. + +In particular, device code will usually rely on the ``BqlRefCell`` and +``BqlCell`` type to ensure that data is accessed correctly under the +"Big QEMU Lock". These cell types are also known to the ``vmstate`` +crate, which is able to "look inside" them when building an in-memory +representation of a ``struct``'s layout. Note that the same is not true +of a ``RefCell`` or ``Mutex``. + +Bindings code instead will usually use the ``Opaque`` type, which hides +the contents of the underlying struct and can be easily converted to +a raw pointer, for use in calls to C functions. It can be used for +example as follows:: + + #[repr(transparent)] + #[derive(Debug)] + pub struct Object(Opaque<bindings::Object>); + +The bindings will then manually check for the big QEMU lock with +assertions, which allows the wrapper to be declared thread-safe:: + + unsafe impl Send for Object {} + unsafe impl Sync for Object {} Writing bindings to C code '''''''''''''''''''''''''' |