rustpython_common/
lock.rs

1//! A module containing [`lock_api`]-based lock types that are or are not `Send + Sync`
2//! depending on whether the `threading` feature of this module is enabled.
3
4use lock_api::{
5    MappedMutexGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, MutexGuard, RwLock,
6    RwLockReadGuard, RwLockUpgradableReadGuard, RwLockWriteGuard,
7};
8
9cfg_if::cfg_if! {
10    if #[cfg(feature = "threading")] {
11        pub use parking_lot::{RawMutex, RawRwLock, RawThreadId};
12
13        pub use once_cell::sync::{Lazy, OnceCell};
14    } else {
15        mod cell_lock;
16        pub use cell_lock::{RawCellMutex as RawMutex, RawCellRwLock as RawRwLock, SingleThreadId as RawThreadId};
17
18        pub use once_cell::unsync::{Lazy, OnceCell};
19    }
20}
21
22mod immutable_mutex;
23pub use immutable_mutex::*;
24mod thread_mutex;
25pub use thread_mutex::*;
26
27pub type PyMutex<T> = Mutex<RawMutex, T>;
28pub type PyMutexGuard<'a, T> = MutexGuard<'a, RawMutex, T>;
29pub type PyMappedMutexGuard<'a, T> = MappedMutexGuard<'a, RawMutex, T>;
30pub type PyImmutableMappedMutexGuard<'a, T> = ImmutableMappedMutexGuard<'a, RawMutex, T>;
31pub type PyThreadMutex<T> = ThreadMutex<RawMutex, RawThreadId, T>;
32pub type PyThreadMutexGuard<'a, T> = ThreadMutexGuard<'a, RawMutex, RawThreadId, T>;
33pub type PyMappedThreadMutexGuard<'a, T> = MappedThreadMutexGuard<'a, RawMutex, RawThreadId, T>;
34
35pub type PyRwLock<T> = RwLock<RawRwLock, T>;
36pub type PyRwLockUpgradableReadGuard<'a, T> = RwLockUpgradableReadGuard<'a, RawRwLock, T>;
37pub type PyRwLockReadGuard<'a, T> = RwLockReadGuard<'a, RawRwLock, T>;
38pub type PyMappedRwLockReadGuard<'a, T> = MappedRwLockReadGuard<'a, RawRwLock, T>;
39pub type PyRwLockWriteGuard<'a, T> = RwLockWriteGuard<'a, RawRwLock, T>;
40pub type PyMappedRwLockWriteGuard<'a, T> = MappedRwLockWriteGuard<'a, RawRwLock, T>;
41
42// can add fn const_{mutex,rwlock}() if necessary, but we probably won't need to