1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
// SPDX-License-Identifier: MPL-2.0
#![allow(dead_code)]
use alloc::sync::Arc;
use core::{
cell::UnsafeCell,
fmt,
ops::{Deref, DerefMut},
sync::atomic::{
AtomicUsize,
Ordering::{AcqRel, Acquire, Relaxed, Release},
},
};
use crate::{
task::{disable_preempt, DisablePreemptGuard},
trap::{disable_local, DisabledLocalIrqGuard},
};
/// Spin-based Read-write Lock
///
/// # Overview
///
/// This lock allows for multiple readers, or at most one writer to access
/// at any point in time. The writer of this lock has exclusive access to
/// modify the underlying data, while the readers are allowed shared and
/// read-only access.
///
/// The writing and reading portions cannot be active simultaneously, when
/// one portion is in progress, the other portion will spin-wait. This is
/// suitable for scenarios where the lock is expected to be held for short
/// periods of time, and the overhead of context switching is higher than
/// the cost of spinning.
///
/// The lock provides methods to safely acquire locks with interrupts
/// disabled, preventing deadlocks in scenarios where locks are used within
/// interrupt handlers.
///
/// In addition to traditional read and write locks, this implementation
/// provides the upgradeable read lock (`upread lock`). The `upread lock`
/// can be upgraded to write locks atomically, useful in scenarios
/// where a decision to write is made after reading.
///
/// The type parameter `T` represents the data that this lock is protecting.
/// It is necessary for `T` to satisfy [`Send`] to be shared across tasks and
/// [`Sync`] to permit concurrent access via readers. The [`Deref`] method (and
/// [`DerefMut`] for the writer) is implemented for the RAII guards returned
/// by the locking methods, which allows for the access to the protected data
/// while the lock is held.
///
/// # Usage
/// The lock can be used in scenarios where data needs to be read frequently
/// but written to occasionally.
///
/// Use `upread lock` in scenarios where related checking is performed before
/// modification to effectively avoid deadlocks and improve efficiency.
///
/// This lock should not be used in scenarios where lock-holding times are
/// long as it can lead to CPU resource wastage due to spinning.
///
/// # Safety
///
/// Use interrupt-disabled version methods when dealing with interrupt-related read-write locks,
/// as nested interrupts may lead to a deadlock if not properly handled.
///
/// # Examples
///
/// ```
/// use ostd::sync::RwLock;
///
/// let lock = RwLock::new(5)
///
/// // many read locks can be held at once
/// {
/// let r1 = lock.read();
/// let r2 = lock.read();
/// assert_eq!(*r1, 5);
/// assert_eq!(*r2, 5);
///
/// // Upgradeable read lock can share access to data with read locks
/// let r3 = lock.upread();
/// assert_eq!(*r3, 5);
/// drop(r1);
/// drop(r2);
/// // read locks are dropped at this point
///
/// // An upread lock can only be upgraded successfully after all the
/// // read locks are released, otherwise it will spin-wait.
/// let mut w1 = r3.upgrade();
/// *w1 += 1;
/// assert_eq!(*w1, 6);
/// } // upread lock are dropped at this point
///
/// {
/// // Only one write lock can be held at a time
/// let mut w2 = lock.write();
/// *w2 += 1;
/// assert_eq!(*w2, 7);
/// } // write lock is dropped at this point
/// ```
pub struct RwLock<T: ?Sized> {
/// The internal representation of the lock state is as follows:
/// - **Bit 63:** Writer lock.
/// - **Bit 62:** Upgradeable reader lock.
/// - **Bit 61:** Indicates if an upgradeable reader is being upgraded.
/// - **Bits 60-0:** Reader lock count.
lock: AtomicUsize,
val: UnsafeCell<T>,
}
const READER: usize = 1;
const WRITER: usize = 1 << (usize::BITS - 1);
const UPGRADEABLE_READER: usize = 1 << (usize::BITS - 2);
const BEING_UPGRADED: usize = 1 << (usize::BITS - 3);
const MAX_READER: usize = 1 << (usize::BITS - 4);
impl<T> RwLock<T> {
/// Creates a new spin-based read-write lock with an initial value.
pub const fn new(val: T) -> Self {
Self {
val: UnsafeCell::new(val),
lock: AtomicUsize::new(0),
}
}
}
impl<T: ?Sized> RwLock<T> {
/// Acquires a read lock while disabling the local IRQs and spin-wait
/// until it can be acquired.
///
/// The calling thread will spin-wait until there are no writers or
/// upgrading upreaders present. There is no guarantee for the order
/// in which other readers or writers waiting simultaneously will
/// obtain the lock. Once this lock is acquired, the calling thread
/// will not be interrupted.
pub fn read_irq_disabled(&self) -> RwLockReadGuard<T> {
loop {
if let Some(readguard) = self.try_read_irq_disabled() {
return readguard;
} else {
core::hint::spin_loop();
}
}
}
/// Acquires a write lock while disabling the local IRQs and spin-wait
/// until it can be acquired.
///
/// The calling thread will spin-wait until there are no other writers,
/// upreaders or readers present. There is no guarantee for the order
/// in which other readers or writers waiting simultaneously will
/// obtain the lock. Once this lock is acquired, the calling thread
/// will not be interrupted.
pub fn write_irq_disabled(&self) -> RwLockWriteGuard<T> {
loop {
if let Some(writeguard) = self.try_write_irq_disabled() {
return writeguard;
} else {
core::hint::spin_loop();
}
}
}
/// Acquires an upgradeable reader (upreader) while disabling local IRQs
/// and spin-wait until it can be acquired.
///
/// The calling thread will spin-wait until there are no other writers,
/// or upreaders. There is no guarantee for the order in which other
/// readers or writers waiting simultaneously will obtain the lock. Once
/// this lock is acquired, the calling thread will not be interrupted.
///
/// Upreader will not block new readers until it tries to upgrade. Upreader
/// and reader do not differ before invoking the upgread method. However,
/// only one upreader can exist at any time to avoid deadlock in the
/// upgread method.
pub fn upread_irq_disabled(&self) -> RwLockUpgradeableGuard<T> {
loop {
if let Some(guard) = self.try_upread_irq_disabled() {
return guard;
} else {
core::hint::spin_loop();
}
}
}
/// Attempts to acquire a read lock while disabling local IRQs.
///
/// This function will never spin-wait and will return immediately. When
/// multiple readers or writers attempt to acquire the lock, this method
/// does not guarantee any order. Interrupts will automatically be restored
/// when acquiring fails.
pub fn try_read_irq_disabled(&self) -> Option<RwLockReadGuard<T>> {
let irq_guard = disable_local();
let lock = self.lock.fetch_add(READER, Acquire);
if lock & (WRITER | MAX_READER | BEING_UPGRADED) == 0 {
Some(RwLockReadGuard {
inner: self,
inner_guard: InnerGuard::IrqGuard(irq_guard),
})
} else {
self.lock.fetch_sub(READER, Release);
None
}
}
/// Attempts to acquire a write lock while disabling local IRQs.
///
/// This function will never spin-wait and will return immediately. When
/// multiple readers or writers attempt to acquire the lock, this method
/// does not guarantee any order. Interrupts will automatically be restored
/// when acquiring fails.
pub fn try_write_irq_disabled(&self) -> Option<RwLockWriteGuard<T>> {
let irq_guard = disable_local();
if self
.lock
.compare_exchange(0, WRITER, Acquire, Relaxed)
.is_ok()
{
Some(RwLockWriteGuard {
inner: self,
inner_guard: InnerGuard::IrqGuard(irq_guard),
})
} else {
None
}
}
/// Attempts to acquire a upread lock while disabling local IRQs.
///
/// This function will never spin-wait and will return immediately. When
/// multiple readers or writers attempt to acquire the lock, this method
/// does not guarantee any order. Interrupts will automatically be restored
/// when acquiring fails.
pub fn try_upread_irq_disabled(&self) -> Option<RwLockUpgradeableGuard<T>> {
let irq_guard = disable_local();
let lock = self.lock.fetch_or(UPGRADEABLE_READER, Acquire) & (WRITER | UPGRADEABLE_READER);
if lock == 0 {
return Some(RwLockUpgradeableGuard {
inner: self,
inner_guard: InnerGuard::IrqGuard(irq_guard),
});
} else if lock == WRITER {
self.lock.fetch_sub(UPGRADEABLE_READER, Release);
}
None
}
/// Acquires a read lock and spin-wait until it can be acquired.
///
/// The calling thread will spin-wait until there are no writers or
/// upgrading upreaders present. There is no guarantee for the order
/// in which other readers or writers waiting simultaneously will
/// obtain the lock.
///
/// This method does not disable interrupts, so any locks related to
/// interrupt context should avoid using this method, and use [`read_irq_disabled`]
/// instead. When IRQ handlers are allowed to be executed while holding
/// this lock, it is preferable to use this method over the [`read_irq_disabled`]
/// method as it has a higher efficiency.
///
/// [`read_irq_disabled`]: Self::read_irq_disabled
pub fn read(&self) -> RwLockReadGuard<T> {
loop {
if let Some(readguard) = self.try_read() {
return readguard;
} else {
core::hint::spin_loop();
}
}
}
/// Acquires a read lock through an [`Arc`].
///
/// The method is similar to [`read`], but it doesn't have the requirement
/// for compile-time checked lifetimes of the read guard.
///
/// [`read`]: Self::read
pub fn read_arc(self: &Arc<Self>) -> ArcRwLockReadGuard<T> {
loop {
if let Some(readguard) = self.try_read_arc() {
return readguard;
} else {
core::hint::spin_loop();
}
}
}
/// Acquires a write lock and spin-wait until it can be acquired.
///
/// The calling thread will spin-wait until there are no other writers,
/// upreaders or readers present. There is no guarantee for the order
/// in which other readers or writers waiting simultaneously will
/// obtain the lock.
///
/// This method does not disable interrupts, so any locks related to
/// interrupt context should avoid using this method, and use [`write_irq_disabled`]
/// instead. When IRQ handlers are allowed to be executed while holding
/// this lock, it is preferable to use this method over the [`write_irq_disabled`]
/// method as it has a higher efficiency.
///
/// [`write_irq_disabled`]: Self::write_irq_disabled
pub fn write(&self) -> RwLockWriteGuard<T> {
loop {
if let Some(writeguard) = self.try_write() {
return writeguard;
} else {
core::hint::spin_loop();
}
}
}
/// Acquires a write lock through an [`Arc`].
///
/// The method is similar to [`write`], but it doesn't have the requirement
/// for compile-time checked lifetimes of the lock guard.
///
/// [`write`]: Self::write
pub fn write_arc(self: &Arc<Self>) -> ArcRwLockWriteGuard<T> {
loop {
if let Some(writeguard) = self.try_write_arc() {
return writeguard;
} else {
core::hint::spin_loop();
}
}
}
/// Acquires an upreader and spin-wait until it can be acquired.
///
/// The calling thread will spin-wait until there are no other writers,
/// or upreaders. There is no guarantee for the order in which other
/// readers or writers waiting simultaneously will obtain the lock.
///
/// Upreader will not block new readers until it tries to upgrade. Upreader
/// and reader do not differ before invoking the upgread method. However,
/// only one upreader can exist at any time to avoid deadlock in the
/// upgread method.
///
/// This method does not disable interrupts, so any locks related to
/// interrupt context should avoid using this method, and use [`upread_irq_disabled`]
/// instead. When IRQ handlers are allowed to be executed while holding
/// this lock, it is preferable to use this method over the [`upread_irq_disabled`]
/// method as it has a higher efficiency.
///
/// [`upread_irq_disabled`]: Self::upread_irq_disabled
pub fn upread(&self) -> RwLockUpgradeableGuard<T> {
loop {
if let Some(guard) = self.try_upread() {
return guard;
} else {
core::hint::spin_loop();
}
}
}
/// Acquires an upgradeable read lock through an [`Arc`].
///
/// The method is similar to [`upread`], but it doesn't have the requirement
/// for compile-time checked lifetimes of the lock guard.
///
/// [`upread`]: Self::upread
pub fn upread_arc(self: &Arc<Self>) -> ArcRwLockUpgradeableGuard<T> {
loop {
if let Some(guard) = self.try_upread_arc() {
return guard;
} else {
core::hint::spin_loop();
}
}
}
/// Attempts to acquire a read lock.
///
/// This function will never spin-wait and will return immediately.
///
/// This method does not disable interrupts, so any locks related to
/// interrupt context should avoid using this method, and use
/// [`try_read_irq_disabled`] instead. When IRQ handlers are allowed to
/// be executed while holding this lock, it is preferable to use this
/// method over the [`try_read_irq_disabled`] method as it has a higher
/// efficiency.
///
/// [`try_read_irq_disabled`]: Self::try_read_irq_disabled
pub fn try_read(&self) -> Option<RwLockReadGuard<T>> {
let guard = disable_preempt();
let lock = self.lock.fetch_add(READER, Acquire);
if lock & (WRITER | MAX_READER | BEING_UPGRADED) == 0 {
Some(RwLockReadGuard {
inner: self,
inner_guard: InnerGuard::PreemptGuard(guard),
})
} else {
self.lock.fetch_sub(READER, Release);
None
}
}
/// Attempts to acquire an read lock through an [`Arc`].
///
/// The method is similar to [`try_read`], but it doesn't have the requirement
/// for compile-time checked lifetimes of the lock guard.
///
/// [`try_read`]: Self::try_read
pub fn try_read_arc(self: &Arc<Self>) -> Option<ArcRwLockReadGuard<T>> {
let guard = disable_preempt();
let lock = self.lock.fetch_add(READER, Acquire);
if lock & (WRITER | MAX_READER | BEING_UPGRADED) == 0 {
Some(ArcRwLockReadGuard {
inner: self.clone(),
inner_guard: InnerGuard::PreemptGuard(guard),
})
} else {
self.lock.fetch_sub(READER, Release);
None
}
}
/// Attempts to acquire a write lock.
///
/// This function will never spin-wait and will return immediately.
///
/// This method does not disable interrupts, so any locks related to
/// interrupt context should avoid using this method, and use
/// [`try_write_irq_disabled`] instead. When IRQ handlers are allowed to
/// be executed while holding this lock, it is preferable to use this
/// method over the [`try_write_irq_disabled`] method as it has a higher
/// efficiency.
///
/// [`try_write_irq_disabled`]: Self::try_write_irq_disabled
pub fn try_write(&self) -> Option<RwLockWriteGuard<T>> {
let guard = disable_preempt();
if self
.lock
.compare_exchange(0, WRITER, Acquire, Relaxed)
.is_ok()
{
Some(RwLockWriteGuard {
inner: self,
inner_guard: InnerGuard::PreemptGuard(guard),
})
} else {
None
}
}
/// Attempts to acquire a write lock through an [`Arc`].
///
/// The method is similar to [`try_write`], but it doesn't have the requirement
/// for compile-time checked lifetimes of the lock guard.
///
/// [`try_write`]: Self::try_write
fn try_write_arc(self: &Arc<Self>) -> Option<ArcRwLockWriteGuard<T>> {
let guard = disable_preempt();
if self
.lock
.compare_exchange(0, WRITER, Acquire, Relaxed)
.is_ok()
{
Some(ArcRwLockWriteGuard {
inner: self.clone(),
inner_guard: InnerGuard::PreemptGuard(guard),
})
} else {
None
}
}
/// Attempts to acquire an upread lock.
///
/// This function will never spin-wait and will return immediately.
///
/// This method does not disable interrupts, so any locks related to
/// interrupt context should avoid using this method, and use
/// [`try_upread_irq_disabled`] instead. When IRQ handlers are allowed to
/// be executed while holding this lock, it is preferable to use this
/// method over the [`try_upread_irq_disabled`] method as it has a higher
/// efficiency.
///
/// [`try_upread_irq_disabled`]: Self::try_upread_irq_disabled
pub fn try_upread(&self) -> Option<RwLockUpgradeableGuard<T>> {
let guard = disable_preempt();
let lock = self.lock.fetch_or(UPGRADEABLE_READER, Acquire) & (WRITER | UPGRADEABLE_READER);
if lock == 0 {
return Some(RwLockUpgradeableGuard {
inner: self,
inner_guard: InnerGuard::PreemptGuard(guard),
});
} else if lock == WRITER {
self.lock.fetch_sub(UPGRADEABLE_READER, Release);
}
None
}
/// Attempts to acquire an upgradeable read lock through an [`Arc`].
///
/// The method is similar to [`try_upread`], but it doesn't have the requirement
/// for compile-time checked lifetimes of the lock guard.
///
/// [`try_upread`]: Self::try_upread
pub fn try_upread_arc(self: &Arc<Self>) -> Option<ArcRwLockUpgradeableGuard<T>> {
let guard = disable_preempt();
let lock = self.lock.fetch_or(UPGRADEABLE_READER, Acquire) & (WRITER | UPGRADEABLE_READER);
if lock == 0 {
return Some(ArcRwLockUpgradeableGuard {
inner: self.clone(),
inner_guard: InnerGuard::PreemptGuard(guard),
});
} else if lock == WRITER {
self.lock.fetch_sub(UPGRADEABLE_READER, Release);
}
None
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.val, f)
}
}
/// Because there can be more than one readers to get the T's immutable ref,
/// so T must be Sync to guarantee the sharing safety.
unsafe impl<T: ?Sized + Send> Send for RwLock<T> {}
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> !Send for RwLockWriteGuard_<T, R> {}
unsafe impl<T: ?Sized + Sync, R: Deref<Target = RwLock<T>> + Clone + Sync> Sync
for RwLockWriteGuard_<T, R>
{
}
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> !Send for RwLockReadGuard_<T, R> {}
unsafe impl<T: ?Sized + Sync, R: Deref<Target = RwLock<T>> + Clone + Sync> Sync
for RwLockReadGuard_<T, R>
{
}
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> !Send for RwLockUpgradeableGuard_<T, R> {}
unsafe impl<T: ?Sized + Sync, R: Deref<Target = RwLock<T>> + Clone + Sync> Sync
for RwLockUpgradeableGuard_<T, R>
{
}
enum InnerGuard {
IrqGuard(DisabledLocalIrqGuard),
PreemptGuard(DisablePreemptGuard),
}
impl InnerGuard {
/// Transfers the current guard to a new `InnerGuard` instance ensuring atomicity during lock upgrades or downgrades.
///
/// This function guarantees that there will be no 'gaps' between the destruction of the old guard and
/// the creation of the new guard, maintaining the atomicity of lock transitions.
fn transfer_to(&mut self) -> Self {
match self {
InnerGuard::IrqGuard(irq_guard) => InnerGuard::IrqGuard(irq_guard.transfer_to()),
InnerGuard::PreemptGuard(preempt_guard) => {
InnerGuard::PreemptGuard(preempt_guard.transfer_to())
}
}
}
}
/// A guard that provides immutable data access.
pub struct RwLockReadGuard_<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> {
inner_guard: InnerGuard,
inner: R,
}
/// A guard that provides shared read access to the data protected by a [`RwLock`].
pub type RwLockReadGuard<'a, T> = RwLockReadGuard_<T, &'a RwLock<T>>;
/// A guard that provides shared read access to the data protected by a `Arc<RwLock>`.
pub type ArcRwLockReadGuard<T> = RwLockReadGuard_<T, Arc<RwLock<T>>>;
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> Deref for RwLockReadGuard_<T, R> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.inner.val.get() }
}
}
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> Drop for RwLockReadGuard_<T, R> {
fn drop(&mut self) {
self.inner.lock.fetch_sub(READER, Release);
}
}
impl<T: ?Sized + fmt::Debug, R: Deref<Target = RwLock<T>> + Clone> fmt::Debug
for RwLockReadGuard_<T, R>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
/// A guard that provides mutable data access.
pub struct RwLockWriteGuard_<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> {
inner_guard: InnerGuard,
inner: R,
}
/// A guard that provides exclusive write access to the data protected by a [`RwLock`].
pub type RwLockWriteGuard<'a, T> = RwLockWriteGuard_<T, &'a RwLock<T>>;
/// A guard that provides exclusive write access to the data protected by a `Arc<RwLock>`.
pub type ArcRwLockWriteGuard<T> = RwLockWriteGuard_<T, Arc<RwLock<T>>>;
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> Deref for RwLockWriteGuard_<T, R> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.inner.val.get() }
}
}
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> RwLockWriteGuard_<T, R> {
/// Atomically downgrades a write guard to an upgradeable reader guard.
///
/// This method always succeeds because the lock is exclusively held by the writer.
pub fn downgrade(mut self) -> RwLockUpgradeableGuard_<T, R> {
loop {
self = match self.try_downgrade() {
Ok(guard) => return guard,
Err(e) => e,
};
}
}
/// This is not exposed as a public method to prevent intermediate lock states from affecting the
/// downgrade process.
fn try_downgrade(mut self) -> Result<RwLockUpgradeableGuard_<T, R>, Self> {
let inner = self.inner.clone();
let res = self
.inner
.lock
.compare_exchange(WRITER, UPGRADEABLE_READER, AcqRel, Relaxed);
if res.is_ok() {
let inner_guard = self.inner_guard.transfer_to();
drop(self);
Ok(RwLockUpgradeableGuard_ { inner, inner_guard })
} else {
Err(self)
}
}
}
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> DerefMut for RwLockWriteGuard_<T, R> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.inner.val.get() }
}
}
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> Drop for RwLockWriteGuard_<T, R> {
fn drop(&mut self) {
self.inner.lock.fetch_and(!WRITER, Release);
}
}
impl<T: ?Sized + fmt::Debug, R: Deref<Target = RwLock<T>> + Clone> fmt::Debug
for RwLockWriteGuard_<T, R>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
/// A guard that provides immutable data access but can be atomically
/// upgraded to `RwLockWriteGuard`.
pub struct RwLockUpgradeableGuard_<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> {
inner_guard: InnerGuard,
inner: R,
}
/// A upgradable guard that provides read access to the data protected by a [`RwLock`].
pub type RwLockUpgradeableGuard<'a, T> = RwLockUpgradeableGuard_<T, &'a RwLock<T>>;
/// A upgradable guard that provides read access to the data protected by a `Arc<RwLock>`.
pub type ArcRwLockUpgradeableGuard<T> = RwLockUpgradeableGuard_<T, Arc<RwLock<T>>>;
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> RwLockUpgradeableGuard_<T, R> {
/// Upgrades this upread guard to a write guard atomically.
///
/// After calling this method, subsequent readers will be blocked
/// while previous readers remain unaffected. The calling thread
/// will spin-wait until previous readers finish.
pub fn upgrade(mut self) -> RwLockWriteGuard_<T, R> {
self.inner.lock.fetch_or(BEING_UPGRADED, Acquire);
loop {
self = match self.try_upgrade() {
Ok(guard) => return guard,
Err(e) => e,
};
}
}
/// Attempts to upgrade this upread guard to a write guard atomically.
///
/// This function will never spin-wait and will return immediately.
pub fn try_upgrade(mut self) -> Result<RwLockWriteGuard_<T, R>, Self> {
let res = self.inner.lock.compare_exchange(
UPGRADEABLE_READER | BEING_UPGRADED,
WRITER | UPGRADEABLE_READER,
AcqRel,
Relaxed,
);
if res.is_ok() {
let inner = self.inner.clone();
let inner_guard = self.inner_guard.transfer_to();
drop(self);
Ok(RwLockWriteGuard_ { inner, inner_guard })
} else {
Err(self)
}
}
}
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> Deref for RwLockUpgradeableGuard_<T, R> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.inner.val.get() }
}
}
impl<T: ?Sized, R: Deref<Target = RwLock<T>> + Clone> Drop for RwLockUpgradeableGuard_<T, R> {
fn drop(&mut self) {
self.inner.lock.fetch_sub(UPGRADEABLE_READER, Release);
}
}
impl<T: ?Sized + fmt::Debug, R: Deref<Target = RwLock<T>> + Clone> fmt::Debug
for RwLockUpgradeableGuard_<T, R>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}