mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Implement AtomicRefCell.
MozReview-Commit-ID: 9LZ1WPQADav
This commit is contained in:
parent
8c14533e7f
commit
518324cff6
2 changed files with 42 additions and 0 deletions
41
components/style/atomic_refcell.rs
Normal file
41
components/style/atomic_refcell.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||||
|
|
||||||
|
/// Container type providing RefCell-like semantics for objects shared across
|
||||||
|
/// threads.
|
||||||
|
///
|
||||||
|
/// RwLock is traditionally considered to be the |Sync| analogue of RefCell.
|
||||||
|
/// However, for consumers that can guarantee that they will never mutably
|
||||||
|
/// borrow the contents concurrently with immutable borrows, an RwLock feels
|
||||||
|
/// like overkill.
|
||||||
|
///
|
||||||
|
/// The RwLock in the standard library is indeed heavyweight, since it heap-
|
||||||
|
/// allocates an OS-specific primitive and delegates operations to it. However,
|
||||||
|
/// parking_lot provides a pure-rust implementation of the standard
|
||||||
|
/// synchronization primitives, with uncontended borrows compiling down to a
|
||||||
|
/// single atomic operation. This is exactly how we would build an atomic
|
||||||
|
/// RefCell, so we newtype it with some API sugar.
|
||||||
|
pub struct AtomicRefCell<T>(RwLock<T>);
|
||||||
|
|
||||||
|
pub type AtomicRef<'a, T> = RwLockReadGuard<'a, T>;
|
||||||
|
pub type AtomicRefMut<'a, T> = RwLockWriteGuard<'a, T>;
|
||||||
|
impl<T> AtomicRefCell<T> {
|
||||||
|
pub fn new(value: T) -> Self {
|
||||||
|
AtomicRefCell(RwLock::new(value))
|
||||||
|
}
|
||||||
|
pub fn borrow(&self) -> AtomicRef<T> {
|
||||||
|
self.0.try_read().unwrap()
|
||||||
|
}
|
||||||
|
pub fn borrow_mut(&self) -> AtomicRefMut<T> {
|
||||||
|
self.0.try_write().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Default> Default for AtomicRefCell<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new(T::default())
|
||||||
|
}
|
||||||
|
}
|
|
@ -81,6 +81,7 @@ extern crate util;
|
||||||
#[macro_use] pub mod string_cache;
|
#[macro_use] pub mod string_cache;
|
||||||
|
|
||||||
pub mod animation;
|
pub mod animation;
|
||||||
|
pub mod atomic_refcell;
|
||||||
pub mod attr;
|
pub mod attr;
|
||||||
pub mod bezier;
|
pub mod bezier;
|
||||||
pub mod cache;
|
pub mod cache;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue