-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add threading primitives with feature toggle (#263)
If the `threading` feature is set, the `threading` module will contain thread-safe primitives for shared ownership and mutation, otherwise these will be their single threaded counterparts. This way, single-threaded applications don't have to pay for threaded primitives.
- Loading branch information
Showing
7 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//! Type definitions for putting shared ownership and synchronized mutation behind the `threading` feature toggle. | ||
//! | ||
//! That way, single-threaded applications will not have to use thread-safe primitives, and simply do not specify the 'threading' feature. | ||
|
||
#[cfg(feature = "threading")] | ||
mod _impl { | ||
use std::sync::Arc; | ||
|
||
/// A reference counted pointer type for shared ownership. | ||
pub type OwnShared<T> = Arc<T>; | ||
/// A synchronization primitive which can start read-only and transition to support mutation. | ||
pub type MutableOnDemand<T> = parking_lot::RwLock<T>; | ||
|
||
/// Get an upgradable shared reference through a [`MutableOnDemand`] for read-only access. | ||
/// | ||
/// This access can be upgraded using [`upgrade_ref_to_mut()`]. | ||
pub fn get_ref_upgradeable<T>(v: &MutableOnDemand<T>) -> parking_lot::RwLockUpgradableReadGuard<'_, T> { | ||
v.upgradable_read() | ||
} | ||
|
||
/// Get a shared reference through a [`MutableOnDemand`] for read-only access. | ||
pub fn get_ref<T>(v: &MutableOnDemand<T>) -> parking_lot::RwLockReadGuard<'_, T> { | ||
v.read() | ||
} | ||
|
||
/// Get a mutable reference through a [`MutableOnDemand`] for read-write access. | ||
pub fn get_mut<T>(v: &MutableOnDemand<T>) -> parking_lot::RwLockWriteGuard<'_, T> { | ||
v.write() | ||
} | ||
|
||
/// Upgrade a handle previously obtained with [`get_ref_upgradeable()`] to support mutation. | ||
pub fn upgrade_ref_to_mut<T>( | ||
v: parking_lot::RwLockUpgradableReadGuard<'_, T>, | ||
) -> parking_lot::RwLockWriteGuard<'_, T> { | ||
parking_lot::RwLockUpgradableReadGuard::upgrade(v) | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "threading"))] | ||
mod _impl { | ||
use std::{ | ||
cell::{Ref, RefCell, RefMut}, | ||
rc::Rc, | ||
}; | ||
|
||
/// A reference counted pointer type for shared ownership. | ||
pub type OwnShared<T> = Rc<T>; | ||
/// A synchronization primitive which can start read-only and transition to support mutation. | ||
pub type MutableOnDemand<T> = RefCell<T>; | ||
|
||
/// Get an upgradable shared reference through a [`MutableOnDemand`] for read-only access. | ||
/// | ||
/// This access can be upgraded using [`upgrade_ref_to_mut()`]. | ||
pub fn get_ref_upgradeable<T>(v: &RefCell<T>) -> RefMut<'_, T> { | ||
v.borrow_mut() | ||
} | ||
|
||
/// Get a shared reference through a [`MutableOnDemand`] for read-only access. | ||
pub fn get_mut<T>(v: &RefCell<T>) -> RefMut<'_, T> { | ||
v.borrow_mut() | ||
} | ||
|
||
/// Get a mutable reference through a [`MutableOnDemand`] for read-write access. | ||
pub fn get_ref<T>(v: &RefCell<T>) -> Ref<'_, T> { | ||
v.borrow() | ||
} | ||
|
||
/// Upgrade a handle previously obtained with [`get_ref_upgradeable()`] to support mutation. | ||
pub fn upgrade_ref_to_mut<T>(v: RefMut<'_, T>) -> RefMut<'_, T> { | ||
v | ||
} | ||
} | ||
|
||
pub use _impl::*; |