[riot-notifications] [RIOT-OS/RIOT] sys: Added simple memory barrier API (#11438)

Marian Buschsieweke notifications at github.com
Wed Apr 24 16:51:55 CEST 2019

maribu commented on this pull request.

> + * uint16_t measure_time(void) {
+ *     uint16_t pre = TIMER;
+ *     do_computing();
+ *     uint16_t post = TIMER;
+ *     return pre - post;
+ * }
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * In the above example the compiler will now dutifully generate code to access
+ * the timer register two times. The compiler will also not reorder any
+ * `volatile` memory access in regard to any other `volatile` memory access.
+ * But some compilers will reorder non-`volatile` accesses across `volatile`
+ * memory accesses. If the implementation of `do_computing()` is visible to the
+ * compiler, the compiler may conclude that do_computing() could be moved
+ * above the first or below the second access to `TIMER`, which will render the
+ * benchmark meaningless. (**Beware:** Even if `do_something()` is an external

The compiler can do any transformation that will not yield different observable behavior compared to the abstract machine. If I remember correctly observable behavior is defined as (likely incomplete):

- Side-effects cause by function calls
- Access to memory marked as `volatile`
- The return value of `main()` (at least on non-embedded systems)

If the C compiler can proof that a function does not yield observable behavior, the compiler thus can get as creative as it wants.

You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190424/0723b84f/attachment-0001.html>

More information about the notifications mailing list