Expand description

Zero cost abstraction for Locking Methods

‘parking_lot’ has a lot variants for locking, there is the normal blocking lock, a try_lock, two forms of timed try locks and all of these are available as recursive variant as well.

This leads to a some explosion on the api, dispatching to these variants in generic code becomes pretty bloated. This crate abstracts all the different locking variants into a single trait with a uniform API that takes policy objects for the dispatch on underlying locking methods

Example

// reexports parts of parking_lot as well
use parking_method::*;

let rwlock = RwLock::new(String::from("test"));

// Note the 2 following syntax forms are equivalent
assert_eq!(*Blocking.read(&rwlock).unwrap(), "test");
assert_eq!(*ReadLockMethod::read(&Blocking, &rwlock).unwrap(), "test");

assert_eq!(*TryLock.read(&rwlock).unwrap(), "test");

let timeout = Duration::from_millis(100);

assert_eq!(
    *ReadLockMethod::read(&timeout, &rwlock).unwrap(),
    "test"
);

assert_eq!(
    *ReadLockMethod::read(
        &Recursive(Instant::now() + Duration::from_millis(100)),
        &rwlock).unwrap(),
    "test"
);

The Locking methods

There are plenty flavors on how a lock can be obtained. The normal blocking way, trying to obtain a lock, possibly with timeouts, allow a thread to lock a single RwLock multiple times. These are (zero-cost) abstracted here.

Structs

Marker for blocking locks, waits until the lock becomes available.

reexport for convenience A Duration type to represent a span of time, typically used for system timeouts.

reexport for convenience A measurement of a monotonically nondecreasing clock. Opaque and useful only with Duration.

Marker for recursive locking. Allows to obtain a read-lock multiple times by a single thread.

Marker for trying to obtain a lock in a fallible way.

Traits

Trait for implementing read/write flavors on Mutex. Note that there are no Recursive locks in Mutex, use ReentrantMutex for that.

Trait for implementing read flavors on RwLocks.

Trait for implementing read/write flavors on ReentantMutex.

Trait for implementing write flavors on RwLocks.

Type Definitions

A mutual exclusion primitive useful for protecting shared data

An RAII implementation of a “scoped lock” of a mutex. When this structure is dropped (falls out of scope), the lock will be unlocked.

A mutex which can be recursively locked by a single thread.

An RAII implementation of a “scoped lock” of a reentrant mutex. When this structure is dropped (falls out of scope), the lock will be unlocked.

A reader-writer lock

RAII structure used to release the shared read access of a lock when dropped.

RAII structure used to release the exclusive write access of a lock when dropped.