Skip to content

Commit 78d154c

Browse files
committed
pthread: test: facilitate dynamically allocated thread stacks
Tests for dynamically allocated POSIX thread stacks. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 15b9e8a commit 78d154c

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

tests/posix/common/prj.conf

Lines changed: 1 addition & 1 deletion
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=2
44
CONFIG_ZTEST=y
55
CONFIG_ZTEST_NEW_API=y
66
CONFIG_SEM_VALUE_MAX=32767

tests/posix/common/src/pthread.c

Lines changed: 41 additions & 6 deletions
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, EINVAL, "create thread with NULL successful");
590+
}
576591

577592
/* initialized attr without set stack to create thread */
578593
ret = pthread_attr_init(&attr1);
@@ -771,3 +786,23 @@ ZTEST(posix_apis, test_posix_pthread_barrier)
771786
ret = pthread_barrierattr_destroy(&attr);
772787
zassert_equal(ret, 0, "pthread_barrierattr_destroy failed");
773788
}
789+
790+
static void *fun(void *arg)
791+
{
792+
*((uint32_t *)arg) = 0xB105F00D;
793+
return NULL;
794+
}
795+
796+
ZTEST(posix_apis, test_pthread_dynamic_stacks)
797+
{
798+
pthread_t th;
799+
uint32_t x = 0;
800+
801+
if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD)) {
802+
ztest_test_skip();
803+
}
804+
805+
zassert_ok(pthread_create(&th, NULL, fun, &x));
806+
zassert_ok(pthread_join(th, NULL));
807+
zassert_equal(0xB105F00D, x);
808+
}

tests/posix/common/testcase.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,11 @@ tests:
8080
- CONFIG_SPIN_VALIDATE=n
8181
integration_platforms:
8282
- mps2_an385
83+
portability.posix.common.dynamic_stack:
84+
filter: not CONFIG_MMU and not CONFIG_MPU
85+
platform_allow:
86+
- qemu_cortex_m3
87+
extra_configs:
88+
- CONFIG_DYNAMIC_THREAD=y
89+
- CONFIG_THREAD_STACK_INFO=y
90+
- CONFIG_DYNAMIC_THREAD_POOL_SIZE=5

0 commit comments

Comments
 (0)