Page 1 of 1

Does C11 _Atomic keyword mean anything for ChibiOS?

Posted: Thu Oct 08, 2015 2:57 pm
by hyOzd
I have a thread that does read an `int` variable. Other threads write to this variable. I don't want to use a mutex for just an `int` variable.

Can I make sure accesses to this variable are atomic, using the C11's _Atomic keyword? Like this;

Code: Select all

_Atomic(int) number;


Or should I assume that everything is alright without any extra specifiers, because `int` is 32 bits on my 32 bit platform. I'm working with a STM32F4 mcu.

Re: Does C11 _Atomic keyword mean anything for ChibiOS?

Posted: Thu Oct 08, 2015 3:30 pm
by Giovanni
Hi,

I imagine that the compiler just calls some library functions for mutual exclusion, those could (should) probably be mapped on mechanisms of the underlying OS, mutextes or critical zones for example.

I don't know the details, you should read the compiler documentation.

Giovanni

Re: Does C11 _Atomic keyword mean anything for ChibiOS?

Posted: Thu Oct 08, 2015 7:16 pm
by alex31
on stm32, 32 bits access is defacto atomic, so no need to protect access.
you just need to declare your variable volatile to avoid local register cache compiler strategy.

https://en.wikipedia.org/wiki/Volatile_%28computer_programming%29

Alexandre

Re: Does C11 _Atomic keyword mean anything for ChibiOS?

Posted: Fri Oct 09, 2015 3:23 pm
by hyOzd
alex31 thanks for clarification and the tip.

Giovanni,
I imagine that the compiler just calls some library functions for mutual exclusion

You are right about that. Instead of `int` I declared my variable as `long long` :

Code: Select all

_Atomic(long long) number;

And I got these linker errors:

Code: Select all

undefined reference to `__atomic_load_8'
undefined reference to `__atomic_store_8'


I think it would be nice if ChibiOS provided such functions to support C11. What do you think?

By the way, in case of `_Atomic(int)`, compiler puts a `DMB` instruction before and after operations on this variable. According to my research, this operator doesn't mean anything for Cortex-M processors. But for more advanced processors it disables some runtime optimizations.

Re: Does C11 _Atomic keyword mean anything for ChibiOS?

Posted: Fri Oct 09, 2015 3:33 pm
by Giovanni
There are a lot of other things in C11 that would require OS support, it is something to consider but it has not a very high priority right now.

Of course unless somebody contributes it :)

Giovanni