-
Notifications
You must be signed in to change notification settings - Fork 112
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
lazy_static uses spinlocks in my with-std crate #150
Comments
|
Yes, that's probably rust-lang/cargo#2524.
Which one would that be?
The fact remains though that any crate making a similar mistake anywhere in the crate tree will globally enable spinlocks. That seems like a huge footgun. |
I was writing it. :) It's rust-random/getrandom#50. Yes, So I think fixing the feature handling bug in Cargo should take priority. |
I think a "proper" solution would be a locking crate that uses parking_lot when there is an OS and falls back to spinning when there is not. That would basically be a |
Thanks @RalfJung! Yeh this sounds like a footgun, even though I'd kind of expect those wasted cycles to be amortized over the complete running time of the process. It would at least be better if we had an opt-out So the "proper" solution of an external locking crate that can determine what strategy to use opaquely sounds like our best way forward to me. I'm guessing this crate could rely on |
Cc @matklad who seems to recently be interested in spinlocks ;) |
I'd love to push back on these two proposals. First, Second, even it is genuinely no-OS scenario, you can't "just" use a spinlock and call it a day, at least because there are two kinds of spinlocks you may use in on bare metal (saving/notsaving interrupts), and it is wrong for the library to pick any particular one: https://www.kernel.org/doc/Documentation/locking/spinlocks.txt. |
That makes sense. Do you have any alternative proposals though? What you say sounds a lot like " |
That's my opinion, yeah. My hypothesis is that most of the uses of
In the ideal world, yes. In the real world, the API is published and stable, and I think removing it will cause more churn than potential benefits. Hopefully, the problem will solve itself once we agree on, implement and stabilize standard lazy type. |
I have a tiny crate (really just a crate to test miri) where I noticed that after a
cargo update
,lazy_static
depends onspin
. Or, at least,spin
gets compiled. (If you want to reproduce, get that folder and docargo update
; I couldn't land the updated version yet due to other problems.)Isn't that a problem? The fact that
spin_no_std
is a feature means that if any create enables it, then globally all lazy statics will use a spin lock. Given that there is no way to know what kind of computation is done in the initializer, spinning instead of properly putting waiting threads to sleep seems like a huge waste of CPU cycles.I am not entirely sure what exactly is enabling the feature in my case, the reverse dependency graph of
spin
is not very big:@Mark-Simulacrum on Zulip suggested that Cargo ignores
cfg
restrictions for feature flags, and thusgetrandom
is likely the trigger here. Cc @newpavlovFWIW, the fact that there is a negated term ("no") in the feature also indicates this is probably a bad approach, see the feature name guidelines. Cargo features are additive; creates can only ask for a feature to be enabled but never ask for a feature to be disabled.
The text was updated successfully, but these errors were encountered: