mvdnes / spin-rs

Spin-based synchronization primitives
MIT License
485 stars 92 forks source link

Expose weak versions of CAS methods #150

Closed tsoutsman closed 1 year ago

tsoutsman commented 1 year ago

Hi,

I'm working on implementing sync primitives in Theseus OS on top of spin locks. We currently have to implement our own spin lock that is identical to spin except that it exposes try_lock_weak, so that we can have custom lock implementations that use a weak CMPXCHG, while still using a strong CMPXCHG for try_lock.

It would be nice if we could upstream this so that we could directly depend on spin. I think it makes sense to include it as spin is often used to build higher-level primitives which could use a weak CMPXCHG for better performance.

zesterer commented 1 year ago

Are you looking to see a new release with this feature?

tsoutsman commented 1 year ago

It would be nice but not critical. We're happy to just use a patch manifest section.

tsoutsman commented 1 year ago

Sorry, I'm currently implementing our blocking spin lock and noticed there isn't a way to access the reader count from the guard.

Ideally, force_read_decrement would return the previous count so that we know when no readers are left. However, this would be a breaking change and require exposing rw_lock::READER, which is an implementation detail, so I think it may be better if we forked spin rather than upstreaming that change, considering how niche our use case is and how many dependents spin has.