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 <juha.niskanen@haltian.com>
This commit is contained in:
Juha Niskanen 2024-03-11 16:43:21 +02:00 committed by Xiang Xiao
parent fec49af501
commit 767642b4ea

View File

@ -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 [<num_threads>]\n");
printf("\nWhere:\n");
printf(" <num_threads> 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);