Skip to content

Commit c836fff

Browse files
committed
hal: access 64-bit pin values with relaxed atomics
Since the move to 64-bit HAL types every integer pin is 64 bits wide. On 32-bit targets the plain volatile accessors compile to two 32-bit accesses (two movl on i386), so a reader in another thread can see half of a write: at a zero crossing the value is off by 2^32. A test with one thread toggling a sint pin between -1 and 0 and another reading it sees millions of torn values per second on i386, and still about 30 per second pinned to one CPU. A real path is a userspace jog wheel (e.g. shuttle) writing counts that the servo thread reads as joint.N.jog-counts. Use __atomic_load_n/__atomic_store_n (__atomic_load/__atomic_store for real) with __ATOMIC_RELAXED in the getters and setters. On amd64 and arm64 this compiles to the same single loads and stores as before; on i386 it is fildq/fistpq and on ARMv7 ldrexd/strexd. The same test sees no torn values. This also makes real pins atomic on ARMv7, where vldr/vstr were not guaranteed to be. Without lock-free 64-bit atomics the compiler falls back to libatomic, whose locks are process-local and do not protect HAL shared memory, so configure now fails on such targets (e.g. i486, ARM before ARMv6K), as requested by Bertho Stultiens in #4614.
1 parent eab7726 commit c836fff

2 files changed

Lines changed: 43 additions & 16 deletions

File tree

‎src/configure.ac‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,22 @@ elif ! test `$CC -dumpversion | cut -d '.' -f 1` -gt 2 ; then
419419
AC_MSG_ERROR([Compilers older than gcc 3.x are no longer supported])
420420
fi
421421

422+
#
423+
# HAL pin accessors use 64-bit atomics. A non-lock-free fallback (libatomic)
424+
# uses process-local locks, which do not protect HAL shared memory.
425+
#
426+
AC_MSG_CHECKING([for lock-free 64-bit atomics])
427+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
428+
#include <stdint.h>
429+
_Static_assert(__atomic_always_lock_free(sizeof(int64_t), 0), "64-bit integer");
430+
_Static_assert(__atomic_always_lock_free(sizeof(double), 0), "64-bit double");
431+
]], [[]])],[
432+
AC_MSG_RESULT(yes)
433+
],[
434+
AC_MSG_RESULT(no)
435+
AC_MSG_ERROR([$CC has no lock-free 64-bit atomics for this target. HAL needs them, e.g. i586 or later on x86, ARMv6K or later on ARM.])
436+
])
437+
422438
AC_MSG_CHECKING([for usability of linux/hidraw.h])
423439
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
424440
#include <sys/ioctl.h>

‎src/hal/hal.h‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -469,19 +469,26 @@ typedef union {
469469
volatile rtapi_port _p; \
470470
}
471471

472+
// Pin values are accessed with relaxed atomics so that a reader in another
473+
// thread never sees half of a write (64-bit values on 32-bit targets). On
474+
// 64-bit targets these compile to plain loads and stores. configure checks
475+
// that 64-bit atomics are lock-free.
476+
#define __HAL_LOAD(m) __atomic_load_n(&((union __hal_mapped_type *)ref)->m, __ATOMIC_RELAXED)
477+
#define __HAL_STORE(m, v) __atomic_store_n(&((union __hal_mapped_type *)ref)->m, (v), __ATOMIC_RELAXED)
478+
472479

