Skip to content

perf: inline everything #17

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

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct RawSpinlock {
impl RawSpinlock {
// Can fail to lock even if the spinlock is not locked. May be more efficient than `try_lock`
// when called in a loop.
#[inline]
fn try_lock_weak(&self) -> bool {
// The Orderings are the same as try_lock, and are still correct here.
self.locked
Expand All @@ -48,6 +49,7 @@ unsafe impl RawMutex for RawSpinlock {
// A spinlock guard can be sent to another thread and unlocked there
type GuardMarker = GuardSend;

#[inline]
fn lock(&self) {
while !self.try_lock_weak() {
// Wait until the lock looks unlocked before retrying
Expand All @@ -59,6 +61,7 @@ unsafe impl RawMutex for RawSpinlock {
}
}

#[inline]
fn try_lock(&self) -> bool {
// Code taken from:
// https://github.com/Amanieu/parking_lot/blob/fa294cd677936bf365afa0497039953b10c722f5/lock_api/src/lib.rs#L49-L53
Expand All @@ -74,10 +77,12 @@ unsafe impl RawMutex for RawSpinlock {
.is_ok()
}

#[inline]
unsafe fn unlock(&self) {
self.locked.store(false, Ordering::Release);
}

#[inline]
fn is_locked(&self) -> bool {
// Relaxed is sufficient because this operation does not provide synchronization, only atomicity.
self.locked.load(Ordering::Relaxed)
Expand Down Expand Up @@ -197,6 +202,7 @@ pub type MappedSpinlockGuard<'a, T> = lock_api::MappedMutexGuard<'a, RawSpinlock
///
/// static SPINLOCK: Spinlock<i32> = const_spinlock(42);
/// ```
#[inline]
pub const fn const_spinlock<T>(val: T) -> Spinlock<T> {
Spinlock::const_new(<RawSpinlock as lock_api::RawMutex>::INIT, val)
}
Expand Down