1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-27 15:31:17 +01:00

tests/bitarithm_timings: Fix use of volatile

The tests used the volatile qualifier for two this:

1.  Prevent the compiler to optimize out calls to the inline-able functions
    bitarithm_msb, bitarithm_lsb, bitarithm_bits_set
2.  Communication between IRQ context and thread context

While the first use is valid, the second is dangerous, see [1], [2], [3], [4].
This commit replaces the second use with C11 atomics, which were explicitly
added to the C standard to address this use case.

[1]: https://www.kernel.org/doc/html/latest/process/volatile-considered-harmful.html
[2]: http://c.isvolatileusefulwiththreads.com/
[3]: https://web.archive.org/web/20181124154026/http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/
[4]: https://blog.regehr.org/archives/28
This commit is contained in:
Marian Buschsieweke 2019-11-15 09:22:18 +01:00
parent a1be610c9d
commit 8854255d7a
No known key found for this signature in database
GPG Key ID: 61F64C6599B1539F

View File

@ -29,6 +29,8 @@
* @}
*/
#include <stdint.h>
#include <stdatomic.h>
#include <stdio.h>
#include "bitarithm.h"
@ -38,22 +40,21 @@
#define TIMEOUT (TIMEOUT_S * US_PER_SEC)
#define PER_ITERATION (4)
static void callback(void *done_)
static atomic_bool done;
static void callback(void *unused)
{
volatile int *done = done_;
*done = 1;
(void)unused;
atomic_store(&done, true);
}
static void run_test(const char *name, unsigned (*test)(unsigned))
{
volatile int done = 0;
unsigned i = 0;
unsigned long count = 0;
atomic_store(&done, false);
xtimer_t xtimer;
xtimer.callback = callback;
xtimer.arg = (void *) &done;
xtimer_t xtimer = { .callback = callback };
xtimer_set(&xtimer, TIMEOUT);
do {
@ -78,7 +79,7 @@ static void run_test(const char *name, unsigned (*test)(unsigned))
}
++count;
} while (done == 0);
} while (atomic_load(&done) == false);
printf("+ %s: %lu iterations per second\r\n", name, (4*PER_ITERATION) * count / TIMEOUT_S);
}