From 767642b4eaaa9e9f8731f7a60261af66957b0326 Mon Sep 17 00:00:00 2001 From: Juha Niskanen Date: Mon, 11 Mar 2024 16:43:21 +0200 Subject: [PATCH] testing/getprime: allow running with zero threads getprime can now run without creating any additional threads. This helps testing pthreads and measuring performance of directly searching primes from main thread versus creating one thread. Also don't misuse ASSERT() for checking command-line input coming from user. Signed-off-by: Juha Niskanen --- testing/getprime/getprime_main.c | 35 +++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/testing/getprime/getprime_main.c b/testing/getprime/getprime_main.c index 198f4a5ca..910aef797 100644 --- a/testing/getprime/getprime_main.c +++ b/testing/getprime/getprime_main.c @@ -102,8 +102,7 @@ static FAR void *thread_func(FAR void *param) printf("thread #%d finished, found %d primes, last one was %d\n", no, count, last); - pthread_exit(NULL); - return NULL; /* To keep some compilers happy */ + return NULL; } /**************************************************************************** @@ -141,6 +140,14 @@ static void get_prime_in_parallel(int n) printf("Set thread policy to SCHED_FIFO\n"); #endif + /* Zero threads, do work from main thread instead. */ + + if (n == 0) + { + arg[0] = n; + thread_func((FAR void *)&arg[0]); + } + for (i = 0; i < n; i++) { arg[i] = i; @@ -161,6 +168,19 @@ static void get_prime_in_parallel(int n) UNUSED(status); } +/**************************************************************************** + * Name: usage + ****************************************************************************/ + +static void usage(void) +{ + printf("\nUsage: getprime []\n"); + printf("\nWhere:\n"); + printf(" is integer from 0 to %d (default 1).\n", + MAX_THREADS); + exit(0); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -180,9 +200,14 @@ int main(int argc, FAR char *argv[]) if (argc == 2) { n = (int)strtol(argv[1], &endp, 10); - ASSERT(argv[1] != endp); - - ASSERT(0 < n && n <= MAX_THREADS); + if (argv[1] == endp || n < 0 || n > MAX_THREADS) + { + usage(); + } + } + else if (argc > 2) + { + usage(); } clock_gettime(CLOCK_REALTIME, &ts0);