Skip to content

Commit 5a28297

Browse files
committed
pthread: test: facilitate dynamically allocated thread stacks
Tests for dynamically allocated POSIX thread stacks. Signed-off-by: Christopher Friedt <[email protected]>
1 parent 115efa2 commit 5a28297

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

tests/posix/common/prj.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CONFIG_PTHREAD_IPC=y
22
CONFIG_POSIX_API=y
3-
CONFIG_MAX_PTHREAD_COUNT=20
3+
CONFIG_MAX_PTHREAD_COUNT=10
44
CONFIG_ZTEST=y
55
CONFIG_ZTEST_NEW_API=y
66
CONFIG_SEM_VALUE_MAX=32767

tests/posix/common/src/pthread.c

+41-6
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ void *thread_top_term(void *p1)
216216
}
217217

218218
if (id >= 2) {
219+
if (IS_ENABLED(CONFIG_DYNAMIC_THREAD)) {
220+
zassert_false(pthread_detach(self), "failed to set detach state");
221+
}
219222
ret = pthread_detach(self);
220223
if (id == 2) {
221224
zassert_equal(ret, EINVAL, "re-detached thread!");
@@ -345,8 +348,13 @@ ZTEST(posix_apis, test_pthread_execution)
345348
getschedparam.sched_priority,
346349
"scheduling priorities do not match!");
347350

348-
ret = pthread_create(&newthread[i], &attr[i], thread_top_exec,
349-
INT_TO_POINTER(i));
351+
if (IS_ENABLED(CONFIG_DYNAMIC_THREAD)) {
352+
ret = pthread_create(&newthread[i], NULL, thread_top_exec,
353+
INT_TO_POINTER(i));
354+
} else {
355+
ret = pthread_create(&newthread[i], &attr[i], thread_top_exec,
356+
INT_TO_POINTER(i));
357+
}
350358

351359
/* TESTPOINT: Check if thread is created successfully */
352360
zassert_false(ret, "Number of threads exceed max limit");
@@ -500,8 +508,13 @@ ZTEST(posix_apis, test_pthread_termination)
500508
schedparam.sched_priority = 2;
501509
pthread_attr_setschedparam(&attr[i], &schedparam);
502510
pthread_attr_setstack(&attr[i], &stack_t[i][0], STACKS);
503-
ret = pthread_create(&newthread[i], &attr[i], thread_top_term,
504-
INT_TO_POINTER(i));
511+
if (IS_ENABLED(CONFIG_DYNAMIC_THREAD)) {
512+
ret = pthread_create(&newthread[i], NULL, thread_top_term,
513+
INT_TO_POINTER(i));
514+
} else {
515+
ret = pthread_create(&newthread[i], &attr[i], thread_top_term,
516+
INT_TO_POINTER(i));
517+
}
505518

506519
zassert_false(ret, "Not enough space to create new thread");
507520
}
@@ -571,8 +584,10 @@ ZTEST(posix_apis, test_pthread_create_negative)
571584
pthread_attr_t attr1;
572585

573586
/* create pthread without attr initialized */
574-
ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1);
575-
zassert_equal(ret, EINVAL, "create thread with NULL successful");
587+
if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD)) {
588+
ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1);
589+
zassert_equal(ret, EAGAIN, "create thread with NULL successful");
590+
}
576591

577592
/* initialized attr without set stack to create thread */
578593
ret = pthread_attr_init(&attr1);
@@ -777,3 +792,23 @@ ZTEST(posix_apis, test_pthread_equal)
777792
zassert_true(pthread_equal(pthread_self(), pthread_self()));
778793
zassert_false(pthread_equal(pthread_self(), (pthread_t)4242));
779794
}
795+
796+
static void *fun(void *arg)
797+
{
798+
*((uint32_t *)arg) = 0xB105F00D;
799+
return NULL;
800+
}
801+
802+
ZTEST(posix_apis, test_pthread_dynamic_stacks)
803+
{
804+
pthread_t th;
805+
uint32_t x = 0;
806+
807+
if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD)) {
808+
ztest_test_skip();
809+
}
810+
811+
zassert_ok(pthread_create(&th, NULL, fun, &x));
812+
zassert_ok(pthread_join(th, NULL));
813+
zassert_equal(0xB105F00D, x);
814+
}

tests/posix/common/testcase.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,12 @@ tests:
8686
portability.posix.common.signal.big_nsig:
8787
extra_configs:
8888
- CONFIG_POSIX_RTSIG_MAX=1024
89+
portability.posix.common.dynamic_stack:
90+
integration_platforms:
91+
- qemu_x86
92+
- qemu_riscv64
93+
extra_configs:
94+
- CONFIG_DYNAMIC_THREAD=y
95+
- CONFIG_THREAD_STACK_INFO=y
96+
- CONFIG_DYNAMIC_THREAD_POOL_SIZE=5
97+
- CONFIG_HEAP_MEM_POOL_SIZE=16384

0 commit comments

Comments
 (0)