Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
zesterer committed Apr 25, 2023
1 parent 3d061b8 commit 0e0ddca
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 38 deletions.
79 changes: 56 additions & 23 deletions benches/mutex.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
#[macro_use]
extern crate criterion;

use criterion::{Criterion, Bencher, black_box};
use std::{
ops::DerefMut,
sync::Arc,
};
use criterion::{black_box, Bencher, Criterion};
use std::{ops::DerefMut, sync::Arc};

trait Mutex<T>: Send + Sync + 'static {
type Guard<'a>: DerefMut<Target = T> where Self: 'a;
type Guard<'a>: DerefMut<Target = T>
where
Self: 'a;
fn new(x: T) -> Self;
fn lock(&self) -> Self::Guard<'_>;
}

impl<T: Send + 'static> Mutex<T> for spin::mutex::SpinMutex<T> {
type Guard<'a> = spin::mutex::SpinMutexGuard<'a, T> where Self: 'a;
fn new(x: T) -> Self { spin::mutex::SpinMutex::new(x) }
fn lock(&self) -> Self::Guard<'_> { self.lock() }
fn new(x: T) -> Self {
spin::mutex::SpinMutex::new(x)
}
fn lock(&self) -> Self::Guard<'_> {
self.lock()
}
}

impl<T: Send + 'static> Mutex<T> for spin::mutex::TicketMutex<T> {
type Guard<'a> = spin::mutex::TicketMutexGuard<'a, T> where Self: 'a;
fn new(x: T) -> Self { spin::mutex::TicketMutex::new(x) }
fn lock(&self) -> Self::Guard<'_> { self.lock() }
fn new(x: T) -> Self {
spin::mutex::TicketMutex::new(x)
}
fn lock(&self) -> Self::Guard<'_> {
self.lock()
}
}

impl<T: Send + 'static> Mutex<T> for std::sync::Mutex<T> {
type Guard<'a> = std::sync::MutexGuard<'a, T> where Self: 'a;
fn new(x: T) -> Self { std::sync::Mutex::new(x) }
fn lock(&self) -> Self::Guard<'_> { self.lock().unwrap() }
fn new(x: T) -> Self {
std::sync::Mutex::new(x)
}
fn lock(&self) -> Self::Guard<'_> {
self.lock().unwrap()
}
}

fn gen_create<M: Mutex<u32>>(b: &mut Bencher) {
Expand Down Expand Up @@ -92,27 +103,49 @@ fn gen_lock_unlock_write_contention<M: Mutex<u32>>(b: &mut Bencher) {
}

fn create(b: &mut Criterion) {
b.bench_function("create-spin-spinmutex", |b| gen_create::<spin::mutex::SpinMutex<u32>>(b));
b.bench_function("create-spin-ticketmutex", |b| gen_create::<spin::mutex::TicketMutex<u32>>(b));
b.bench_function("create-spin-spinmutex", |b| {
gen_create::<spin::mutex::SpinMutex<u32>>(b)
});
b.bench_function("create-spin-ticketmutex", |b| {
gen_create::<spin::mutex::TicketMutex<u32>>(b)
});
b.bench_function("create-std", |b| gen_create::<std::sync::Mutex<u32>>(b));
}

fn lock_unlock(b: &mut Criterion) {
b.bench_function("lock_unlock-spin-spinmutex", |b| gen_lock_unlock::<spin::mutex::SpinMutex<u32>>(b));
b.bench_function("lock_unlock-spin-ticketmutex", |b| gen_lock_unlock::<spin::mutex::TicketMutex<u32>>(b));
b.bench_function("lock_unlock-std", |b| gen_lock_unlock::<std::sync::Mutex<u32>>(b));
b.bench_function("lock_unlock-spin-spinmutex", |b| {
gen_lock_unlock::<spin::mutex::SpinMutex<u32>>(b)
});
b.bench_function("lock_unlock-spin-ticketmutex", |b| {
gen_lock_unlock::<spin::mutex::TicketMutex<u32>>(b)
});
b.bench_function("lock_unlock-std", |b| {
gen_lock_unlock::<std::sync::Mutex<u32>>(b)
});
}

fn lock_unlock_read_contention(b: &mut Criterion) {
b.bench_function("lock_unlock_read_contention-spin-spinmutex", |b| gen_lock_unlock_read_contention::<spin::mutex::SpinMutex<u32>>(b));
b.bench_function("lock_unlock_read_contention-spin-ticketmutex", |b| gen_lock_unlock_read_contention::<spin::mutex::TicketMutex<u32>>(b));
b.bench_function("lock_unlock_read_contention-std", |b| gen_lock_unlock_read_contention::<std::sync::Mutex<u32>>(b));
b.bench_function("lock_unlock_read_contention-spin-spinmutex", |b| {
gen_lock_unlock_read_contention::<spin::mutex::SpinMutex<u32>>(b)
});
b.bench_function("lock_unlock_read_contention-spin-ticketmutex", |b| {
gen_lock_unlock_read_contention::<spin::mutex::TicketMutex<u32>>(b)
});
b.bench_function("lock_unlock_read_contention-std", |b| {
gen_lock_unlock_read_contention::<std::sync::Mutex<u32>>(b)
});
}

fn lock_unlock_write_contention(b: &mut Criterion) {
b.bench_function("lock_unlock_write_contention-spin-spinmutex", |b| gen_lock_unlock_write_contention::<spin::mutex::SpinMutex<u32>>(b));
b.bench_function("lock_unlock_write_contention-spin-ticketmutex", |b| gen_lock_unlock_write_contention::<spin::mutex::TicketMutex<u32>>(b));
b.bench_function("lock_unlock_write_contention-std", |b| gen_lock_unlock_write_contention::<std::sync::Mutex<u32>>(b));
b.bench_function("lock_unlock_write_contention-spin-spinmutex", |b| {
gen_lock_unlock_write_contention::<spin::mutex::SpinMutex<u32>>(b)
});
b.bench_function("lock_unlock_write_contention-spin-ticketmutex", |b| {
gen_lock_unlock_write_contention::<spin::mutex::TicketMutex<u32>>(b)
});
b.bench_function("lock_unlock_write_contention-std", |b| {
gen_lock_unlock_write_contention::<std::sync::Mutex<u32>>(b)
});
}

criterion_group!(
Expand Down
117 changes: 102 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,79 @@ use core::sync::atomic;
#[cfg(all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))]
use portable_atomic as atomic;

#[cfg(all(feature = "portable_atomic", not(portable_atomic_unsafe_assume_single_core)))]
#[cfg(all(
feature = "portable_atomic",
not(portable_atomic_unsafe_assume_single_core)
))]
core::compile_error!("The feature \"portable_atomic\" requires the \"portable_atomic_unsafe_assume_single_core\" cfg flag to be enabled. See https://docs.rs/portable-atomic/latest/portable_atomic/#optional-cfg.");

