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 /// An internal function that is used by doctests. 128 pub fn bql_start_test() { 129 if cfg!(MESON) { 130 // SAFETY: integration tests are run with --test-threads=1, while 131 // unit tests and doctests are not multithreaded and do not have 132 // any BQL-protected data. Just set bql_locked to true. 133 unsafe { 134 bindings::rust_bql_mock_lock(); 135 } 136 } 137 } 138 139 pub fn bql_locked() -> bool { 140 // SAFETY: the function does nothing but return a thread-local bool 141 !cfg!(MESON) || unsafe { bindings::bql_locked() } 142 } 143 144 fn bql_block_unlock(increase: bool) { 145 if cfg!(MESON) { 146 // SAFETY: this only adjusts a counter 147 unsafe { 148 bindings::bql_block_unlock(increase); 149 } 150 } 151 } 152 153 /// A mutable memory location that is protected by the Big QEMU Lock. 154 /// 155 /// # Memory layout 156 /// 157 /// `BqlCell<T>` has the same in-memory representation as its inner type `T`. 158 #[repr(transparent)] 159 pub struct BqlCell<T> { 160 value: UnsafeCell<T>, 161 } 162 163 // SAFETY: Same as for std::sync::Mutex. In the end this *is* a Mutex, 164 // except it is stored out-of-line 165 unsafe impl<T: Send> Send for BqlCell<T> {} 166 unsafe impl<T: Send> Sync for BqlCell<T> {} 167 168 impl<T: Copy> Clone for BqlCell<T> { 169 #[inline] 170 fn clone(&self) -> BqlCell<T> { 171 BqlCell::new(self.get()) 172 } 173 } 174 175 impl<T: Default> Default for BqlCell<T> { 176 /// Creates a `BqlCell<T>`, with the `Default` value for T. 177 #[inline] 178 fn default() -> BqlCell<T> { 179 BqlCell::new(Default::default()) 180 } 181 } 182 183 impl<T: PartialEq + Copy> PartialEq for BqlCell<T> { 184 #[inline] 185 fn eq(&self, other: &BqlCell<T>) -> bool { 186 self.get() == other.get() 187 } 188 } 189 190 impl<T: Eq + Copy> Eq for BqlCell<T> {} 191 192 impl<T: PartialOrd + Copy> PartialOrd for BqlCell<T> { 193 #[inline] 194 fn partial_cmp(&self, other: &BqlCell<T>) -> Option<Ordering> { 195 self.get().partial_cmp(&other.get()) 196 } 197 } 198 199 impl<T: Ord + Copy> Ord for BqlCell<T> { 200 #[inline] 201 fn cmp(&self, other: &BqlCell<T>) -> Ordering { 202 self.get().cmp(&other.get()) 203 } 204 } 205 206 impl<T> From<T> for BqlCell<T> { 207 /// Creates a new `BqlCell<T>` containing the given value. 208 fn from(t: T) -> BqlCell<T> { 209 BqlCell::new(t) 210 } 211 } 212 213 impl<T: fmt::Debug + Copy> fmt::Debug for BqlCell<T> { 214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 215 self.get().fmt(f) 216 } 217 } 218 219 impl<T: fmt::Display + Copy> fmt::Display for BqlCell<T> { 220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 221 self.get().fmt(f) 222 } 223 } 224 225 impl<T> BqlCell<T> { 226 /// Creates a new `BqlCell` containing the given value. 227 /// 228 /// # Examples 229 /// 230 /// ``` 231 /// use qemu_api::cell::BqlCell; 232 /// # qemu_api::cell::bql_start_test(); 233 /// 234 /// let c = BqlCell::new(5); 235 /// ``` 236 #[inline] 237 pub const fn new(value: T) -> BqlCell<T> { 238 BqlCell { 239 value: UnsafeCell::new(value), 240 } 241 } 242 243 /// Sets the contained value. 244 /// 245 /// # Examples 246 /// 247 /// ``` 248 /// use qemu_api::cell::BqlCell; 249 /// # qemu_api::cell::bql_start_test(); 250 /// 251 /// let c = BqlCell::new(5); 252 /// 253 /// c.set(10); 254 /// ``` 255 #[inline] 256 pub fn set(&self, val: T) { 257 self.replace(val); 258 } 259 260 /// Replaces the contained value with `val`, and returns the old contained 261 /// value. 262 /// 263 /// # Examples 264 /// 265 /// ``` 266 /// use qemu_api::cell::BqlCell; 267 /// # qemu_api::cell::bql_start_test(); 268 /// 269 /// let cell = BqlCell::new(5); 270 /// assert_eq!(cell.get(), 5); 271 /// assert_eq!(cell.replace(10), 5); 272 /// assert_eq!(cell.get(), 10); 273 /// ``` 274 #[inline] 275 pub fn replace(&self, val: T) -> T { 276 assert!(bql_locked()); 277 // SAFETY: This can cause data races if called from multiple threads, 278 // but it won't happen as long as C code accesses the value 279 // under BQL protection only. 280 mem::replace(unsafe { &mut *self.value.get() }, val) 281 } 282 283 /// Unwraps the value, consuming the cell. 284 /// 285 /// # Examples 286 /// 287 /// ``` 288 /// use qemu_api::cell::BqlCell; 289 /// # qemu_api::cell::bql_start_test(); 290 /// 291 /// let c = BqlCell::new(5); 292 /// let five = c.into_inner(); 293 /// 294 /// assert_eq!(five, 5); 295 /// ``` 296 pub fn into_inner(self) -> T { 297 assert!(bql_locked()); 298 self.value.into_inner() 299 } 300 } 301 302 impl<T: Copy> BqlCell<T> { 303 /// Returns a copy of the contained value. 304 /// 305 /// # Examples 306 /// 307 /// ``` 308 /// use qemu_api::cell::BqlCell; 309 /// # qemu_api::cell::bql_start_test(); 310 /// 311 /// let c = BqlCell::new(5); 312 /// 313 /// let five = c.get(); 314 /// ``` 315 #[inline] 316 pub fn get(&self) -> T { 317 assert!(bql_locked()); 318 // SAFETY: This can cause data races if called from multiple threads, 319 // but it won't happen as long as C code accesses the value 320 // under BQL protection only. 321 unsafe { *self.value.get() } 322 } 323 } 324 325 impl<T> BqlCell<T> { 326 /// Returns a raw pointer to the underlying data in this cell. 327 /// 328 /// # Examples 329 /// 330 /// ``` 331 /// use qemu_api::cell::BqlCell; 332 /// # qemu_api::cell::bql_start_test(); 333 /// 334 /// let c = BqlCell::new(5); 335 /// 336 /// let ptr = c.as_ptr(); 337 /// ``` 338 #[inline] 339 pub const fn as_ptr(&self) -> *mut T { 340 self.value.get() 341 } 342 } 343 344 impl<T: Default> BqlCell<T> { 345 /// Takes the value of the cell, leaving `Default::default()` in its place. 346 /// 347 /// # Examples 348 /// 349 /// ``` 350 /// use qemu_api::cell::BqlCell; 351 /// # qemu_api::cell::bql_start_test(); 352 /// 353 /// let c = BqlCell::new(5); 354 /// let five = c.take(); 355 /// 356 /// assert_eq!(five, 5); 357 /// assert_eq!(c.into_inner(), 0); 358 /// ``` 359 pub fn take(&self) -> T { 360 self.replace(Default::default()) 361 } 362 } 363 364 /// A mutable memory location with dynamically checked borrow rules, 365 /// protected by the Big QEMU Lock. 366 /// 367 /// See the [module-level documentation](self) for more. 368 /// 369 /// # Memory layout 370 /// 371 /// `BqlRefCell<T>` starts with the same in-memory representation as its 372 /// inner type `T`. 373 #[repr(C)] 374 pub struct BqlRefCell<T> { 375 // It is important that this is the first field (which is not the case 376 // for std::cell::BqlRefCell), so that we can use offset_of! on it. 377 // UnsafeCell and repr(C) both prevent usage of niches. 378 value: UnsafeCell<T>, 379 borrow: Cell<BorrowFlag>, 380 // Stores the location of the earliest currently active borrow. 381 // This gets updated whenever we go from having zero borrows 382 // to having a single borrow. When a borrow occurs, this gets included 383 // in the panic message 384 #[cfg(feature = "debug_cell")] 385 borrowed_at: Cell<Option<&'static std::panic::Location<'static>>>, 386 } 387 388 // Positive values represent the number of `BqlRef` active. Negative values 389 // represent the number of `BqlRefMut` active. Right now QEMU's implementation 390 // does not allow to create `BqlRefMut`s that refer to distinct, nonoverlapping 391 // components of a `BqlRefCell` (e.g., different ranges of a slice). 392 // 393 // `BqlRef` and `BqlRefMut` are both two words in size, and so there will likely 394 // never be enough `BqlRef`s or `BqlRefMut`s in existence to overflow half of 395 // the `usize` range. Thus, a `BorrowFlag` will probably never overflow or 396 // underflow. However, this is not a guarantee, as a pathological program could 397 // repeatedly create and then mem::forget `BqlRef`s or `BqlRefMut`s. Thus, all 398 // code must explicitly check for overflow and underflow in order to avoid 399 // unsafety, or at least behave correctly in the event that overflow or 400 // underflow happens (e.g., see BorrowRef::new). 401 type BorrowFlag = isize; 402 const UNUSED: BorrowFlag = 0; 403 404 #[inline(always)] 405 const fn is_writing(x: BorrowFlag) -> bool { 406 x < UNUSED 407 } 408 409 #[inline(always)] 410 const fn is_reading(x: BorrowFlag) -> bool { 411 x > UNUSED 412 } 413 414 impl<T> BqlRefCell<T> { 415 /// Creates a new `BqlRefCell` containing `value`. 416 /// 417 /// # Examples 418 /// 419 /// ``` 420 /// use qemu_api::cell::BqlRefCell; 421 /// 422 /// let c = BqlRefCell::new(5); 423 /// ``` 424 #[inline] 425 pub const fn new(value: T) -> BqlRefCell<T> { 426 BqlRefCell { 427 value: UnsafeCell::new(value), 428 borrow: Cell::new(UNUSED), 429 #[cfg(feature = "debug_cell")] 430 borrowed_at: Cell::new(None), 431 } 432 } 433 } 434 435 // This ensures the panicking code is outlined from `borrow_mut` for 436 // `BqlRefCell`. 437 #[inline(never)] 438 #[cold] 439 #[cfg(feature = "debug_cell")] 440 fn panic_already_borrowed(source: &Cell<Option<&'static std::panic::Location<'static>>>) -> ! { 441 // If a borrow occurred, then we must already have an outstanding borrow, 442 // so `borrowed_at` will be `Some` 443 panic!("already borrowed at {:?}", source.take().unwrap()) 444 } 445 446 #[inline(never)] 447 #[cold] 448 #[cfg(not(feature = "debug_cell"))] 449 fn panic_already_borrowed() -> ! { 450 panic!("already borrowed") 451 } 452 453 impl<T> BqlRefCell<T> { 454 #[inline] 455 #[allow(clippy::unused_self)] 456 fn panic_already_borrowed(&self) -> ! { 457 #[cfg(feature = "debug_cell")] 458 { 459 panic_already_borrowed(&self.borrowed_at) 460 } 461 #[cfg(not(feature = "debug_cell"))] 462 { 463 panic_already_borrowed() 464 } 465 } 466 467 /// Immutably borrows the wrapped value. 468 /// 469 /// The borrow lasts until the returned `BqlRef` exits scope. Multiple 470 /// immutable borrows can be taken out at the same time. 471 /// 472 /// # Panics 473 /// 474 /// Panics if the value is currently mutably borrowed. 475 /// 476 /// # Examples 477 /// 478 /// ``` 479 /// use qemu_api::cell::BqlRefCell; 480 /// # qemu_api::cell::bql_start_test(); 481 /// 482 /// let c = BqlRefCell::new(5); 483 /// 484 /// let borrowed_five = c.borrow(); 485 /// let borrowed_five2 = c.borrow(); 486 /// ``` 487 /// 488 /// An example of panic: 489 /// 490 /// ```should_panic 491 /// use qemu_api::cell::BqlRefCell; 492 /// # qemu_api::cell::bql_start_test(); 493 /// 494 /// let c = BqlRefCell::new(5); 495 /// 496 /// let m = c.borrow_mut(); 497 /// let b = c.borrow(); // this causes a panic 498 /// ``` 499 #[inline] 500 #[track_caller] 501 pub fn borrow(&self) -> BqlRef<'_, T> { 502 if let Some(b) = BorrowRef::new(&self.borrow) { 503 // `borrowed_at` is always the *first* active borrow 504 if b.borrow.get() == 1 { 505 #[cfg(feature = "debug_cell")] 506 self.borrowed_at.set(Some(std::panic::Location::caller())); 507 } 508 509 bql_block_unlock(true); 510 511 // SAFETY: `BorrowRef` ensures that there is only immutable access 512 // to the value while borrowed. 513 let value = unsafe { NonNull::new_unchecked(self.value.get()) }; 514 BqlRef { value, borrow: b } 515 } else { 516 self.panic_already_borrowed() 517 } 518 } 519 520 /// Mutably borrows the wrapped value. 521 /// 522 /// The borrow lasts until the returned `BqlRefMut` or all `BqlRefMut`s 523 /// derived from it exit scope. The value cannot be borrowed while this 524 /// borrow is active. 525 /// 526 /// # Panics 527 /// 528 /// Panics if the value is currently borrowed. 529 /// 530 /// # Examples 531 /// 532 /// ``` 533 /// use qemu_api::cell::BqlRefCell; 534 /// # qemu_api::cell::bql_start_test(); 535 /// 536 /// let c = BqlRefCell::new("hello".to_owned()); 537 /// 538 /// *c.borrow_mut() = "bonjour".to_owned(); 539 /// 540 /// assert_eq!(&*c.borrow(), "bonjour"); 541 /// ``` 542 /// 543 /// An example of panic: 544 /// 545 /// ```should_panic 546 /// use qemu_api::cell::BqlRefCell; 547 /// # qemu_api::cell::bql_start_test(); 548 /// 549 /// let c = BqlRefCell::new(5); 550 /// let m = c.borrow(); 551 /// 552 /// let b = c.borrow_mut(); // this causes a panic 553 /// ``` 554 #[inline] 555 #[track_caller] 556 pub fn borrow_mut(&self) -> BqlRefMut<'_, T> { 557 if let Some(b) = BorrowRefMut::new(&self.borrow) { 558 #[cfg(feature = "debug_cell")] 559 { 560 self.borrowed_at.set(Some(std::panic::Location::caller())); 561 } 562 563 // SAFETY: this only adjusts a counter 564 bql_block_unlock(true); 565 566 // SAFETY: `BorrowRefMut` guarantees unique access. 567 let value = unsafe { NonNull::new_unchecked(self.value.get()) }; 568 BqlRefMut { 569 value, 570 _borrow: b, 571 marker: PhantomData, 572 } 573 } else { 574 self.panic_already_borrowed() 575 } 576 } 577 578 /// Returns a raw pointer to the underlying data in this cell. 579 /// 580 /// # Examples 581 /// 582 /// ``` 583 /// use qemu_api::cell::BqlRefCell; 584 /// 585 /// let c = BqlRefCell::new(5); 586 /// 587 /// let ptr = c.as_ptr(); 588 /// ``` 589 #[inline] 590 pub const fn as_ptr(&self) -> *mut T { 591 self.value.get() 592 } 593 } 594 595 // SAFETY: Same as for std::sync::Mutex. In the end this is a Mutex that is 596 // stored out-of-line. Even though BqlRefCell includes Cells, they are 597 // themselves protected by the Big QEMU Lock. Furtheremore, the Big QEMU 598 // Lock cannot be released while any borrows is active. 599 unsafe impl<T> Send for BqlRefCell<T> where T: Send {} 600 unsafe impl<T> Sync for BqlRefCell<T> {} 601 602 impl<T: Clone> Clone for BqlRefCell<T> { 603 /// # Panics 604 /// 605 /// Panics if the value is currently mutably borrowed. 606 #[inline] 607 #[track_caller] 608 fn clone(&self) -> BqlRefCell<T> { 609 BqlRefCell::new(self.borrow().clone()) 610 } 611 612 /// # Panics 613 /// 614 /// Panics if `source` is currently mutably borrowed. 615 #[inline] 616 #[track_caller] 617 fn clone_from(&mut self, source: &Self) { 618 self.value.get_mut().clone_from(&source.borrow()) 619 } 620 } 621 622 impl<T: Default> Default for BqlRefCell<T> { 623 /// Creates a `BqlRefCell<T>`, with the `Default` value for T. 624 #[inline] 625 fn default() -> BqlRefCell<T> { 626 BqlRefCell::new(Default::default()) 627 } 628 } 629 630 impl<T: PartialEq> PartialEq for BqlRefCell<T> { 631 /// # Panics 632 /// 633 /// Panics if the value in either `BqlRefCell` is currently mutably 634 /// borrowed. 635 #[inline] 636 fn eq(&self, other: &BqlRefCell<T>) -> bool { 637 *self.borrow() == *other.borrow() 638 } 639 } 640 641 impl<T: Eq> Eq for BqlRefCell<T> {} 642 643 impl<T: PartialOrd> PartialOrd for BqlRefCell<T> { 644 /// # Panics 645 /// 646 /// Panics if the value in either `BqlRefCell` is currently mutably 647 /// borrowed. 648 #[inline] 649 fn partial_cmp(&self, other: &BqlRefCell<T>) -> Option<Ordering> { 650 self.borrow().partial_cmp(&*other.borrow()) 651 } 652 } 653 654 impl<T: Ord> Ord for BqlRefCell<T> { 655 /// # Panics 656 /// 657 /// Panics if the value in either `BqlRefCell` is currently mutably 658 /// borrowed. 659 #[inline] 660 fn cmp(&self, other: &BqlRefCell<T>) -> Ordering { 661 self.borrow().cmp(&*other.borrow()) 662 } 663 } 664 665 impl<T> From<T> for BqlRefCell<T> { 666 /// Creates a new `BqlRefCell<T>` containing the given value. 667 fn from(t: T) -> BqlRefCell<T> { 668 BqlRefCell::new(t) 669 } 670 } 671 672 struct BorrowRef<'b> { 673 borrow: &'b Cell<BorrowFlag>, 674 } 675 676 impl<'b> BorrowRef<'b> { 677 #[inline] 678 fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRef<'b>> { 679 let b = borrow.get().wrapping_add(1); 680 if !is_reading(b) { 681 // Incrementing borrow can result in a non-reading value (<= 0) in these cases: 682 // 1. It was < 0, i.e. there are writing borrows, so we can't allow a read 683 // borrow due to Rust's reference aliasing rules 684 // 2. It was isize::MAX (the max amount of reading borrows) and it overflowed 685 // into isize::MIN (the max amount of writing borrows) so we can't allow an 686 // additional read borrow because isize can't represent so many read borrows 687 // (this can only happen if you mem::forget more than a small constant amount 688 // of `BqlRef`s, which is not good practice) 689 None 690 } else { 691 // Incrementing borrow can result in a reading value (> 0) in these cases: 692 // 1. It was = 0, i.e. it wasn't borrowed, and we are taking the first read 693 // borrow 694 // 2. It was > 0 and < isize::MAX, i.e. there were read borrows, and isize is 695 // large enough to represent having one more read borrow 696 borrow.set(b); 697 Some(BorrowRef { borrow }) 698 } 699 } 700 } 701 702 impl Drop for BorrowRef<'_> { 703 #[inline] 704 fn drop(&mut self) { 705 let borrow = self.borrow.get(); 706 debug_assert!(is_reading(borrow)); 707 self.borrow.set(borrow - 1); 708 bql_block_unlock(false) 709 } 710 } 711 712 impl Clone for BorrowRef<'_> { 713 #[inline] 714 fn clone(&self) -> Self { 715 BorrowRef::new(self.borrow).unwrap() 716 } 717 } 718 719 /// Wraps a borrowed reference to a value in a `BqlRefCell` box. 720 /// A wrapper type for an immutably borrowed value from a `BqlRefCell<T>`. 721 /// 722 /// See the [module-level documentation](self) for more. 723 pub struct BqlRef<'b, T: 'b> { 724 // NB: we use a pointer instead of `&'b T` to avoid `noalias` violations, because a 725 // `BqlRef` argument doesn't hold immutability for its whole scope, only until it drops. 726 // `NonNull` is also covariant over `T`, just like we would have with `&T`. 727 value: NonNull<T>, 728 borrow: BorrowRef<'b>, 729 } 730 731 impl<T> Deref for BqlRef<'_, T> { 732 type Target = T; 733 734 #[inline] 735 fn deref(&self) -> &T { 736 // SAFETY: the value is accessible as long as we hold our borrow. 737 unsafe { self.value.as_ref() } 738 } 739 } 740 741 impl<'b, T> BqlRef<'b, T> { 742 /// Copies a `BqlRef`. 743 /// 744 /// The `BqlRefCell` is already immutably borrowed, so this cannot fail. 745 /// 746 /// This is an associated function that needs to be used as 747 /// `BqlRef::clone(...)`. A `Clone` implementation or a method would 748 /// interfere with the widespread use of `r.borrow().clone()` to clone 749 /// the contents of a `BqlRefCell`. 750 #[must_use] 751 #[inline] 752 #[allow(clippy::should_implement_trait)] 753 pub fn clone(orig: &BqlRef<'b, T>) -> BqlRef<'b, T> { 754 BqlRef { 755 value: orig.value, 756 borrow: orig.borrow.clone(), 757 } 758 } 759 } 760 761 impl<T: fmt::Debug> fmt::Debug for BqlRef<'_, T> { 762 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 763 (**self).fmt(f) 764 } 765 } 766 767 impl<T: fmt::Display> fmt::Display for BqlRef<'_, T> { 768 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 769 (**self).fmt(f) 770 } 771 } 772 773 struct BorrowRefMut<'b> { 774 borrow: &'b Cell<BorrowFlag>, 775 } 776 777 impl<'b> BorrowRefMut<'b> { 778 #[inline] 779 fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRefMut<'b>> { 780 // There must currently be no existing references when borrow_mut() is 781 // called, so we explicitly only allow going from UNUSED to UNUSED - 1. 782 match borrow.get() { 783 UNUSED => { 784 borrow.set(UNUSED - 1); 785 Some(BorrowRefMut { borrow }) 786 } 787 _ => None, 788 } 789 } 790 } 791 792 impl Drop for BorrowRefMut<'_> { 793 #[inline] 794 fn drop(&mut self) { 795 let borrow = self.borrow.get(); 796 debug_assert!(is_writing(borrow)); 797 self.borrow.set(borrow + 1); 798 bql_block_unlock(false) 799 } 800 } 801 802 /// A wrapper type for a mutably borrowed value from a `BqlRefCell<T>`. 803 /// 804 /// See the [module-level documentation](self) for more. 805 pub struct BqlRefMut<'b, T: 'b> { 806 // NB: we use a pointer instead of `&'b mut T` to avoid `noalias` violations, because a 807 // `BqlRefMut` argument doesn't hold exclusivity for its whole scope, only until it drops. 808 value: NonNull<T>, 809 _borrow: BorrowRefMut<'b>, 810 // `NonNull` is covariant over `T`, so we need to reintroduce invariance. 811 marker: PhantomData<&'b mut T>, 812 } 813 814 impl<T> Deref for BqlRefMut<'_, T> { 815 type Target = T; 816 817 #[inline] 818 fn deref(&self) -> &T { 819 // SAFETY: the value is accessible as long as we hold our borrow. 820 unsafe { self.value.as_ref() } 821 } 822 } 823 824 impl<T> DerefMut for BqlRefMut<'_, T> { 825 #[inline] 826 fn deref_mut(&mut self) -> &mut T { 827 // SAFETY: the value is accessible as long as we hold our borrow. 828 unsafe { self.value.as_mut() } 829 } 830 } 831 832 impl<T: fmt::Debug> fmt::Debug for BqlRefMut<'_, T> { 833 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 834 (**self).fmt(f) 835 } 836 } 837 838 impl<T: fmt::Display> fmt::Display for BqlRefMut<'_, T> { 839 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 840 (**self).fmt(f) 841 } 842 } 843