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

Added spinlocks #208

Merged
merged 2 commits into from
Nov 25, 2021
Merged

Added spinlocks #208

merged 2 commits into from
Nov 25, 2021

Conversation

VictorKoenders
Copy link
Contributor

@VictorKoenders VictorKoenders commented Nov 24, 2021

Implements spinlocks, a simple wrapper around the sio::spinlockX address.

As per documentation:

2.3.1.3. Hardware Spinlocks

The  SIO  provides  32  hardware  spinlocks,  which  can  be  used  to  manage  mutually-exclusive  access  to  shared  software
resources. Each spinlock is a one-bit flag, mapped to a different address (from SPINLOCK0 to SPINLOCK31). Software
interacts with each spinlock with one of the following operations:
• Read: attempt to claim the lock. Read value is nonzero if the lock was successfully claimed, or zero if the lock had
already been claimed by a previous read.
• Write (any value): release the lock. The next attempt to claim the lock will be successful.

If both cores try to claim the same lock on the same clock cycle, core 0 succeeds.

Generally  software  will  acquire  a  lock  by  repeatedly  polling  the  lock  bit  ("spinning"  on  the  lock)  until  it  is  successfully
claimed. This is inefficient if the lock is held for long periods, so generally the spinlocks should be used to protect the
short critical sections of higher-level primitives such as mutexes, semaphores and queues.

For debugging purposes, the current state of all 32 spinlocks can be observed via SPINLOCK_ST.

For usability I've added a Spinlock trait that all SpinlockX implement, so we can make an API like:

pub struct Mutex<T, S: Spinlock> {
    ...
}

let mutex = Mutex::<u32, SpinLock5>::new(32);
*mutex.lock() = 1234;

@9names
Copy link
Member

9names commented Nov 24, 2021

Would this be easier or harder if the spinlocks were in a single array as per
rp-rs/rp2040-pac#39

@VictorKoenders
Copy link
Contributor Author

Should be the same, the macro would have to take an index instead of a register. We can wait on that PR if you want

@9names
Copy link
Member

9names commented Nov 24, 2021

No need to wait, just wanted to check that it wouldn't be a problem to move to it later.

@jonathanpallant jonathanpallant merged commit ed860ed into rp-rs:main Nov 25, 2021
@jonathanpallant
Copy link
Contributor

Thank you for the PR! :)

@9names 9names mentioned this pull request Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants