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.

Signed-off-by: Christopher Friedt <[email protected]>
  • Loading branch information
cfriedt committed Jul 20, 2023
1 parent 35a7f4e commit 60b5cbf
Show file tree
Hide file tree
Showing 3 changed files with 51 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=10
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, EAGAIN, "create thread with NULL successful");
}

/* initialized attr without set stack to create thread */
ret = pthread_attr_init(&attr1);
Expand Down Expand Up @@ -777,3 +792,23 @@ ZTEST(posix_apis, test_pthread_equal)
zassert_true(pthread_equal(pthread_self(), pthread_self()));
zassert_false(pthread_equal(pthread_self(), (pthread_t)4242));
}

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);
}
9 changes: 9 additions & 0 deletions tests/posix/common/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 60b5cbf

Please sign in to comment.