Hello,
After the C11 spec was finalized, GCC added a bunch of new built-in
“__atomic_X” functions to replace the older “__sync_” functions. These
atomic functions are generally used by <stdatomic.h> headers, as they were
designed to adhere to the C11 memory model, but there’s nothing preventing
their use in "regular" code. These built-ins are rather convenient, as they
allow for atomic manipulations of regular data types, which is useful when
dealing with data which is shared between threads (or the main program and
an ISR in the case of an MCU). With these functions, it would no longer be
necessary to declare shared global variables as “volatile”, and it would no
longer be necessary to explicitly disable and re-enable interrupts when
dealing with types larger than the native word size.
Currently, these “__atomic_X” functions (as well as the older “__sync_”
counterparts) are not supported on the AVR.
When the atomic operations aren’t natively implemented by GCC on a specific
platform (either due to hardware limitations or because GCC just hasn’t
implemented them yet), GCC automatically converts calls to the built-in
“__atomic_X” functions into regular function calls which are to be resolved
by an external library. This library is called “libatomic” and is supposed
to implement the required routines in a way which is specific to the target
platform (perhaps using locks or disabling interrupts). The issue is that
no such library exists for the AVR, so any use of the “__atomic_X”
functions ultimately results in undefined references at link time.
The reason I’m posting here is because I’ve created a libatomic library for
the AVR. In reality, it’s just a single C file which implements the
required “__atomic_X” functions by means of saving/disabling interrupts,
performing the operation, then restoring interrupts.
With this library, I was also able to include a portable version of the
<stdatomic.h> header which allows for the successful use of C11 atomic
types!
I would like to get this merged into avr-libc, as it seems to be the most
sensible place for it (I think). However, I’m not familiar with the
process, coding requirements, etc…
I am willing to release it under whatever license is required, which I
assume would be BSD. This would be good also because the portable
<stdatomic.h> header file that I used is already BSD licensed.
By implementing the libatomic library and (optionally) including a
<stdatomic.h> header, we would support a fully portable way of dealing with
atomic types, which I think would be a good thing.
Do you think this is the right place for such an implementation? If not,
could you direct me?
I'm not familiar with the general development process here, so forgive me
if I'm out of line.
Jake