Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put everything in Context behind the same Mutex #1050

Merged
merged 13 commits into from
Jan 10, 2022
Prev Previous commit
Next Next commit
Revert to using atomic_refcell
  • Loading branch information
emilk committed Jan 8, 2022
commit 3b2a7839c59e5c121d9a4590c4ce88066f52e9ec
2 changes: 1 addition & 1 deletion epaint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mint = ["emath/mint"]
# implement serde on most types.
serialize = ["serde", "ahash/serde", "emath/serde"]

single_threaded = []
single_threaded = ["atomic_refcell"]

# Only needed if you plan to use the same fonts from multiple threads.
# It comes with a minor performance impact.
Expand Down
17 changes: 7 additions & 10 deletions epaint/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,19 @@ mod arc_impl {

#[cfg(not(feature = "multi_threaded"))]
mod mutex_impl {
use std::cell::RefCell;
// `atomic_refcell` will panic if multiple threads try to access the same value

/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[derive(Default)]
pub struct Mutex<T>(RefCell<T>);
pub struct Mutex<T>(atomic_refcell::AtomicRefCell<T>);

/// The lock you get from [`Mutex`].
pub use std::cell::RefMut as MutexGuard;
pub use atomic_refcell::AtomicRefMut as MutexGuard;

impl<T> Mutex<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(RefCell::new(val))
Self(atomic_refcell::AtomicRefCell::new(val))
}

/// Panics if already locked.
Expand All @@ -176,24 +175,22 @@ mod mutex_impl {

#[cfg(not(feature = "multi_threaded"))]
mod rw_lock_impl {
use std::cell::RefCell;

// `atomic_refcell` will panic if multiple threads try to access the same value

/// The lock you get from [`RwLock::read`].
pub use std::cell::Ref as RwLockReadGuard;
pub use atomic_refcell::AtomicRef as RwLockReadGuard;

/// The lock you get from [`RwLock::write`].
pub use std::cell::RefMut as RwLockWriteGuard;
pub use atomic_refcell::AtomicRefMut as RwLockWriteGuard;

/// Provides interior mutability. Only thread-safe if the `multi_threaded` feature is enabled.
#[derive(Default)]
pub struct RwLock<T>(RefCell<T>);
pub struct RwLock<T>(atomic_refcell::AtomicRefCell<T>);

impl<T> RwLock<T> {
#[inline(always)]
pub fn new(val: T) -> Self {
Self(RefCell::new(val))
Self(atomic_refcell::AtomicRefCell::new(val))
}

#[inline(always)]
Expand Down