Skip to main content

parking_lot/
lib.rs

1//! This library provides compact and efficient implementations of [`Mutex`],
2//! [`FairMutex`], [`ReentrantMutex`], [`RwLock`], [`RecursiveRwLock`],
3//! [`Condvar`] and [`Once`].
4//!
5//! # Use in global allocators
6//!
7//! The synchronization primitives in this crate must not be used internally by
8//! a global allocator. Their contended paths may allocate memory, including
9//! while internal parking-lot locks are held. If the allocator then blocks on
10//! one of these primitives, it may recursively invoke itself or deadlock.
11
12#![warn(missing_docs)]
13#![warn(rust_2018_idioms)]
14
15mod condvar;
16mod fair_mutex;
17mod mutex;
18mod once;
19mod raw_condvar;
20mod raw_fair_mutex;
21mod raw_mutex;
22mod raw_rwlock;
23mod recursive_rwlock;
24mod remutex;
25mod rwlock;
26mod util;
27
28#[cfg(feature = "deadlock_detection")]
29pub mod deadlock;
30#[cfg(not(feature = "deadlock_detection"))]
31mod deadlock;
32
33// Deadlock detection records lock ownership per thread, so guards cannot be
34// sent to another thread while it is enabled.
35#[cfg(all(feature = "send_guard", feature = "deadlock_detection"))]
36compile_error!("the `send_guard` and `deadlock_detection` features cannot be used together");
37#[cfg(feature = "send_guard")]
38type GuardMarker = lock_api::GuardSend;
39#[cfg(not(feature = "send_guard"))]
40type GuardMarker = lock_api::GuardNoSend;
41
42pub use self::condvar::{Condvar, WaitTimeoutResult};
43pub use self::fair_mutex::{FairMutex, FairMutexGuard, MappedFairMutexGuard};
44pub use self::mutex::{MappedMutexGuard, Mutex, MutexGuard};
45pub use self::once::{Once, OnceState};
46pub use self::raw_condvar::RawCondvar;
47pub use self::raw_fair_mutex::RawFairMutex;
48pub use self::raw_mutex::RawMutex;
49pub use self::raw_rwlock::{RawRwLock, RawRwLockRecursive};
50pub use self::recursive_rwlock::{
51    MappedRecursiveRwLockReadGuard, MappedRecursiveRwLockWriteGuard, RecursiveRwLock,
52    RecursiveRwLockReadGuard, RecursiveRwLockUpgradableReadGuard, RecursiveRwLockWriteGuard,
53};
54pub use self::remutex::{
55    MappedReentrantMutexGuard, RawThreadId, ReentrantMutex, ReentrantMutexGuard,
56};
57pub use self::rwlock::{
58    MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard,
59    RwLockUpgradableReadGuard, RwLockWriteGuard,
60};
61pub use ::lock_api;
62
63#[cfg(feature = "arc_lock")]
64pub use self::lock_api::{
65    ArcMutexGuard, ArcReentrantMutexGuard, ArcRwLockReadGuard, ArcRwLockUpgradableReadGuard,
66    ArcRwLockWriteGuard,
67};
68
69#[cfg(all(test, feature = "send_guard"))]
70#[test]
71fn test_send_guards() {
72    fn assert_send<T: Send>() {}
73
74    assert_send::<MutexGuard<'static, ()>>();
75    assert_send::<MappedMutexGuard<'static, ()>>();
76    assert_send::<FairMutexGuard<'static, ()>>();
77    assert_send::<MappedFairMutexGuard<'static, ()>>();
78    assert_send::<RwLockReadGuard<'static, ()>>();
79    assert_send::<RwLockWriteGuard<'static, ()>>();
80    assert_send::<RwLockUpgradableReadGuard<'static, ()>>();
81    assert_send::<MappedRwLockReadGuard<'static, ()>>();
82    assert_send::<MappedRwLockWriteGuard<'static, ()>>();
83    assert_send::<RecursiveRwLockReadGuard<'static, ()>>();
84    assert_send::<RecursiveRwLockWriteGuard<'static, ()>>();
85    assert_send::<RecursiveRwLockUpgradableReadGuard<'static, ()>>();
86    assert_send::<MappedRecursiveRwLockReadGuard<'static, ()>>();
87    assert_send::<MappedRecursiveRwLockWriteGuard<'static, ()>>();
88}