From cfa7228b8fbec20fcb9d174e2e9e736468dd1c45 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Wed, 10 Feb 2021 09:17:59 -0500 Subject: [PATCH] pthread: test: facilitate dynamically allocated thread stacks Tests for dynamically allocated POSIX thread stacks. Signed-off-by: Christopher Friedt --- tests/posix/common/prj.conf | 2 +- tests/posix/common/src/pthread.c | 47 ++++++++++++++++++++++++++++---- tests/posix/common/testcase.yaml | 9 ++++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/tests/posix/common/prj.conf b/tests/posix/common/prj.conf index f72676270156d59..1eb3e47658d45ef 100644 --- a/tests/posix/common/prj.conf +++ b/tests/posix/common/prj.conf @@ -1,6 +1,6 @@ CONFIG_PTHREAD_IPC=y CONFIG_POSIX_API=y -CONFIG_MAX_PTHREAD_COUNT=20 +CONFIG_MAX_PTHREAD_COUNT=10 CONFIG_ZTEST=y CONFIG_ZTEST_NEW_API=y CONFIG_SEM_VALUE_MAX=32767 diff --git a/tests/posix/common/src/pthread.c b/tests/posix/common/src/pthread.c index 717b96d8ddccc05..72686dcb481a6df 100644 --- a/tests/posix/common/src/pthread.c +++ b/tests/posix/common/src/pthread.c @@ -216,6 +216,9 @@ void *thread_top_term(void *p1) } if (id >= 2) { + if (IS_ENABLED(CONFIG_DYNAMIC_THREAD)) { + zassert_false(pthread_detach(self), "failed to set detach state"); + } ret = pthread_detach(self); if (id == 2) { zassert_equal(ret, EINVAL, "re-detached thread!"); @@ -345,8 +348,13 @@ ZTEST(posix_apis, test_pthread_execution) getschedparam.sched_priority, "scheduling priorities do not match!"); - ret = pthread_create(&newthread[i], &attr[i], thread_top_exec, - INT_TO_POINTER(i)); + if (IS_ENABLED(CONFIG_DYNAMIC_THREAD)) { + ret = pthread_create(&newthread[i], NULL, thread_top_exec, + INT_TO_POINTER(i)); + } else { + ret = pthread_create(&newthread[i], &attr[i], thread_top_exec, + INT_TO_POINTER(i)); + } /* TESTPOINT: Check if thread is created successfully */ zassert_false(ret, "Number of threads exceed max limit"); @@ -500,8 +508,13 @@ ZTEST(posix_apis, test_pthread_termination) schedparam.sched_priority = 2; pthread_attr_setschedparam(&attr[i], &schedparam); pthread_attr_setstack(&attr[i], &stack_t[i][0], STACKS); - ret = pthread_create(&newthread[i], &attr[i], thread_top_term, - INT_TO_POINTER(i)); + if (IS_ENABLED(CONFIG_DYNAMIC_THREAD)) { + ret = pthread_create(&newthread[i], NULL, thread_top_term, + INT_TO_POINTER(i)); + } else { + ret = pthread_create(&newthread[i], &attr[i], thread_top_term, + INT_TO_POINTER(i)); + } zassert_false(ret, "Not enough space to create new thread"); } @@ -571,8 +584,10 @@ ZTEST(posix_apis, test_pthread_create_negative) pthread_attr_t attr1; /* create pthread without attr initialized */ - ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1); - zassert_equal(ret, EINVAL, "create thread with NULL successful"); + if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD)) { + ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1); + zassert_equal(ret, EAGAIN, "create thread with NULL successful"); + } /* initialized attr without set stack to create thread */ ret = pthread_attr_init(&attr1); @@ -771,3 +786,23 @@ ZTEST(posix_apis, test_posix_pthread_barrier) ret = pthread_barrierattr_destroy(&attr); zassert_equal(ret, 0, "pthread_barrierattr_destroy failed"); } + +static void *fun(void *arg) +{ + *((uint32_t *)arg) = 0xB105F00D; + return NULL; +} + +ZTEST(posix_apis, test_pthread_dynamic_stacks) +{ + pthread_t th; + uint32_t x = 0; + + if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD)) { + ztest_test_skip(); + } + + zassert_ok(pthread_create(&th, NULL, fun, &x)); + zassert_ok(pthread_join(th, NULL)); + zassert_equal(0xB105F00D, x); +} diff --git a/tests/posix/common/testcase.yaml b/tests/posix/common/testcase.yaml index a3086233b6c3c3c..19f858f0b50f033 100644 --- a/tests/posix/common/testcase.yaml +++ b/tests/posix/common/testcase.yaml @@ -86,3 +86,12 @@ tests: portability.posix.common.signal.big_nsig: extra_configs: - CONFIG_POSIX_RTSIG_MAX=1024 + portability.posix.common.dynamic_stack: + integration_platforms: + - qemu_x86 + - qemu_riscv64 + extra_configs: + - CONFIG_DYNAMIC_THREAD=y + - CONFIG_THREAD_STACK_INFO=y + - CONFIG_DYNAMIC_THREAD_POOL_SIZE=5 + - CONFIG_HEAP_MEM_POOL_SIZE=16384