1 // SPDX-License-Identifier: MIT 2 // 3 // This file is based on library/core/src/cell.rs from 4 // Rust 1.82.0. 5 // 6 // Permission is hereby granted, free of charge, to any 7 // person obtaining a copy of this software and associated 8 // documentation files (the "Software"), to deal in the 9 // Software without restriction, including without 10 // limitation the rights to use, copy, modify, merge, 11 // publish, distribute, sublicense, and/or sell copies of 12 // the Software, and to permit persons to whom the Software 13 // is furnished to do so, subject to the following 14 // conditions: 15 // 16 // The above copyright notice and this permission notice 17 // shall be included in all copies or substantial portions 18 // of the Software. 19 // 20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 21 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 22 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 23 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 24 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 25 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 27 // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 // DEALINGS IN THE SOFTWARE. 29 30 //! BQL-protected mutable containers. 31 //! 32 //! Rust memory safety is based on this rule: Given an object `T`, it is only 33 //! possible to have one of the following: 34 //! 35 //! - Having several immutable references (`&T`) to the object (also known as 36 //! **aliasing**). 37 //! - Having one mutable reference (`&mut T`) to the object (also known as 38 //! **mutability**). 39 //! 40 //! This is enforced by the Rust compiler. However, there are situations where 41 //! this rule is not flexible enough. Sometimes it is required to have multiple 42 //! references to an object and yet mutate it. In particular, QEMU objects 43 //! usually have their pointer shared with the "outside world very early in 44 //! their lifetime", for example when they create their 45 //! [`MemoryRegion`s](crate::bindings::MemoryRegion). Therefore, individual 46 //! parts of a device must be made mutable in a controlled manner through the 47 //! use of cell types. 48 //! 49 //! [`BqlCell<T>`] and [`BqlRefCell<T>`] allow doing this via the Big QEMU Lock. 50 //! While they are essentially the same single-threaded primitives that are 51 //! available in `std::cell`, the BQL allows them to be used from a 52 //! multi-threaded context and to share references across threads, while 53 //! maintaining Rust's safety guarantees. For this reason, unlike 54 //! their `std::cell` counterparts, `BqlCell` and `BqlRefCell` implement the 55 //! `Sync` trait. 56 //! 57 //! BQL checks are performed in debug builds but can be optimized away in 58 //! release builds, providing runtime safety during development with no overhead 59 //! in production. 60 //! 61 //! The two provide different ways of handling interior mutability. 62 //! `BqlRefCell` is best suited for data that is primarily accessed by the 63 //! device's own methods, where multiple reads and writes can be grouped within 64 //! a single borrow and a mutable reference can be passed around. Instead, 65 //! [`BqlCell`] is a better choice when sharing small pieces of data with 66 //! external code (especially C code), because it provides simple get/set 67 //! operations that can be used one at a time. 68 //! 69 //! Warning: While `BqlCell` and `BqlRefCell` are similar to their `std::cell` 70 //! counterparts, they are not interchangeable. Using `std::cell` types in 71 //! QEMU device implementations is usually incorrect and can lead to 72 //! thread-safety issues. 73 //! 74 //! ## `BqlCell<T>` 75 //! 76 //! [`BqlCell<T>`] implements interior mutability by moving values in and out of 77 //! the cell. That is, an `&mut T` to the inner value can never be obtained as 78 //! long as the cell is shared. The value itself cannot be directly obtained 79 //! without copying it, cloning it, or replacing it with something else. This 80 //! type provides the following methods, all of which can be called only while 81 //! the BQL is held: 82 //! 83 //! - For types that implement [`Copy`], the [`get`](BqlCell::get) method 84 //! retrieves the current interior value by duplicating it. 85 //! - For types that implement [`Default`], the [`take`](BqlCell::take) method 86 //! replaces the current interior value with [`Default::default()`] and 87 //! returns the replaced value. 88 //! - All types have: 89 //! - [`replace`](BqlCell::replace): replaces the current interior value and 90 //! returns the replaced value. 91 //! - [`set`](BqlCell::set): this method replaces the interior value, 92 //! dropping the replaced value. 93 //! 94 //! ## `BqlRefCell<T>` 95 //! 96 //! [`BqlRefCell<T>`] uses Rust's lifetimes to implement "dynamic borrowing", a 97 //! process whereby one can claim temporary, exclusive, mutable access to the 98 //! inner value: 99 //! 100 //! ```ignore 101 //! fn clear_interrupts(&self, val: u32) { 102 //! // A mutable borrow gives read-write access to the registers 103 //! let mut regs = self.registers.borrow_mut(); 104 //! let old = regs.interrupt_status(); 105 //! regs.update_interrupt_status(old & !val); 106 //! } 107 //! ``` 108 //! 109 //! Borrows for `BqlRefCell<T>`s are tracked at _runtime_, unlike Rust's native 110 //! reference types which are entirely tracked statically, at compile time. 111 //! Multiple immutable borrows are allowed via [`borrow`](BqlRefCell::borrow), 112 //! or a single mutable borrow via [`borrow_mut`](BqlRefCell::borrow_mut). The 113 //! thread will panic if these rules are violated or if the BQL is not held. 114 115 use std::{ 116 cell::{Cell, UnsafeCell}, 117 cmp::Ordering, 118 fmt, 119 marker::PhantomData, 120 mem, 121 ops::{Deref, DerefMut}, 122 ptr::NonNull, 123 }; 124 125 use crate::bindings; 126 127 // TODO: When building doctests do not include the actual BQL, because cargo 128 // does not know how to link them to libqemuutil. This can be fixed by 129 // running rustdoc from "meson test" instead of relying on cargo. 130 pub fn bql_locked() -> bool { 131 // SAFETY: the function does nothing but return a thread-local bool 132 !cfg!(MESON) || unsafe { bindings::bql_locked() } 133 } 134 135 fn bql_block_unlock(increase: bool) { 136 if cfg!(MESON) { 137 // SAFETY: this only adjusts a counter 138 unsafe { 139 bindings::bql_block_unlock(increase); 140 } 141 } 142 } 143 144 /// A mutable memory location that is protected by the Big QEMU Lock. 145 /// 146 /// # Memory layout 147 /// 148 /// `BqlCell<T>` has the same in-memory representation as its inner type `T`. 149 #[repr(transparent)] 150 pub struct BqlCell<T> { 151 value: UnsafeCell<T>, 152 } 153 154 // SAFETY: Same as for std::sync::Mutex. In the end this *is* a Mutex, 155 // except it is stored out-of-line 156 unsafe impl<T: Send> Send for BqlCell<T> {} 157 unsafe impl<T: Send> Sync for BqlCell<T> {} 158 159 impl<T: Copy> Clone for BqlCell<T> { 160 #[inline] 161 fn clone(&self) -> BqlCell<T> { 162 BqlCell::new(self.get()) 163 } 164 } 165 166 impl<T: Default> Default for BqlCell<T> { 167 /// Creates a `BqlCell<T>`, with the `Default` value for T. 168 #[inline] 169 fn default() -> BqlCell<T> { 170 BqlCell::new(Default::default()) 171 } 172 } 173 174 impl<T: PartialEq + Copy> PartialEq for BqlCell<T> { 175 #[inline] 176 fn eq(&self, other: &BqlCell<T>) -> bool { 177 self.get() == other.get() 178 } 179 } 180 181 impl<T: Eq + Copy> Eq for BqlCell<T> {} 182 183 impl<T: PartialOrd + Copy> PartialOrd for BqlCell<T> { 184 #[inline] 185 fn partial_cmp(&self, other: &BqlCell<T>) -> Option<Ordering> { 186 self.get().partial_cmp(&other.get()) 187 } 188 } 189 190 impl<T: Ord + Copy> Ord for BqlCell<T> { 191 #[inline] 192 fn cmp(&self, other: &BqlCell<T>) -> Ordering { 193 self.get().cmp(&other.get()) 194 } 195 } 196 197 impl<T> From<T> for BqlCell<T> { 198 /// Creates a new `BqlCell<T>` containing the given value. 199 fn from(t: T) -> BqlCell<T> { 200 BqlCell::new(t) 201 } 202 } 203 204 impl<T: fmt::Debug + Copy> fmt::Debug for BqlCell<T> { 205 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 206 self.get().fmt(f) 207 } 208 } 209 210 impl<T: fmt::Display + Copy> fmt::Display for BqlCell<T> { 211 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 212 self.get().fmt(f) 213 } 214 } 215 216 impl<T> BqlCell<T> { 217 /// Creates a new `BqlCell` containing the given value. 218 /// 219 /// # Examples 220 /// 221 /// ``` 222 /// use qemu_api::cell::BqlCell; 223 /// 224 /// let c = BqlCell::new(5); 225 /// ``` 226 #[inline] 227 pub const fn new(value: T) -> BqlCell<T> { 228 BqlCell { 229 value: UnsafeCell::new(value), 230 } 231 } 232 233 /// Sets the contained value. 234 /// 235 /// # Examples 236 /// 237 /// ``` 238 /// use qemu_api::cell::BqlCell; 239 /// 240 /// let c = BqlCell::new(5); 241 /// 242 /// c.set(10); 243 /// ``` 244 #[inline] 245 pub fn set(&self, val: T) { 246 self.replace(val); 247 } 248 249 /// Replaces the contained value with `val`, and returns the old contained 250 /// value. 251 /// 252 /// # Examples 253 /// 254 /// ``` 255 /// use qemu_api::cell::BqlCell; 256 /// 257 /// let cell = BqlCell::new(5); 258 /// assert_eq!(cell.get(), 5); 259 /// assert_eq!(cell.replace(10), 5); 260 /// assert_eq!(cell.get(), 10); 261 /// ``` 262 #[inline] 263 pub fn replace(&self, val: T) -> T { 264 assert!(bql_locked()); 265 // SAFETY: This can cause data races if called from multiple threads, 266 // but it won't happen as long as C code accesses the value 267 // under BQL protection only. 268 mem::replace(unsafe { &mut *self.value.get() }, val) 269 } 270 271 /// Unwraps the value, consuming the cell. 272 /// 273 /// # Examples 274 /// 275 /// ``` 276 /// use qemu_api::cell::BqlCell; 277 /// 278 /// let c = BqlCell::new(5); 279 /// let five = c.into_inner(); 280 /// 281 /// assert_eq!(five, 5); 282 /// ``` 283 pub fn into_inner(self) -> T { 284 assert!(bql_locked()); 285 self.value.into_inner() 286 } 287 } 288 289 impl<T: Copy> BqlCell<T> { 290 /// Returns a copy of the contained value. 291 /// 292 /// # Examples 293 /// 294 /// ``` 295 /// use qemu_api::cell::BqlCell; 296 /// 297 /// let c = BqlCell::new(5); 298 /// 299 /// let five = c.get(); 300 /// ``` 301 #[inline] 302 pub fn get(&self) -> T { 303 assert!(bql_locked()); 304 // SAFETY: This can cause data races if called from multiple threads, 305 // but it won't happen as long as C code accesses the value 306 // under BQL protection only. 307 unsafe { *self.value.get() } 308 } 309 } 310 311 impl<T> BqlCell<T> { 312 /// Returns a raw pointer to the underlying data in this cell. 313 /// 314 /// # Examples 315 /// 316 /// ``` 317 /// use qemu_api::cell::BqlCell; 318 /// 319 /// let c = BqlCell::new(5); 320 /// 321 /// let ptr = c.as_ptr(); 322 /// ``` 323 #[inline] 324 pub const fn as_ptr(&self) -> *mut T { 325 self.value.get() 326 } 327 } 328 329 impl<T: Default> BqlCell<T> { 330 /// Takes the value of the cell, leaving `Default::default()` in its place. 331 /// 332 /// # Examples 333 /// 334 /// ``` 335 /// use qemu_api::cell::BqlCell; 336 /// 337 /// let c = BqlCell::new(5); 338 /// let five = c.take(); 339 /// 340 /// assert_eq!(five, 5); 341 /// assert_eq!(c.into_inner(), 0); 342 /// ``` 343 pub fn take(&self) -> T { 344 self.replace(Default::default()) 345 } 346 } 347 348 /// A mutable memory location with dynamically checked borrow rules, 349 /// protected by the Big QEMU Lock. 350 /// 351 /// See the [module-level documentation](self) for more. 352 /// 353 /// # Memory layout 354 /// 355 /// `BqlRefCell<T>` starts with the same in-memory representation as its 356 /// inner type `T`. 357 #[repr(C)] 358 pub struct BqlRefCell<T> { 359 // It is important that this is the first field (which is not the case 360 // for std::cell::BqlRefCell), so that we can use offset_of! on it. 361 // UnsafeCell and repr(C) both prevent usage of niches. 362 value: UnsafeCell<T>, 363 borrow: Cell<BorrowFlag>, 364 // Stores the location of the earliest currently active borrow. 365 // This gets updated whenever we go from having zero borrows 366 // to having a single borrow. When a borrow occurs, this gets included 367 // in the panic message 368 #[cfg(feature = "debug_cell")] 369 borrowed_at: Cell<Option<&'static std::panic::Location<'static>>>, 370 } 371 372 // Positive values represent the number of `BqlRef` active. Negative values 373 // represent the number of `BqlRefMut` active. Right now QEMU's implementation 374 // does not allow to create `BqlRefMut`s that refer to distinct, nonoverlapping 375 // components of a `BqlRefCell` (e.g., different ranges of a slice). 376 // 377 // `BqlRef` and `BqlRefMut` are both two words in size, and so there will likely 378 // never be enough `BqlRef`s or `BqlRefMut`s in existence to overflow half of 379 // the `usize` range. Thus, a `BorrowFlag` will probably never overflow or 380 // underflow. However, this is not a guarantee, as a pathological program could 381 // repeatedly create and then mem::forget `BqlRef`s or `BqlRefMut`s. Thus, all 382 // code must explicitly check for overflow and underflow in order to avoid 383 // unsafety, or at least behave correctly in the event that overflow or 384 // underflow happens (e.g., see BorrowRef::new). 385 type BorrowFlag = isize; 386 const UNUSED: BorrowFlag = 0; 387 388 #[inline(always)] 389 const fn is_writing(x: BorrowFlag) -> bool { 390 x < UNUSED 391 } 392 393 #[inline(always)] 394 const fn is_reading(x: BorrowFlag) -> bool { 395 x > UNUSED 396 } 397 398 impl<T> BqlRefCell<T> { 399 /// Creates a new `BqlRefCell` containing `value`. 400 /// 401 /// # Examples 402 /// 403 /// ``` 404 /// use qemu_api::cell::BqlRefCell; 405 /// 406 /// let c = BqlRefCell::new(5); 407 /// ``` 408 #[inline] 409 pub const fn new(value: T) -> BqlRefCell<T> { 410 BqlRefCell { 411 value: UnsafeCell::new(value), 412 borrow: Cell::new(UNUSED), 413 #[cfg(feature = "debug_cell")] 414 borrowed_at: Cell::new(None), 415 } 416 } 417 } 418 419 // This ensures the panicking code is outlined from `borrow_mut` for 420 // `BqlRefCell`. 421 #[inline(never)] 422 #[cold] 423 #[cfg(feature = "debug_cell")] 424 fn panic_already_borrowed(source: &Cell<Option<&'static std::panic::Location<'static>>>) -> ! { 425 // If a borrow occurred, then we must already have an outstanding borrow, 426 // so `borrowed_at` will be `Some` 427 panic!("already borrowed at {:?}", source.take().unwrap()) 428 } 429 430 #[inline(never)] 431 #[cold] 432 #[cfg(not(feature = "debug_cell"))] 433 fn panic_already_borrowed() -> ! { 434 panic!("already borrowed") 435 } 436 437 impl<T> BqlRefCell<T> { 438 #[inline] 439 #[allow(clippy::unused_self)] 440 fn panic_already_borrowed(&self) -> ! { 441 #[cfg(feature = "debug_cell")] 442 { 443 panic_already_borrowed(&self.borrowed_at) 444 } 445 #[cfg(not(feature = "debug_cell"))] 446 { 447 panic_already_borrowed() 448 } 449 } 450 451 /// Immutably borrows the wrapped value. 452 /// 453 /// The borrow lasts until the returned `BqlRef` exits scope. Multiple 454 /// immutable borrows can be taken out at the same time. 455 /// 456 /// # Panics 457 /// 458 /// Panics if the value is currently mutably borrowed. 459 /// 460 /// # Examples 461 /// 462 /// ``` 463 /// use qemu_api::cell::BqlRefCell; 464 /// 465 /// let c = BqlRefCell::new(5); 466 /// 467 /// let borrowed_five = c.borrow(); 468 /// let borrowed_five2 = c.borrow(); 469 /// ``` 470 /// 471 /// An example of panic: 472 /// 473 /// ```should_panic 474 /// use qemu_api::cell::BqlRefCell; 475 /// 476 /// let c = BqlRefCell::new(5); 477 /// 478 /// let m = c.borrow_mut(); 479 /// let b = c.borrow(); // this causes a panic 480 /// ``` 481 #[inline] 482 #[track_caller] 483 pub fn borrow(&self) -> BqlRef<'_, T> { 484 if let Some(b) = BorrowRef::new(&self.borrow) { 485 // `borrowed_at` is always the *first* active borrow 486 if b.borrow.get() == 1 { 487 #[cfg(feature = "debug_cell")] 488 self.borrowed_at.set(Some(std::panic::Location::caller())); 489 } 490 491 bql_block_unlock(true); 492 493 // SAFETY: `BorrowRef` ensures that there is only immutable access 494 // to the value while borrowed. 495 let value = unsafe { NonNull::new_unchecked(self.value.get()) }; 496 BqlRef { value, borrow: b } 497 } else { 498 self.panic_already_borrowed() 499 } 500 } 501 502 /// Mutably borrows the wrapped value. 503 /// 504 /// The borrow lasts until the returned `BqlRefMut` or all `BqlRefMut`s 505 /// derived from it exit scope. The value cannot be borrowed while this 506 /// borrow is active. 507 /// 508 /// # Panics 509 /// 510 /// Panics if the value is currently borrowed. 511 /// 512 /// # Examples 513 /// 514 /// ``` 515 /// use qemu_api::cell::BqlRefCell; 516 /// 517 /// let c = BqlRefCell::new("hello".to_owned()); 518 /// 519 /// *c.borrow_mut() = "bonjour".to_owned(); 520 /// 521 /// assert_eq!(&*c.borrow(), "bonjour"); 522 /// ``` 523 /// 524 /// An example of panic: 525 /// 526 /// ```should_panic 527 /// use qemu_api::cell::BqlRefCell; 528 /// 529 /// let c = BqlRefCell::new(5); 530 /// let m = c.borrow(); 531 /// 532 /// let b = c.borrow_mut(); // this causes a panic 533 /// ``` 534 #[inline] 535 #[track_caller] 536 pub fn borrow_mut(&self) -> BqlRefMut<'_, T> { 537 if let Some(b) = BorrowRefMut::new(&self.borrow) { 538 #[cfg(feature = "debug_cell")] 539 { 540 self.borrowed_at.set(Some(std::panic::Location::caller())); 541 } 542 543 // SAFETY: this only adjusts a counter 544 bql_block_unlock(true); 545 546 // SAFETY: `BorrowRefMut` guarantees unique access. 547 let value = unsafe { NonNull::new_unchecked(self.value.get()) }; 548 BqlRefMut { 549 value, 550 _borrow: b, 551 marker: PhantomData, 552 } 553 } else { 554 self.panic_already_borrowed() 555 } 556 } 557 558 /// Returns a raw pointer to the underlying data in this cell. 559 /// 560 /// # Examples 561 /// 562 /// ``` 563 /// use qemu_api::cell::BqlRefCell; 564 /// 565 /// let c = BqlRefCell::new(5); 566 /// 567 /// let ptr = c.as_ptr(); 568 /// ``` 569 #[inline] 570 pub const fn as_ptr(&self) -> *mut T { 571 self.value.get() 572 } 573 } 574 575 // SAFETY: Same as for std::sync::Mutex. In the end this is a Mutex that is 576 // stored out-of-line. Even though BqlRefCell includes Cells, they are 577 // themselves protected by the Big QEMU Lock. Furtheremore, the Big QEMU 578 // Lock cannot be released while any borrows is active. 579 unsafe impl<T> Send for BqlRefCell<T> where T: Send {} 580 unsafe impl<T> Sync for BqlRefCell<T> {} 581 582 impl<T: Clone> Clone for BqlRefCell<T> { 583 /// # Panics 584 /// 585 /// Panics if the value is currently mutably borrowed. 586 #[inline] 587 #[track_caller] 588 fn clone(&self) -> BqlRefCell<T> { 589 BqlRefCell::new(self.borrow().clone()) 590 } 591 592 /// # Panics 593 /// 594 /// Panics if `source` is currently mutably borrowed. 595 #[inline] 596 #[track_caller] 597 fn clone_from(&mut self, source: &Self) { 598 self.value.get_mut().clone_from(&source.borrow()) 599 } 600 } 601 602 impl<T: Default> Default for BqlRefCell<T> { 603 /// Creates a `BqlRefCell<T>`, with the `Default` value for T. 604 #[inline] 605 fn default() -> BqlRefCell<T> { 606 BqlRefCell::new(Default::default()) 607 } 608 } 609 610 impl<T: PartialEq> PartialEq for BqlRefCell<T> { 611 /// # Panics 612 /// 613 /// Panics if the value in either `BqlRefCell` is currently mutably 614 /// borrowed. 615 #[inline] 616 fn eq(&self, other: &BqlRefCell<T>) -> bool { 617 *self.borrow() == *other.borrow() 618 } 619 } 620 621 impl<T: Eq> Eq for BqlRefCell<T> {} 622 623 impl<T: PartialOrd> PartialOrd for BqlRefCell<T> { 624 /// # Panics 625 /// 626 /// Panics if the value in either `BqlRefCell` is currently mutably 627 /// borrowed. 628 #[inline] 629 fn partial_cmp(&self, other: &BqlRefCell<T>) -> Option<Ordering> { 630 self.borrow().partial_cmp(&*other.borrow()) 631 } 632 } 633 634 impl<T: Ord> Ord for BqlRefCell<T> { 635 /// # Panics 636 /// 637 /// Panics if the value in either `BqlRefCell` is currently mutably 638 /// borrowed. 639 #[inline] 640 fn cmp(&self, other: &BqlRefCell<T>) -> Ordering { 641 self.borrow().cmp(&*other.borrow()) 642 } 643 } 644 645 impl<T> From<T> for BqlRefCell<T> { 646 /// Creates a new `BqlRefCell<T>` containing the given value. 647 fn from(t: T) -> BqlRefCell<T> { 648 BqlRefCell::new(t) 649 } 650 } 651 652 struct BorrowRef<'b> { 653 borrow: &'b Cell<BorrowFlag>, 654 } 655 656 impl<'b> BorrowRef<'b> { 657 #[inline] 658 fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRef<'b>> { 659 let b = borrow.get().wrapping_add(1); 660 if !is_reading(b) { 661 // Incrementing borrow can result in a non-reading value (<= 0) in these cases: 662 // 1. It was < 0, i.e. there are writing borrows, so we can't allow a read 663 // borrow due to Rust's reference aliasing rules 664 // 2. It was isize::MAX (the max amount of reading borrows) and it overflowed 665 // into isize::MIN (the max amount of writing borrows) so we can't allow an 666 // additional read borrow because isize can't represent so many read borrows 667 // (this can only happen if you mem::forget more than a small constant amount 668 // of `BqlRef`s, which is not good practice) 669 None 670 } else { 671 // Incrementing borrow can result in a reading value (> 0) in these cases: 672 // 1. It was = 0, i.e. it wasn't borrowed, and we are taking the first read 673 // borrow 674 // 2. It was > 0 and < isize::MAX, i.e. there were read borrows, and isize is 675 // large enough to represent having one more read borrow 676 borrow.set(b); 677 Some(BorrowRef { borrow }) 678 } 679 } 680 } 681 682 impl Drop for BorrowRef<'_> { 683 #[inline] 684 fn drop(&mut self) { 685 let borrow = self.borrow.get(); 686 debug_assert!(is_reading(borrow)); 687 self.borrow.set(borrow - 1); 688 bql_block_unlock(false) 689 } 690 } 691 692 impl Clone for BorrowRef<'_> { 693 #[inline] 694 fn clone(&self) -> Self { 695 BorrowRef::new(self.borrow).unwrap() 696 } 697 } 698 699 /// Wraps a borrowed reference to a value in a `BqlRefCell` box. 700 /// A wrapper type for an immutably borrowed value from a `BqlRefCell<T>`. 701 /// 702 /// See the [module-level documentation](self) for more. 703 pub struct BqlRef<'b, T: 'b> { 704 // NB: we use a pointer instead of `&'b T` to avoid `noalias` violations, because a 705 // `BqlRef` argument doesn't hold immutability for its whole scope, only until it drops. 706 // `NonNull` is also covariant over `T`, just like we would have with `&T`. 707 value: NonNull<T>, 708 borrow: BorrowRef<'b>, 709 } 710 711 impl<T> Deref for BqlRef<'_, T> { 712 type Target = T; 713 714 #[inline] 715 fn deref(&self) -> &T { 716 // SAFETY: the value is accessible as long as we hold our borrow. 717 unsafe { self.value.as_ref() } 718 } 719 } 720 721 impl<'b, T> BqlRef<'b, T> { 722 /// Copies a `BqlRef`. 723 /// 724 /// The `BqlRefCell` is already immutably borrowed, so this cannot fail. 725 /// 726 /// This is an associated function that needs to be used as 727 /// `BqlRef::clone(...)`. A `Clone` implementation or a method would 728 /// interfere with the widespread use of `r.borrow().clone()` to clone 729 /// the contents of a `BqlRefCell`. 730 #[must_use] 731 #[inline] 732 #[allow(clippy::should_implement_trait)] 733 pub fn clone(orig: &BqlRef<'b, T>) -> BqlRef<'b, T> { 734 BqlRef { 735 value: orig.value, 736 borrow: orig.borrow.clone(), 737 } 738 } 739 } 740 741 impl<T: fmt::Debug> fmt::Debug for BqlRef<'_, T> { 742 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 743 (**self).fmt(f) 744 } 745 } 746 747 impl<T: fmt::Display> fmt::Display for BqlRef<'_, T> { 748 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 749 (**self).fmt(f) 750 } 751 } 752 753 struct BorrowRefMut<'b> { 754 borrow: &'b Cell<BorrowFlag>, 755 } 756 757 impl<'b> BorrowRefMut<'b> { 758 #[inline] 759 fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRefMut<'b>> { 760 // There must currently be no existing references when borrow_mut() is 761 // called, so we explicitly only allow going from UNUSED to UNUSED - 1. 762 match borrow.get() { 763 UNUSED => { 764 borrow.set(UNUSED - 1); 765 Some(BorrowRefMut { borrow }) 766 } 767 _ => None, 768 } 769 } 770 } 771 772 impl Drop for BorrowRefMut<'_> { 773 #[inline] 774 fn drop(&mut self) { 775 let borrow = self.borrow.get(); 776 debug_assert!(is_writing(borrow)); 777 self.borrow.set(borrow + 1); 778 bql_block_unlock(false) 779 } 780 } 781 782 /// A wrapper type for a mutably borrowed value from a `BqlRefCell<T>`. 783 /// 784 /// See the [module-level documentation](self) for more. 785 pub struct BqlRefMut<'b, T: 'b> { 786 // NB: we use a pointer instead of `&'b mut T` to avoid `noalias` violations, because a 787 // `BqlRefMut` argument doesn't hold exclusivity for its whole scope, only until it drops. 788 value: NonNull<T>, 789 _borrow: BorrowRefMut<'b>, 790 // `NonNull` is covariant over `T`, so we need to reintroduce invariance. 791 marker: PhantomData<&'b mut T>, 792 } 793 794 impl<T> Deref for BqlRefMut<'_, T> { 795 type Target = T; 796 797 #[inline] 798 fn deref(&self) -> &T { 799 // SAFETY: the value is accessible as long as we hold our borrow. 800 unsafe { self.value.as_ref() } 801 } 802 } 803 804 impl<T> DerefMut for BqlRefMut<'_, T> { 805 #[inline] 806 fn deref_mut(&mut self) -> &mut T { 807 // SAFETY: the value is accessible as long as we hold our borrow. 808 unsafe { self.value.as_mut() } 809 } 810 } 811 812 impl<T: fmt::Debug> fmt::Debug for BqlRefMut<'_, T> { 813 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 814 (**self).fmt(f) 815 } 816 } 817 818 impl<T: fmt::Display> fmt::Display for BqlRefMut<'_, T> { 819 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 820 (**self).fmt(f) 821 } 822 } 823