Skip to content

Commit

Permalink
pthread: test: facilitate dynamically allocated thread stacks
Browse files Browse the repository at this point in the history
Tests for dynamically allocated POSIX thread stacks.

Fixes zephyrproject-rtos#25973

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
  • Loading branch information
cfriedt committed Jul 17, 2023
1 parent a39de7c commit 189d39c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tests/posix/common/prj.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CONFIG_PTHREAD_IPC=y
CONFIG_POSIX_API=y
CONFIG_MAX_PTHREAD_COUNT=20
CONFIG_MAX_PTHREAD_COUNT=2
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y
CONFIG_SEM_VALUE_MAX=32767
Expand Down
47 changes: 41 additions & 6 deletions tests/posix/common/src/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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, EINVAL, "create thread with NULL successful");
}

/* initialized attr without set stack to create thread */
ret = pthread_attr_init(&attr1);
Expand Down Expand Up @@ -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);
}
8 changes: 8 additions & 0 deletions tests/posix/common/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,11 @@ tests:
- CONFIG_SPIN_VALIDATE=n
integration_platforms:
- mps2_an385
portability.posix.common.dynamic_stack:
filter: not CONFIG_MMU and not CONFIG_MPU
platform_allow:
- qemu_cortex_m3
extra_configs:
- CONFIG_DYNAMIC_THREAD=y
- CONFIG_THREAD_STACK_INFO=y
- CONFIG_DYNAMIC_THREAD_POOL_SIZE=5

0 comments on commit 189d39c

Please sign in to comment.