Struct abi_stable::external_types::parking_lot::rw_lock::RRwLock[][src]

#[repr(C)]
pub struct RRwLock<T> { /* fields omitted */ }
Expand description

A read-write lock that allows dynamic mutable/shared borrows of shared data.

RRwLock allows either multiple shared locks,or a single write lock.

Poisoning

As opposed to the standard library version of this type, this rwlock type does not use poisoning, simply unlocking the lock when a panic happens.

Example

use abi_stable::external_types::RRwLock;

static LOCK: RRwLock<usize> = RRwLock::new(0);

let guard = std::thread::spawn(|| {
    for _ in 0..100 {
        *LOCK.write() += 1;
    }
});

for _ in 0..100 {
    *LOCK.write() += 1;
}

guard.join().unwrap();

assert_eq!(*LOCK.read(), 200);

Implementations

Constructs a lock,wrapping value.

Example
use abi_stable::external_types::RRwLock;

static LOCK: RRwLock<Option<String>> = RRwLock::new(None);

let lock = RRwLock::new(0);

Unwraps this lock into its wrapped data.

Example
use abi_stable::external_types::RRwLock;

let lock = RRwLock::new("hello".to_string());

assert_eq!(lock.into_inner().as_str(), "hello");

Gets a mutable reference to its wrapped data.

This does not require any locking,since it takes self mutably.

Example
use abi_stable::external_types::RRwLock;

let mut lock = RRwLock::new("Hello".to_string());

lock.get_mut().push_str(", World!");

assert_eq!(lock.read().as_str(), "Hello, World!");

Acquires a lock for reading,blocking the current thread until it can.

This function returns a read guard,which releases read access when it is dropped.

Trying to lock the rwlock for reading in the same thread that has write access to the same rwlock will cause a deadlock.

Example
use abi_stable::external_types::RRwLock;

static LOCK: RRwLock<usize> = RRwLock::new(0);

*LOCK.write() += 4;

let read_guard_a = LOCK.read();
let read_guard_b = LOCK.read();

assert_eq!(*read_guard_a, 4);
assert_eq!(*read_guard_b, 4);

Attemps to acquire a lock for reading,failing if it is locked for writing.

Returns the read guard if the rwlock can be immediately acquired,otherwise returns RNone.

Example
use abi_stable::external_types::RRwLock;

static LOCK: RRwLock<usize> = RRwLock::new(0);

let mut write_guard = LOCK.write();

assert!(LOCK.try_read().is_none());

*write_guard += 4;
drop(write_guard);

assert_eq!(*LOCK.try_read().unwrap(), 4);

Attempts to acquire a lock for reading,for the timeout duration.

Once the timeout is reached,this will return None, otherwise it will return the read guard.

Example
use abi_stable::{external_types::RRwLock, std_types::RDuration};

static LOCK: RRwLock<usize> = RRwLock::new(0);

static DUR: RDuration = RDuration::from_millis(1);

let mut write_guard = LOCK.write();

assert!(LOCK.try_read_for(DUR).is_none());

*write_guard += 7;
drop(write_guard);

assert_eq!(*LOCK.try_read_for(DUR).unwrap(), 7);

Acquires a lock for writing,blocking the current thread until it can.

This function returns a write guard,which releases write access when it is dropped.

Trying to lock the rwlock in the same thread that has read or write access to the same rwlock will cause a deadlock.

Example
use abi_stable::external_types::RRwLock;

let lock = RRwLock::new(0);

let mut guard = lock.write();

*guard += 4;

assert_eq!(*guard, 4);

Attemps to acquire a lock for writing.

Returns the write guard if the rwlock can be immediately acquired,otherwise returns RNone.

Example
use abi_stable::external_types::RRwLock;

let lock = RRwLock::new(0);

let mut guard = lock.write();

assert!(lock.try_write().is_none());

*guard += 4;

assert_eq!(*guard, 4);

Attempts to acquire a lock for writing,for the timeout duration.

Once the timeout is reached,this will return None, otherwise it will return the write guard.

Example
use abi_stable::{external_types::RRwLock, std_types::RDuration};

static DUR: RDuration = RDuration::from_millis(1);

let lock = RRwLock::new(0);

let mut write_guard = lock.try_write_for(DUR).unwrap();
*write_guard += 4;

assert!(lock.try_write_for(DUR).is_none());

assert_eq!(*write_guard, 4);

Trait Implementations

Whether this type has a single invalid bit-pattern. Read more

The layout of the type provided by implementors.

const-equivalents of the associated types.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

This is always WithMetadata_<Self, Self>

Performs the conversion.

Gets a reference to a field, determined by offset. Read more

Gets a muatble reference to a field, determined by offset. Read more

Gets a const pointer to a field, the field is determined by offset. Read more

Gets a mutable pointer to a field, determined by offset. Read more

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more

Swaps a field (determined by offset) with the same field in right. Read more

Gets a copy of a field (determined by offset). The field is determined by offset. Read more

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more

Swaps a field (determined by offset) with the same field in right. Read more

Gets a copy of a field (determined by offset). The field is determined by offset. Read more

Compares the address of self with the address of other. Read more

Emulates the pipeline operator, allowing method syntax in more places. Read more

The same as piped except that the function takes &Self Useful for functions that take &Self instead of Self. Read more

The same as piped, except that the function takes &mut Self. Useful for functions that take &mut Self instead of Self. Read more

Mutates self using a closure taking self by mutable reference, passing it along the method chain. Read more

Observes the value of self, passing it along unmodified. Useful in long method chains. Read more

Performs a conversion with Into. using the turbofish .into_::<_>() syntax. Read more

Performs a reference to reference conversion with AsRef, using the turbofish .as_ref_::<_>() syntax. Read more

Performs a mutable reference to mutable reference conversion with AsMut, using the turbofish .as_mut_::<_>() syntax. Read more

Drops self using method notation. Alternative to std::mem::drop. Read more

Transmutes the element type of this pointer.. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

This is always Self.

Converts a value back to the original type.

Converts a reference back to the original type.

Converts a mutable reference back to the original type.

This is supported on crate feature alloc only.

Converts a box back to the original type.

This is supported on crate feature alloc only.

Converts an Arc back to the original type. Read more

This is supported on crate feature alloc only.

Converts an Rc back to the original type. Read more

Converts a value back to the original type.

Converts a reference back to the original type.

Converts a mutable reference back to the original type.

This is supported on crate feature alloc only.

Converts a box back to the original type.

This is supported on crate feature alloc only.

Converts an Arc back to the original type.

This is supported on crate feature alloc only.

Converts an Rc back to the original type.