Skip to content

Commit 01b3be3

Browse files
committed
rtapi: Fix rtapi atomics to work with clang/clang++
1 parent 4e9fc3b commit 01b3be3

8 files changed

Lines changed: 52 additions & 49 deletions

File tree

src/hal/hal.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,6 @@ typedef union {
339339
hal_u64_t lu;
340340
} hal_data_u;
341341

342-
typedef struct {
343-
volatile unsigned int read; //offset into buff that outgoing data gets read from
344-
volatile unsigned int write; //offset into buff that incoming data gets written to
345-
unsigned int size; //size of allocated buffer
346-
char buff[];
347-
} hal_port_shm_t;
348-
349342
/***********************************************************************
350343
* "LOCKING" FUNCTIONS *
351344
************************************************************************/
@@ -945,6 +938,7 @@ union hal_stream_data {
945938
uint32_t u;
946939
};
947940

941+
struct hal_stream_shm; // Forward declaration. Only relevant in hal_lib.c.
948942
typedef struct {
949943
int comp_id, shmem_id;
950944
struct hal_stream_shm *fifo;

src/hal/hal_lib.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3762,6 +3762,15 @@ static char *halpr_type_string(int type, char *buf, size_t nbuf) {
37623762
HAL PORT functions
37633763
******************************************************************************/
37643764

3765+
// This struct is purely local to this file to keep track of the
3766+
// port data in shmem.
3767+
typedef struct __hal_port_shm_t {
3768+
atomic_uint read; // offset into buff that outgoing data gets read from
3769+
atomic_uint write; // offset into buff that incoming data gets written to
3770+
unsigned int size; // size of allocated buffer
3771+
char buff[];
3772+
} hal_port_shm_t;
3773+
37653774
static void hal_port_atomic_load(hal_port_shm_t* port_shm, unsigned* read, unsigned* write)
37663775
{
37673776
*read = atomic_load_explicit(&port_shm->read, memory_order_acquire);
@@ -4067,6 +4076,22 @@ void hal_port_wait_writable(hal_port_t** port, unsigned count, sig_atomic_t* sto
40674076
/*
40684077
hal stream implementation
40694078
*/
4079+
4080+
// This struct is purely local to this file to keep track of the stream
4081+
// data in shmem.
4082+
struct hal_stream_shm {
4083+
unsigned magic;
4084+
atomic_uint in;
4085+
atomic_uint out;
4086+
unsigned this_sample;
4087+
unsigned depth;
4088+
int num_pins;
4089+
atomic_uint num_overruns;
4090+
atomic_uint num_underruns;
4091+
hal_type_t type[HAL_STREAM_MAX_PINS];
4092+
union hal_stream_data data[];
4093+
};
4094+
40704095
int halpr_parse_types(hal_type_t type[HAL_STREAM_MAX_PINS], const char *cfg)
40714096
{
40724097
const char *c;

src/hal/hal_priv.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -494,17 +494,6 @@ extern int hal_port_alloc(unsigned size, hal_port_t *port);
494494

495495

496496
#define HAL_STREAM_MAGIC_NUM 0x4649464F
497-
struct hal_stream_shm {
498-
unsigned magic;
499-
volatile unsigned in;
500-
volatile unsigned out;
501-
unsigned this_sample;
502-
unsigned depth;
503-
int num_pins;
504-
unsigned long num_overruns, num_underruns;
505-
hal_type_t type[HAL_STREAM_MAX_PINS];
506-
union hal_stream_data data[];
507-
};
508497

509498
extern int halpr_parse_types(hal_type_t type[HAL_STREAM_MAX_PINS], const char *fcg);
510499
RTAPI_END_DECLS

src/rtapi/rtapi_atomic.h

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 Jeff Epler
2+
// Copyright 2026 B.Stultiens
23
//
34
// This program is free software; you can redistribute it and/or modify
45
// it under the terms of the GNU General Public License as published by
@@ -16,37 +17,33 @@
1617
#ifndef __LINUXCNC_RTAPI_ATOMIC_H
1718
#define __LINUXCNC_RTAPI_ATOMIC_H
1819

19-
#if defined(__GNUC__) && ((__GNUC__ << 8) | __GNUC_MINOR__) >= 0x409
20+
#if defined(__cplusplus)
21+
22+
// We use C++20 and that has all the atomics we need
23+
#include <atomic>
24+
25+
#else // defined(__cplusplus)
26+
27+
// Standard C, we require C11 or better
28+
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
2029
#define RTAPI_USE_STDATOMIC
21-
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
30+
#elif defined(__GNUC__) && ((__GNUC__ << 8) | __GNUC_MINOR__) >= 0x409
2231
#define RTAPI_USE_STDATOMIC
2332
#endif
2433

25-
#ifdef RTAPI_USE_STDATOMIC
34+
#if defined(RTAPI_USE_STDATOMIC)
2635
#include <stdatomic.h>
27-
#else
2836

29-
#warning "Old compiler has no C11 atomics. Please consider upgrading your compiler."
30-
31-
enum memory_order {
32-
memory_order_relaxed,
33-
memory_order_consume,
34-
memory_order_acquire,
35-
memory_order_release,
36-
memory_order_acq_rel,
37-
memory_order_seq_cst
38-
};
37+
#if defined(__STDC_NO_ATOMICS__)
38+
#error "Your compiler/libc has set __STDC_NO_ATOMICS__ and atomics are required."
39+
#endif
3940

40-
#define atomic_store(obj, desired) atomic_store_explicit((obj), (desired), memory_order_seq_cst)
41-
#define atomic_load(obj) atomic_load_explicit((obj), memory_order_seq_cst)
41+
#else // defined(RTAPI_USE_STDATOMIC)
4242

43-
// note that in this implementation, only one level of synchronization is supported, equivalent to memory_order_seq_cst
44-
#define atomic_store_explicit(obj, desired, order) \
45-
({ (void)order; __sync_synchronize(); *(obj) = (desired); (void)0; })
43+
#error "Old compiler has no C11 atomics. Please upgrade your compiler to support C11 or better."
4644

47-
#define atomic_load_explicit(obj, order) \
48-
({ (void)order; __typeof__(*(obj)) v = *(obj); __sync_synchronize(); v; })
45+
#endif // defined(RTAPI_USE_STDATOMIC)
4946

50-
#endif
47+
#endif // defined(__cplusplus)
5148

5249
#endif

src/rtapi/rtapi_uspace.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#endif
2323
#include <unistd.h>
2424
#include <pthread.h>
25-
#include <atomic>
25+
#include "rtapi_atomic.h"
2626

2727
static inline void rtapi_timespec_add(timespec &result, const timespec &ta, const timespec &tb) {
2828
result.tv_sec = ta.tv_sec + tb.tv_sec;
@@ -45,7 +45,7 @@ struct WithRoot
4545
{
4646
WithRoot();
4747
~WithRoot();
48-
static std::atomic<int> level;
48+
static std::atomic_int level;
4949
};
5050

5151
struct rtapi_task {

src/rtapi/uspace_rtai.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#pragma GCC diagnostic ignored "-Wnarrowing"
66
#include <rtai_lxrt.h>
77
#pragma GCC diagnostic pop
8-
#include <atomic>
98
#ifdef HAVE_SYS_IO_H
109
#include <sys/io.h>
1110
#endif
@@ -16,7 +15,7 @@ RtapiApp *app;
1615

1716
struct RtaiTask : rtapi_task {
1817
RtaiTask() : rtapi_task{}, cancel{}, rt_task{}, thr{} {}
19-
std::atomic<int> cancel;
18+
std::atomic_int cancel;
2019
RT_TASK *rt_task;
2120
pthread_t thr;
2221
};

src/rtapi/uspace_rtapi_app.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
#include <pthread_np.h>
5252
#endif
5353

54+
#include <boost/lockfree/queue.hpp>
55+
5456
#include "rtapi.h"
5557
#include <hal.h>
5658
#include "hal/hal_priv.h"
5759
#include "rtapi_uspace.hh"
5860

59-
#include <boost/lockfree/queue.hpp>
60-
61-
std::atomic<int> WithRoot::level;
61+
std::atomic_int WithRoot::level;
6262
static uid_t euid, ruid;
6363

6464
#include "rtapi/uspace_common.h"

src/rtapi/uspace_xenomai.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <errno.h>
66
#include <stdio.h>
77
#include <cstring>
8-
#include <atomic>
98
#ifdef HAVE_SYS_IO_H
109
#include <sys/io.h>
1110
#endif
@@ -14,7 +13,7 @@ namespace
1413
{
1514
struct RtaiTask : rtapi_task {
1615
RtaiTask() : rtapi_task{}, cancel{}, thr{} {}
17-
std::atomic<int> cancel;
16+
std::atomic_int cancel;
1817
pthread_t thr;
1918
};
2019

0 commit comments

Comments
 (0)