-
Notifications
You must be signed in to change notification settings - Fork 58
rpi-pico: Multicore-safe atomics using hardware spinlocks. #61
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
Open
damaki
wants to merge
3
commits into
AdaCore:master
Choose a base branch
from
damaki:rp2040-atomics-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| ------------------------------------------------------------------------------ | ||
| -- -- | ||
| -- GNAT RUN-TIME COMPONENTS -- | ||
| -- -- | ||
| -- Copyright (C) 2022, Daniel King -- | ||
| -- Copyright (C) 2022, AdaCore -- | ||
| -- -- | ||
| -- GNAT is free software; you can redistribute it and/or modify it under -- | ||
| -- terms of the GNU General Public License as published by the Free Soft- -- | ||
| -- ware Foundation; either version 3, or (at your option) any later ver- -- | ||
| -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- | ||
| -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | ||
| -- or FITNESS FOR A PARTICULAR PURPOSE. -- | ||
| -- -- | ||
| -- As a special exception under Section 7 of GPL version 3, you are granted -- | ||
| -- additional permissions described in the GCC Runtime Library Exception, -- | ||
| -- version 3.1, as published by the Free Software Foundation. -- | ||
| -- -- | ||
| -- You should have received a copy of the GNU General Public License and -- | ||
| -- a copy of the GCC Runtime Library Exception along with this program; -- | ||
| -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- | ||
| -- <http://www.gnu.org/licenses/>. -- | ||
| -- -- | ||
| -- GNAT was originally developed by the GNAT team at New York University. -- | ||
| -- Extensive contributions were provided by Ada Core Technologies Inc. -- | ||
| -- -- | ||
| ------------------------------------------------------------------------------ | ||
|
|
||
| with System.Machine_Code; use System.Machine_Code; | ||
| with System.BB.Parameters; | ||
| with Interfaces; use Interfaces; | ||
| with Interfaces.RP2040.SIO; use Interfaces.RP2040.SIO; | ||
|
|
||
| package body System.BB.RP2040_Atomic is | ||
|
|
||
| ------------- | ||
| -- PRIMASK -- | ||
| ------------- | ||
|
|
||
| function PRIMASK return Unsigned_32 is | ||
| R : Unsigned_32; | ||
| begin | ||
| Asm ("mrs %0, PRIMASK", Outputs => Unsigned_32'Asm_Output ("=r", R), | ||
| Volatile => True); | ||
| return R; | ||
| end PRIMASK; | ||
|
|
||
| ------------------------ | ||
| -- Interrupt_Disabled -- | ||
| ------------------------ | ||
|
|
||
| function Interrupt_Disabled return Boolean | ||
| is ((PRIMASK and 1) /= 0); | ||
|
|
||
| ------------------------ | ||
| -- Disable_Interrupts -- | ||
| ------------------------ | ||
|
|
||
| procedure Disable_Interrupts is | ||
| begin | ||
| Asm ("cpsid i" & ASCII.CR & ASCII.LF | ||
| & "dsb" & ASCII.CR & ASCII.LF | ||
| & "isb", | ||
| Clobber => "memory", | ||
| Volatile => True); | ||
| end Disable_Interrupts; | ||
|
|
||
| ----------------------- | ||
| -- Enable_Interrupts -- | ||
| ----------------------- | ||
|
|
||
| procedure Enable_Interrupts is | ||
| begin | ||
| Asm ("cpsie i" & ASCII.CR & ASCII.LF | ||
| & "dsb" & ASCII.CR & ASCII.LF | ||
| & "isb", | ||
| Clobber => "memory", | ||
| Volatile => True); | ||
| end Enable_Interrupts; | ||
|
|
||
| ------------------- | ||
| -- Spinlock_Lock -- | ||
| ------------------- | ||
|
|
||
| procedure Spinlock_Lock is | ||
| use type Interfaces.RP2040.UInt32; | ||
| begin | ||
| -- Reads 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. | ||
| loop | ||
| exit when SIO_Periph.SPINLOCK31 /= 0; | ||
| end loop; | ||
| end Spinlock_Lock; | ||
|
|
||
| --------------------- | ||
| -- Spinlock_Unlock -- | ||
| --------------------- | ||
|
|
||
| procedure Spinlock_Unlock is | ||
| begin | ||
| -- Write any value to release the lock | ||
| SIO_Periph.SPINLOCK31 := 0; | ||
| end Spinlock_Unlock; | ||
|
|
||
| -------------------- | ||
| -- Atomic_Wrapper -- | ||
| -------------------- | ||
|
|
||
| procedure Atomic_Wrapper is | ||
| Already_Disabled : constant Boolean := Interrupt_Disabled; | ||
| -- Make sure not to change the status of interrupt control by checking | ||
| -- if they are enabled when entering the function. | ||
| begin | ||
|
|
||
| if not Already_Disabled then | ||
| Disable_Interrupts; | ||
| end if; | ||
|
|
||
| if System.BB.Parameters.Multiprocessor then | ||
| Spinlock_Lock; | ||
| end if; | ||
|
|
||
| Wrapped_Proc; | ||
|
|
||
| if System.BB.Parameters.Multiprocessor then | ||
| Spinlock_Unlock; | ||
| end if; | ||
|
|
||
| -- If the interrupts were disabled when entering this function, we do | ||
| -- not want enable them. | ||
| if not Already_Disabled then | ||
| Enable_Interrupts; | ||
| end if; | ||
| end Atomic_Wrapper; | ||
|
|
||
| ---------------------------- | ||
| -- Sync_Lock_Test_And_Set -- | ||
| ---------------------------- | ||
|
|
||
| function Sync_Lock_Test_And_Set (Addr : System.Address; | ||
| Value : T) | ||
| return T | ||
| is | ||
| Data : T with Address => Addr; | ||
| Ret : T; | ||
|
|
||
| procedure Inner | ||
| with Inline_Always; | ||
|
|
||
| procedure Inner | ||
| is | ||
| begin | ||
| Ret := Data; | ||
| Data := Value; | ||
| end Inner; | ||
|
|
||
| procedure Atomic_Action is new Atomic_Wrapper (Inner); | ||
|
|
||
| begin | ||
| Atomic_Action; | ||
| return Ret; | ||
| end Sync_Lock_Test_And_Set; | ||
|
|
||
| -------------------------------- | ||
| -- Sync_Bool_Compare_And_Swap -- | ||
| -------------------------------- | ||
|
|
||
| function Sync_Bool_Compare_And_Swap (Addr : System.Address; | ||
| Old_Value : T; | ||
| New_Value : T) | ||
| return Interfaces.C.char | ||
| is | ||
| Data : T with Address => Addr; | ||
| Ret : Interfaces.C.char; | ||
|
|
||
| procedure Inner | ||
| with Inline_Always; | ||
|
|
||
| procedure Inner | ||
| is | ||
| begin | ||
| if Data = Old_Value then | ||
| Data := New_Value; | ||
| Ret := Interfaces.C.char'Succ (Interfaces.C.nul); -- True | ||
| else | ||
| Ret := Interfaces.C.nul; -- False | ||
| end if; | ||
| end Inner; | ||
|
|
||
| procedure Atomic_Action is new Atomic_Wrapper (Inner); | ||
|
|
||
| begin | ||
| Atomic_Action; | ||
| return Ret; | ||
| end Sync_Bool_Compare_And_Swap; | ||
|
|
||
| end System.BB.RP2040_Atomic; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.