aboutsummaryrefslogtreecommitdiff
path: root/rust/qemu-api/src/lib.rs
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2024-11-15 13:26:58 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2024-12-10 18:49:25 +0100
commit8e194c0ea5cdbae05b77125a582f9927678121ee (patch)
tree070b9ae2f74a8231810943750b3a9292c5871ce8 /rust/qemu-api/src/lib.rs
parent37fb26601dd156369ebb84096c2ecfbe89f0a83b (diff)
downloadqemu-8e194c0ea5cdbae05b77125a582f9927678121ee.zip
qemu-8e194c0ea5cdbae05b77125a582f9927678121ee.tar.gz
qemu-8e194c0ea5cdbae05b77125a582f9927678121ee.tar.bz2
rust: cell: add BQL-enforcing Cell variant
QEMU objects usually have their pointer shared with the "outside world" very early in their lifetime, for example when they create their MemoryRegions. Because at this point it is not valid anymore to create a &mut reference to the device, individual parts of the device struct must be made mutable in a controlled manner. QEMU's Big Lock (BQL) effectively turns multi-threaded code into single-threaded code while device code runs, as long as the BQL is not released while the device is borrowed (because C code could sneak in and mutate the device). We can then introduce custom interior mutability primitives that are semantically similar to the standard library's (single-threaded) Cell and RefCell, but account for QEMU's threading model. Accessing the "BqlCell" or borrowing the "BqlRefCell" requires proving that the BQL is held, and attempting to access without the BQL is a runtime panic, similar to RefCell's already-borrowed panic. With respect to naming I also considered omitting the "Bql" prefix or moving it to the module, e.g. qemu_api::bql::{Cell, RefCell}. However, this could easily lead to mistakes and confusion; for example rustc could suggest the wrong import, leading to subtle bugs. As a start introduce the an equivalent of Cell. Almost all of the code was taken from Rust's standard library, while removing unstable features and probably-unnecessary functionality that constitute a large of the original code. A lot of what's left is documentation, as well as unit tests in the form of doctests. These are not yet integrated in "make check" but can be run with "cargo test --doc". Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'rust/qemu-api/src/lib.rs')
-rw-r--r--rust/qemu-api/src/lib.rs1
1 files changed, 1 insertions, 0 deletions
diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
index 440aff3..b04d110 100644
--- a/rust/qemu-api/src/lib.rs
+++ b/rust/qemu-api/src/lib.rs
@@ -8,6 +8,7 @@
pub mod bindings;
pub mod c_str;
+pub mod cell;
pub mod definitions;
pub mod device_class;
pub mod offset_of;