[riot-notifications] [RIOT-OS/RIOT] cpu/atmega_common: RTC: fix off-by-one second normalization & simplify rtc_get_time() (#16442)

Marian Buschsieweke notifications at github.com
Tue May 4 22:27:26 CEST 2021


@maribu commented on this pull request.



> +    do {
+        before = TCNT2;
         *time = tm_now;
-    }
+    } while (before > TCNT2);

```suggestion
    do {
        before = TCNT2;
        /* prevent compiler from reordering memory access to tm_now;
         * including moving it out of the loop */
        __asm__ volatile ("" : : "memory");
        *time = tm_now;
    } while (before > TCNT2);
```

Otherwise the code below would be considered to be a valid optimization of the above code:

```C
    do {
        before = TCNT2;
    } while (before > TCNT2);
    *time = tm_now;
```

Or even the following would be otherwise valid:

```C
    *time = tm_now;
    do {
        before = TCNT2;
    } while (before > TCNT2);
```

Reasoning: Only the reads from the (`volatile` qualified) `TCNT2` are considered to be observable behavior and the value of `tm_now` would be considered to be unchanged (as the C compiler doesn't care about concurrency / IRQs). The memory barrier will force the compiler to assume that all memory (and hence also `tm_now`) has potentially been changed in between and read it again after loading `before`.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/16442#pullrequestreview-651636864
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20210504/fd8f65a6/attachment.htm>


More information about the notifications mailing list