1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/tests/thread_float/main.c
Marian Buschsieweke f62b662b08
tests/thread_float: improve and add test script
- Perform the same computation over and over again. If the results
  differ, context switches have an impact on the calculation (e.g.
  when the FPU internally uses more bits than a float, but that bits
  are not saved / restored on context switch)
- Give the three threads the names "t1", "t2", and "t3" and print them
  on console, instead of the process ID. This makes interpretation of
  the output easier, as the process IDs depend e.g. on whether a given
  platforms requires an idle thread or not.
- Do not use the thread ID in the calculation, but the number at the
  end of the thread name. This will result in the number printed only
  depending on the precision of the (software) FPU and the printf()
  implementation, and not on which threads are created in which order
  (including the idle thread)
- Add a script to support running `make test`

Update tests/thread_float/tests/01-run.py

Co-authored-by: Alexandre Abadie <alexandre.abadie@inria.fr>
2021-11-09 19:57:59 +01:00

99 lines
2.5 KiB
C

/*
* Copyright (C) 2017 OTA keys S.A.
* 2021 Otto-von-Guericke-Universität Magdeburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief Thread test application
*
* @author Vincent Dupont <vincent@otakeys.com>
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include "thread.h"
#include "msg.h"
#include "xtimer.h"
#include "timex.h"
static char t1_stack[THREAD_STACKSIZE_MAIN];
static char t2_stack[THREAD_STACKSIZE_MAIN];
static char t3_stack[THREAD_STACKSIZE_MAIN];
static kernel_pid_t p1, p2, p3;
static xtimer_t timer;
#define OFFSET (10 * XTIMER_BACKOFF)
static mutex_t lock = MUTEX_INIT;
static void timer_cb(void *arg)
{
(void) arg;
thread_yield();
xtimer_set(&timer, OFFSET);
}
static void *thread_1_2_3(void *_arg)
{
const char *arg = _arg;
float f, init;
mutex_lock(&lock);
printf("THREAD %s start\n", arg);
mutex_unlock(&lock);
/* Use number at end of thread name, e.g. 3 for "t3", to seed the calculation */
init = 1.0 * (arg[strlen(arg) - 1] - '0');
f = init;
while (1) {
for (unsigned long i = 0; i < 10000ul; i++) {
f = f + 1.0 / f;
}
/* only t1 and t3 should print */
if (strcmp("t2", arg) != 0) {
mutex_lock(&lock);
printf("%s: %f\n", arg, (double)f);
mutex_unlock(&lock);
}
f = init;
}
return NULL;
}
int main(void)
{
const char *t1_name = "t1";
const char *t2_name = "t2";
const char *t3_name = "t3";
p1 = thread_create(t1_stack, sizeof(t1_stack), THREAD_PRIORITY_MAIN + 1,
THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
thread_1_2_3, (void *)t1_name, t1_name);
p2 = thread_create(t2_stack, sizeof(t2_stack), THREAD_PRIORITY_MAIN + 1,
THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
thread_1_2_3, (void *)t2_name, t2_name);
p3 = thread_create(t3_stack, sizeof(t3_stack), THREAD_PRIORITY_MAIN + 1,
THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
thread_1_2_3, (void *)t3_name, t3_name);
puts("THREADS CREATED\n");
timer.callback = timer_cb;
xtimer_set(&timer, OFFSET);
return 0;
}