lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 07/09/2021 21:45, Viacheslav Usov wrote:
On Mon, Sep 6, 2021 at 7:18 PM Lorenzo Donati
<> wrote:

For example, assuming x and y are 16 bit quantities on an 8 bit MCU,
if you write:

x = <expr1>;
y = <expr2>;

there is no way in C99 to ensure that the updating of x happens
completely before the updating of y (the upper 8 bits and the lower 8
bits of each can be modified in any order-usually for optimization

Unless you are talking about setting x and y from two different
threads, and unless you also need to be wary of
interrupts/signals/etc, it is enough to have x and y marked as
volatile in C99.

Yep, you are right, volatile is sufficient. I re-checked annex C and indeed there's a sequence point after each full expression such as a statement expression (and assignments are statements expressions).

So as per 6.7.3, paragraph 6:

"An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine, except as modified by the unknown factors mentioned previously. What constitutes an access to an object that
has volatile-qualified type is implementation-defined."

In later versions, atomics might be a better choice.