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

Replace custom spinlock with spin #41

Closed
wants to merge 1 commit into from

Conversation

notgull
Copy link
Member

@notgull notgull commented Nov 21, 2022

This deduplicates the custom spinlock we have with the one that's currently used in the spin crate.

@taiki-e
Copy link
Collaborator

taiki-e commented Nov 22, 2022

Hmm, why do we have spinlock? Sorry for not being able to review your recent PRs, but I remember I disagreed with spinlock in a no-std related issue in this repository -- AFAIK, using spinlock around pure ops like arithmetic operations is fine, but using it around allocations is not always safe (may cause a deadlock). In fact, there is a report that the spinlock around the allocations at crossbeam-channel (recently removed) is believed to have caused a deadlock.

@taiki-e
Copy link
Collaborator

taiki-e commented Nov 22, 2022

Ah, it seems the current implementation doesn't have a problem with deadlock as it is try_lock only and the spinning is not unbounded.

@notgull
Copy link
Member Author

notgull commented Nov 22, 2022

Yes, the main idea is that it uses a spin lock in the case where it's theoretically ideal and falls back to an atomic queue during periods of high contention. I wrote a more detailed write-up in #34.

(Also, don't apologize for not being able to review my PRs. I know you're busy, and your well-being is more important than my code. 🙂)

@notgull
Copy link
Member Author

notgull commented Nov 26, 2022

Note than spin's master branch supports our MSRV as of mvdnes/spin-rs#131.

@@ -25,6 +25,7 @@ __test = []
crossbeam-utils = { version = "0.8.12", default-features = false }
parking = { version = "2.0.0", optional = true }
slab = { version = "0.4.7", default-features = false }
spin = { version = "0.9.4", default-features = false, features = ["spin_mutex"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo it would be more readable to replace this with

[dependencies.spin]
version = "0.9.4"
default-features = false
features = ["spin_mutex"]

fn drop(&mut self) {
self.mutex.locked.store(false, Ordering::Release);
}
guard: spin::MutexGuard<'a, T>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it make more sense to just do use spin::MutexGuard; here`

@notgull
Copy link
Member Author

notgull commented Mar 15, 2023

Taking another look at this, I'm not a fan of incurring the extra dependency.

@notgull notgull closed this Mar 15, 2023
@notgull notgull deleted the ext-spin branch March 15, 2023 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants