Skip to content

Commit 7289c27

Browse files
linguini1jerpelea
authored andcommitted
testing/{ostest,sched/smp}: Remove BOARD_LOOPSPERMSEC references
In an effort to avoid unexpected behaviour from users not calibrating BOARD_LOOPSPERMSEC, a compilation error occurs when it is undefined. On some systems, this is allowed (those that use timer implementations instead of busy-looping for delays). In these cases, we should busy-loop using the clock time instead of relying on a possibly undefined/uncalibrated macro. Signed-off-by: Matteo Golin <[email protected]>
1 parent d48b450 commit 7289c27

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

testing/ostest/sporadic.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,19 @@ static time_t g_start_time;
6565

6666
static void my_mdelay(unsigned int milliseconds)
6767
{
68-
volatile unsigned int i;
69-
volatile unsigned int j;
68+
struct timespec start;
69+
struct timespec cur;
70+
struct timespec diff;
7071

71-
for (i = 0; i < milliseconds; i++)
72+
clock_gettime(CLOCK_MONOTONIC, &start);
73+
74+
for (; ; )
7275
{
73-
for (j = 0; j < CONFIG_BOARD_LOOPSPERMSEC; j++)
76+
clock_gettime(CLOCK_MONOTONIC, &cur);
77+
clock_timespec_subtract(&cur, &start, &diff);
78+
if (diff.tv_sec * 1000 + diff.tv_nsec / 1000000 > milliseconds)
7479
{
80+
break;
7581
}
7682
}
7783
}

testing/ostest/sporadic2.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,19 @@ static int32_t g_ms_cnt2[2] =
8080

8181
static void my_mdelay(unsigned int milliseconds)
8282
{
83-
volatile unsigned int i;
84-
volatile unsigned int j;
83+
struct timespec start;
84+
struct timespec cur;
85+
struct timespec diff;
8586

86-
for (i = 0; i < milliseconds; i++)
87+
clock_gettime(CLOCK_MONOTONIC, &start);
88+
89+
for (; ; )
8790
{
88-
for (j = 0; j < CONFIG_BOARD_LOOPSPERMSEC; j++)
91+
clock_gettime(CLOCK_MONOTONIC, &cur);
92+
clock_timespec_subtract(&cur, &start, &diff);
93+
if (diff.tv_sec * 1000 + diff.tv_nsec / 1000000 > milliseconds)
8994
{
95+
break;
9096
}
9197
}
9298
}
@@ -172,7 +178,7 @@ static void sporadic_test_case(int32_t budget_1_ns, int32_t budget_2_ns)
172178

173179
sem_init(&g_sporadic_sem, 0, 0);
174180

175-
/* initilize global worker-thread millisecons-counters */
181+
/* Initialize global worker-thread milliseconds-counters */
176182

177183
g_ms_cnt1[PRIO_HI_NDX] = 0;
178184
g_ms_cnt1[PRIO_LO_NDX] = 0;

testing/sched/smp/smp_main.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <sched.h>
2828
#include <stdio.h>
2929
#include <stdlib.h>
30+
#include <time.h>
3031
#include <unistd.h>
3132
#include <pthread.h>
3233
#include <string.h>
@@ -87,13 +88,19 @@ static void show_cpu_conditional(FAR const char *caller, int threadno)
8788

8889
static void hog_milliseconds(unsigned int milliseconds)
8990
{
90-
volatile unsigned int i;
91-
volatile unsigned int j;
91+
struct timespec start;
92+
struct timespec cur;
93+
struct timespec diff;
9294

93-
for (i = 0; i < milliseconds; i++)
95+
clock_gettime(CLOCK_MONOTONIC, &start);
96+
97+
for (; ; )
9498
{
95-
for (j = 0; j < CONFIG_BOARD_LOOPSPERMSEC; j++)
99+
clock_gettime(CLOCK_MONOTONIC, &cur);
100+
clock_timespec_subtract(&cur, &start, &diff);
101+
if (diff.tv_sec * 1000 + diff.tv_nsec / 1000000 > milliseconds)
96102
{
103+
break;
97104
}
98105
}
99106
}

0 commit comments

Comments
 (0)