473480
// A 'hal_port_t' is a pin reference which content represents
474481
// the integer offset in the HAL shared memory segment to a
475482
// hal_port_shm_t structure.
476483
static inline __HAL_ALWAYS_INLINE rtapi_port hal_get_port(hal_port_t ref) {
477484
__HAL_MAPPED_TYPE;
478485
// cppcheck-suppress dangerousTypeCast
479-
return ((union __hal_mapped_type *)ref)->_p;
486+
return __HAL_LOAD(_p);
480487
}
481488
static inline __HAL_ALWAYS_INLINE rtapi_port hal_set_port(hal_port_t ref, rtapi_port val) {
482489
__HAL_MAPPED_TYPE;
483490
// cppcheck-suppress dangerousTypeCast
484-
((union __hal_mapped_type *)ref)->_p = val;
491+
__HAL_STORE(_p, val);
485492
return val;
486493
}
487494
//
@@ -496,7 +503,7 @@ static inline __HAL_ALWAYS_INLINE rtapi_s32 hal_get_si32_clamped(const hal_sint_
496503
__HAL_MAPPED_TYPE;
497504
// Down conversion from the larger type
498505
// cppcheck-suppress dangerousTypeCast
499-
rtapi_sint val = ((union __hal_mapped_type *)ref)->_s;
506+
rtapi_sint val = __HAL_LOAD(_s);
500507
if(val <= RTAPI_INT32_MIN) return RTAPI_INT32_MIN;
501508
if(val >= RTAPI_INT32_MAX) return RTAPI_INT32_MAX;
502509
return (rtapi_s32)val;
@@ -505,81 +512,85 @@ static inline __HAL_ALWAYS_INLINE rtapi_s32 hal_get_si32(const hal_sint_t ref) {
505512
__HAL_MAPPED_TYPE;
506513
// Implicitly Truncated from the larger type
507514
// cppcheck-suppress dangerousTypeCast
508-
return (rtapi_s32)((union __hal_mapped_type *)ref)->_s;
515+
return (rtapi_s32)__HAL_LOAD(_s);
509516
}
510517
static inline __HAL_ALWAYS_INLINE rtapi_s32 hal_set_si32(hal_sint_t ref, rtapi_s32 val) {
511518
__HAL_MAPPED_TYPE;
512519
// cppcheck-suppress dangerousTypeCast
513-
((union __hal_mapped_type *)ref)->_s = val; // Store in the larger type
520+
__HAL_STORE(_s, val); // Store in the larger type
514521
return val;
515522
}
516523
static inline __HAL_ALWAYS_INLINE rtapi_u32 hal_get_ui32_clamped(const hal_uint_t ref) {
517524
__HAL_MAPPED_TYPE;
518525
// Down conversion from the larger type
519526
// cppcheck-suppress dangerousTypeCast
520-
rtapi_uint val = ((union __hal_mapped_type *)ref)->_u;
527+
rtapi_uint val = __HAL_LOAD(_u);
521528
if(val >= RTAPI_UINT32_MAX) return RTAPI_UINT32_MAX;
522529
return (rtapi_u32)val;
523530
}
524531
static inline __HAL_ALWAYS_INLINE rtapi_u32 hal_get_ui32(const hal_uint_t ref) {
525532
__HAL_MAPPED_TYPE;
526533
// Implicitly Truncated from the larger type
527534
// cppcheck-suppress dangerousTypeCast
528-
return (rtapi_u32)((union __hal_mapped_type *)ref)->_u;
535+
return (rtapi_u32)__HAL_LOAD(_u);
529536
}
530537
static inline __HAL_ALWAYS_INLINE rtapi_u32 hal_set_ui32(hal_uint_t ref, rtapi_u32 val) {
531538
__HAL_MAPPED_TYPE;
532539
// cppcheck-suppress dangerousTypeCast
533-
((union __hal_mapped_type *)ref)->_u = val; // Store in the larger type
540+
__HAL_STORE(_u, val); // Store in the larger type
534541
return val;
535542
}
536543
static inline __HAL_ALWAYS_INLINE rtapi_sint hal_get_sint(const hal_sint_t ref) {
537544
__HAL_MAPPED_TYPE;
538545
// cppcheck-suppress dangerousTypeCast
539-
return ((union __hal_mapped_type *)ref)->_s;
546+
return __HAL_LOAD(_s);
540547
}
541548
static inline __HAL_ALWAYS_INLINE rtapi_sint hal_set_sint(hal_sint_t ref, rtapi_sint val) {
542549
__HAL_MAPPED_TYPE;
543550
// cppcheck-suppress dangerousTypeCast
544-
((union __hal_mapped_type *)ref)->_s = val;
551+
__HAL_STORE(_s, val);
545552
return val;
546553
}
547554
static inline __HAL_ALWAYS_INLINE rtapi_uint hal_get_uint(const hal_uint_t ref) {
548555
__HAL_MAPPED_TYPE;
549556
// cppcheck-suppress dangerousTypeCast
550-
return ((union __hal_mapped_type *)ref)->_u;
557+
return __HAL_LOAD(_u);
551558
}
552559
static inline __HAL_ALWAYS_INLINE rtapi_uint hal_set_uint(hal_uint_t ref, rtapi_uint val) {
553560
__HAL_MAPPED_TYPE;
554561
// cppcheck-suppress dangerousTypeCast
555-
((union __hal_mapped_type *)ref)->_u = val;
562+
__HAL_STORE(_u, val);
556563
return val;
557564
}
558565
static inline __HAL_ALWAYS_INLINE rtapi_real hal_get_real(const hal_real_t ref) {
559566
__HAL_MAPPED_TYPE;
567+
rtapi_real val;
560568
// cppcheck-suppress dangerousTypeCast
561-
return ((union __hal_mapped_type *)ref)->_r;
569+
__atomic_load(&((union __hal_mapped_type *)ref)->_r, &val, __ATOMIC_RELAXED);
570+
return val;
562571
}
563572
static inline __HAL_ALWAYS_INLINE rtapi_real hal_set_real(hal_real_t ref, rtapi_real val) {
564573
__HAL_MAPPED_TYPE;
565574
// cppcheck-suppress dangerousTypeCast
566-
((union __hal_mapped_type *)ref)->_r = val;
575+
__atomic_store(&((union __hal_mapped_type *)ref)->_r, &val, __ATOMIC_RELAXED);
567576
return val;
568577
}
569578
static inline __HAL_ALWAYS_INLINE rtapi_bool hal_get_bool(const hal_bool_t ref) {
570579
__HAL_MAPPED_TYPE;
571580
// cppcheck-suppress dangerousTypeCast
572-
return ((union __hal_mapped_type *)ref)->_b;
581+
return __HAL_LOAD(_b);
573582
}
574583
static inline __HAL_ALWAYS_INLINE rtapi_bool hal_set_bool(hal_bool_t ref, rtapi_bool val) {
575584
__HAL_MAPPED_TYPE;
576585
// 'val' is declared bool and will therefore store a one (1)
577586
// or a zero (0) in the larger target. This still works if the
578587
// call is made using an integer type as original argument.
579588
// cppcheck-suppress dangerousTypeCast
580-
((union __hal_mapped_type *)ref)->_u = val;
589+
__HAL_STORE(_u, val);
581590
return val;
582591
}
592+
#undef __HAL_STORE
593+
#undef __HAL_LOAD
583594
#undef __HAL_MAPPED_TYPE
584595

585596
/***********************************************************************

0 commit comments

Comments
 (0)