Git Product home page Git Product logo

Comments (9)

zesterer avatar zesterer commented on August 18, 2024

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.

zesterer avatar zesterer commented on August 18, 2024

There's also some potentially relevant discussion on this thread

from spin-rs.

zesterer avatar zesterer commented on August 18, 2024

This sounds like the same issue.

from spin-rs.

jimblandy avatar jimblandy commented on August 18, 2024

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, define mutex::yield::SpinMutex that is not async-signal-safe but that uses OS features to yield. Case b) would request the std feature and use spin::mutex::yield::SpinMutex.
  • Define mutex::general::SpinMutex as an alias for mutex::yield::SpinMutex if available, or mutex::no_yield::SpinMutex otherwise. Case c) would use mutex::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.

 avatar commented on August 18, 2024

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.

zesterer avatar zesterer commented on August 18, 2024

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.

Ericson2314 avatar Ericson2314 commented on August 18, 2024

Yeah yield policy parameter + features for default yield policy sounds good.

from spin-rs.

zesterer avatar zesterer commented on August 18, 2024

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.

zesterer avatar zesterer commented on August 18, 2024

#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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.