#[cfg(all(feature = "barrier", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "barrier",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "barrier")))]
pub mod barrier;
#[cfg(all(feature = "lazy", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "lazy",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "lazy")))]
pub mod lazy;
#[cfg(all(feature = "mutex", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "mutex",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "mutex")))]
pub mod mutex;
#[cfg(all(feature = "once", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "once",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "once")))]
pub mod once;
pub mod relax;
#[cfg(all(feature = "rwlock", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub mod rwlock;

#[cfg(all(feature = "mutex", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "mutex",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "mutex")))]
pub use mutex::MutexGuard;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub use relax::Yield;
pub use relax::{RelaxStrategy, Spin};
#[cfg(all(feature = "rwlock", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub use rwlock::RwLockReadGuard;

Expand All @@ -110,39 +155,69 @@ pub use rwlock::RwLockReadGuard;
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(feature = "barrier", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "barrier",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "barrier")))]
pub type Barrier = crate::barrier::Barrier;

/// A value which is initialized on the first access. See [`lazy::Lazy`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(feature = "lazy", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "lazy",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "lazy")))]
pub type Lazy<T, F = fn() -> T> = crate::lazy::Lazy<T, F>;

/// A primitive that synchronizes the execution of multiple threads. See [`mutex::Mutex`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(feature = "mutex", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "mutex",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "mutex")))]
pub type Mutex<T> = crate::mutex::Mutex<T>;

/// A primitive that provides lazy one-time initialization. See [`once::Once`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(feature = "once", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "once",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "once")))]
pub type Once<T = ()> = crate::once::Once<T>;

/// A lock that provides data access to either one writer or many readers. See [`rwlock::RwLock`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(feature = "rwlock", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub type RwLock<T> = crate::rwlock::RwLock<T>;

Expand All @@ -151,15 +226,27 @@ pub type RwLock<T> = crate::rwlock::RwLock<T>;
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(feature = "rwlock", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub type RwLockUpgradableGuard<'a, T> = crate::rwlock::RwLockUpgradableGuard<'a, T>;

/// A guard that provides mutable data access. See [`rwlock::RwLockWriteGuard`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(feature = "rwlock", any(not(feature = "portable_atomic"), all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))))]
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub type RwLockWriteGuard<'a, T> = crate::rwlock::RwLockWriteGuard<'a, T>;

Expand Down

0 comments on commit 0e0ddca

Please sign in to comment.