Comments (9)
Hello. This is an interesting (and frustrating) product of cargo's somewhat bizarre feature flag logic. This makes me wonder whether, instead of an std
feature, we should have a no_std
feature and have it be the default. Are you aware of any potential problems with such an approach?
Edit: One such problem could be that an std
-supporting crate might intend to use thread yielding, but then another crate pulls in spin
with no_std
enabled, causing priority inversion for the std
-supporting crate. This is obviously undesirable too. It's almost like we need mutually-exclusive std
and no_std
features, but I can't imagine that working well without breaking a lot of builds that pull in spin
multiple times with different features enabled. This is mentioned here.
from spin-rs.
There's also some potentially relevant discussion on this thread
from spin-rs.
This sounds like the same issue.
from spin-rs.
It seems to me that a client could have three possible requirements:
a) They may always need an async-signal-safe lock. (My case.)
b) They may always need an OS-friendly lock. (Currently, what people get if they request the std
feature.)
c) They may have no requirements themselves, but want to be usable in both std and no-std cases. (Currently what people get if they do not request the std
feature.)
I would say these are all useful and reasonable.
So perhaps what the crate should do is:
- Have a non-default
std
feature (as it does now). - Unconditionally define a
mutex::no_yield::SpinMutex
that is async-signal-safe. Case a) would use this. - If the
std
feature is enabled, definemutex::yield::SpinMutex
that is not async-signal-safe but that uses OS features to yield. Case b) would request thestd
feature anduse spin::mutex::yield::SpinMutex
. - Define
mutex::general::SpinMutex
as an alias formutex::yield::SpinMutex
if available, ormutex::no_yield::SpinMutex
otherwise. Case c) would usemutex::general::SpinMutex
The ticket_mutex
feature could also influence the definition of mutex::general::SpinMutex
.
By putting all three in submodules of spin::mutex
, users have to make an explicit choice, so they're unlikely to choose the general mutex by accident when they have stricter requirements. Users who don't have specific requirements and perhaps aren't informed about the issues will recognize that general
as the most-trodden not-doing-anything-weird path, which is correct. And since it's all done by use
statements, making the choice explicit doesn't really clutter up their code too much.
from spin-rs.
I'm wondering about an alternate approach that has the mutex generic over a "yield policy" (perhaps with a better name like "relax"). Something like the following:
pub trait Yield {
fn yield();
}
This crate could provide the existing policies like AtomicYield
that uses core::sync::atomic::spin_loop_hint
(and conditionally ThreadYield
that uses std:thread::yield_now
based on the std
feature), and would also allow clients to define their own yield policy for specific use cases. I also like the submodule idea above for clients that don't want to care about the general case of a custom policy.
from spin-rs.
This seems like a reasonable suggestion. If anybody wants to open a PR to add this I'd be grateful. If not, I'll have the time to implement it in a few weeks.
from spin-rs.
Yeah yield policy parameter + features for default yield policy sounds good.
from spin-rs.
I've opened #102. However, it still has unresolved problems relating to type inference (more information in the PR). If anybody has any ideas about this, please put them in the PR.
from spin-rs.
#102 has now been merged and included in version 0.8.0
. You can select a different relax strategy using an additional type parameter. For example, spin::mutex::Mutex<T, Spin>
or spin::mutex::Mutex<T, Yield>
. Note that you'll need to use the fully qualified path instead of the type aliases in the root of the crate due to the inference issue mentioned in the PR.
from spin-rs.
Related Issues (20)
- Inconsistent feature name HOT 2
- `lock_api` feature is not available in version 0.7.1 and 0.8.0. HOT 4
- Cannot borrow `lazy::Lazy<T>` mutably? HOT 5
- Remove `panicked` field from Once's Finish guard HOT 2
- Implement Once::get_mut_unchecked and Once::into_inner_unchecked HOT 4
- unwrap function in MutexGuard? HOT 4
- Request: Add support for atomic-polyfill HOT 7
- `Once::call_once` doesn't guarantee that the given closure is called only if this is the first time `call_once` has been called. HOT 2
- RwLock::try_read is unsound HOT 2
- MIRI build in CI is failing HOT 4
- portable_atomic feature does not compile HOT 2
- CI: Set minimal permissions on GitHub Workflow HOT 3
- Why was spin 0.9.6 yanked? HOT 2
- Unsoundness in `Once::try_call_once()`
- Are `rwlock` and `spin_mutex` compatible with `portable_atomic`? HOT 11
- `Lazy` panics under bare metal environment HOT 10
- Add a Security-Policy
- Add `std` feature with support for thread yielding HOT 1
- Switching to GitHub actions HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spin-rs.