Does C11 _Atomic keyword mean anything for ChibiOS?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
hyOzd
Posts: 20
Joined: Tue Jul 21, 2015 12:17 pm
Has thanked: 1 time
Been thanked: 2 times

Does C11 _Atomic keyword mean anything for ChibiOS?

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Does C11 _Atomic keyword mean anything for ChibiOS?

Post 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
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: Does C11 _Atomic keyword mean anything for ChibiOS?

Post 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
hyOzd
Posts: 20
Joined: Tue Jul 21, 2015 12:17 pm
Has thanked: 1 time
Been thanked: 2 times

Re: Does C11 _Atomic keyword mean anything for ChibiOS?

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Does C11 _Atomic keyword mean anything for ChibiOS?

Post 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
Post Reply