Skip to content

Commit

Permalink
px4_atomic: Overload __atomic_always_lock_free for RISC-V
Browse files Browse the repository at this point in the history
The macro __atomic_always_lock_free is lying on some compiler versions;
RISC-V does have subword atomics, but for some reason the macro refuses
to understand this.

We know for a fact that all sizes <= sizeof(pointer) are atomic, so
overload the macro to fix px4::atomic for RISC-V.

This also allows removing the implicit typing of atomic<bool> as
atomic<int>
  • Loading branch information
pussuw committed May 8, 2024
1 parent dcc8729 commit a980285
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions platforms/common/include/px4_platform_common/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@
# include <nuttx/irq.h>
#endif // __PX4_NUTTX

/* With RISC-V the compiler / __atomic_always_lock_free lies about the atomicy
* of subword size variables. It is unclear whether this is intentional, or a
* bug.
*
* We know for a fact that subword atomics exist, so overload the the GCC macro
* here.
*
* More on this subject can be found here:
* https://github.com/riscv-collab/riscv-gcc/issues/15
*/

#ifdef CONFIG_ARCH_RISCV
#undef __atomic_always_lock_free
#define __atomic_always_lock_free(size, ptr) ((size) <= sizeof(uintptr_t))
#endif

namespace px4
{

Expand Down Expand Up @@ -289,17 +305,7 @@ class atomic

using atomic_int = atomic<int>;
using atomic_int32_t = atomic<int32_t>;

/* On riscv64-unknown-elf atomic<bool> is not quaranteed to be lock-free
* It is unclear whether it is really required.
* An optimal solution could be atomic_flag, but it doesn't seem to be available
* Just use atomic ints for now, to be safe
*/
#if !defined(CONFIG_ARCH_RISCV)
using atomic_bool = atomic<bool>;
#else
using atomic_bool = atomic<int>;
#endif

} /* namespace px4 */

Expand Down

0 comments on commit a980285

Please sign in to comment.