Add C11 standard atomic support #1645
No reviewers
Labels
No labels
bazel
bluetooth
board settings
boot_stage2
bug
build
c++
can't reproduce
clang
clib-interface
cmsis
code cleanliness
code size
compiler version issues
cyw43_driver
debug
discussion
documentation
duplicate
dynamic_clock
elf2uf2
enhancement
errata
filesystem
flash layout
floating point
FreeRTOS
hardware_adc
hardware_claim
hardware_clocks
hardware_dma
hardware_exception
hardware_flash
hardware_gpio
hardware_i2c
hardware_irq
hardware_pio
hardware_pwm
hardware_rcp
hardware_rtc
hardware_spi
hardware_sync
hardware_uart
hardware_vreg
hardware_watchdog
hardware_xosc
help wanted
host
IAR
installation
invalid
linker scripts
lto
lwip
mbedtls
missing API
needs reporter detail
networking
newlib
newlib-multicore
non-c
non-cmake
non-gcc
openocd
orphan PR
OS integration
pico_async_context
pico_base
pico_binary_info
pico_flash
picolibc
pico_lwip
pico_malloc
pico_multicore
pico_printf
pico_runtime
pico_stdio
pico_stdio_usb
pico_sync
pico_time
pico_util
pioasm
port
question
review
review2
rtos interop
runtime init
static library
svd
test
tinyusb
vscode
wifi integration
wontfix
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-raspberrypi/pico-sdk!1645
Loading…
Reference in a new issue
No description provided.
Delete branch "add-atomic-support"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR Fixes #1642 Runtime for C11 atomics missing.
The file pico_atomic.c implements the required functions for the read/modify/write function of atomic variables with sizes of 1, 2, 4 and 8 bytes. Atomic load and store for 8 bytes quantities is also supported. Support for atomic_flags is also present.
To eliminate interference with existing hardware spinlock usage while reducing multicore contention on unique atomic variables, we use one of the watchdog scratch registers (WATCHDOG_SCRATCH3) to implement 16, 2 bit, multicore locks, via a variation of Dekker's Algorithm. The lock is selected as a function of the variable address and the stripe width which hashes variables addresses to locks numbers and leverages the RP2040 Atomic Register Access functionality of the WATCHDOG scratch registers.
Clearly the current approach (See __atomic_lock) is slower than using the RP2040 Hardware Spinlocks directly, but I was not sure how to assign spin locks from the reserved range (IDs 0-13) and was concerned about multicore lock contention when the number of hash buckets (hardware spinlocks) is minimized. Any recommendations concerning the number of spins lock to allocated to this library along with the stripping width (number of addresses hashed to the same lock) would greatly appreciated? My thoughts are four to eight locks with a stripe width of four bytes. Using the hardware spinlocks is an easy update.
As motivation to add this PR, below is a sample ticket based spin lock implementation similar to the Linux kernel. This type of implementation removes the limitation of 32 hardware spin locks at the minimal cost of potential hardware spin lock contention.
@sgstreet ; I'm guessing you may have closed this because we hadn't gotten to it yet. It is a useful PR, if you are still OK with us merging it; we generally add our own copyright to yours
Sure, let me update the PR there was a bug that needs to be fixed.
@ -0,0 +8,4 @@#ifndef __STDATOMIC_H#define __STDATOMIC_H#include_next <stdatomic.h>phew; i was wondering what versions of compilers supported this, but GCC 6 is happy, so OK i guess!
@ -0,0 +15,4 @@#include "pico/config.h"#ifndef __optimize#define __optimize __attribute__((optimize("-Os")))is there a reason for Os by default? note i am merging this now as I want to test it with something else, but may fixup later
@ -0,0 +8,4 @@#ifndef __STDATOMIC_H#define __STDATOMIC_H#include_next <stdatomic.h>I could drop a complete replacement for stdatomic.h as different approach? Seem like that would be worse in the long term.
@ -0,0 +8,4 @@#ifndef __STDATOMIC_H#define __STDATOMIC_H#include_next <stdatomic.h>yeah; i am happy with
include_nexti just thought it was a recent add@ -0,0 +15,4 @@#include "pico/config.h"#ifndef __optimize#define __optimize __attribute__((optimize("-Os")))From my experience, you get better code from -Os on the cortex parts. Less and generally faster code, but our mileage may vary. I can remove if you wish.
Sort of a adjacent question is if the atomic code should be forced in to SRAM?
@ -0,0 +15,4 @@#include "pico/config.h"#ifndef __optimize#define __optimize __attribute__((optimize("-Os")))good question; in similar bits of code we have a
#defineto pickNo, i think
-Osis fine, and probably as you say better in many circumstances (our default being -O3 is a historic cmake thing, and i am worried about knock on effects on existing stuff if i change it)Thanks, I have merged this for now somewhat rapidly as we have something internal dependent on it...
Glad you and the team found a use for this! Let me know if there is a problem or something else the team would like changed.
cool; i have actually backed it out again temporarily, as it seems certain versions of GCC complain about a few things
If I could know which compiler versions and what problems cropped up I can see if there is an easy fix?
@ -0,0 +15,4 @@#include "pico/config.h"#ifndef __optimize#define __optimize __attribute__((optimize("-Os")))Note that this makes the code incompatible with Clang which doesn't support the
optimizeattribute which is GCC specific. A correct way to achieve this without relying on non-portable behavior is to set the optimization level for this specific target through the build system.@ -0,0 +15,4 @@#include "pico/config.h"#ifndef __optimize#define __optimize __attribute__((optimize("-Os")))I tend not to use clang on my projects. I will remove this momentarily.
@ -0,0 +15,4 @@#include "pico/config.h"#ifndef __optimize#define __optimize __attribute__((optimize("-Os")))I updated the branching underlying this PR, but since the PR has been merged, closed and reverted I'm not sure how to get github to see the PR updates. @kilograham @petrhosek I think we will need a new PR. See PR #1763
I create PR #1763 to reopen this PR and remove the GCC specific optimize attribute.