Skip to content

Commit

Permalink
tests: posix: dynamic stack support for pthread_create
Browse files Browse the repository at this point in the history
Tests for calling `pthread_create()` `attr == NULL`
or with `attr.stack == NULL` or with `attr.stacksize == 0`.

Signed-off-by: Christopher Friedt <[email protected]>
  • Loading branch information
cfriedt committed Apr 11, 2022
1 parent 472e41f commit d288c14
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
52 changes: 42 additions & 10 deletions tests/posix/common/src/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,13 @@ void test_posix_pthread_execution(void)
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_PTHREAD_DYNAMIC_STACK)) {
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 @@ -495,8 +500,13 @@ void test_posix_pthread_termination(void)
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_PTHREAD_DYNAMIC_STACK)) {
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 @@ -540,23 +550,45 @@ static void *create_thread1(void *p1)
void test_posix_pthread_create_negative(void)
{
int ret;
void *unused;
pthread_t pthread1;
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_PTHREAD_STACKS)) {
ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1);
zassert_equal(ret, 0, "create thread with NULL attr not successful");
ret = pthread_join(&pthread1, &unused);
zassert_equal(ret, 0, "pthread_join() failed");
} else {
ret = pthread_create(&pthread1, NULL, create_thread1, (void *)1);
zassert_equal(ret, EINVAL, "create thread with NULL attr successful");
}

/* initialized attr without set stack to create thread */
ret = pthread_attr_init(&attr1);
zassert_false(ret, "attr1 initialized failed");

attr1.stack = NULL;
ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1);
zassert_equal(ret, EINVAL, "create successful with NULL attr");
if (IS_ENABLED(CONFIG_DYNAMIC_PTHREAD_STACKS)) {
ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1);
zassert_equal(ret, 0, "create not successful with NULL stack");
ret = pthread_join(&pthread1, &unused);
zassert_equal(ret, 0, "pthread_join() failed");
} else {
ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1);
zassert_equal(ret, EINVAL, "create successful with NULL stack");
}

/* set stack size 0 to create thread */
pthread_attr_setstack(&attr1, &stack_1, 0);
ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1);
zassert_equal(ret, EINVAL, "create thread with 0 size");
if (IS_ENABLED(CONFIG_DYNAMIC_PTHREAD_STACKS)) {
ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1);
zassert_equal(ret, 0, "create thread with 0 size failed");
ret = pthread_join(&pthread1, &unused);
zassert_equal(ret, 0, "pthread_join() failed");
} else {
ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1);
zassert_equal(ret, EINVAL, "create thread with 0 size");
}
}
16 changes: 16 additions & 0 deletions tests/posix/common/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ tests:
extra_configs:
- CONFIG_NEWLIB_LIBC=y
- CONFIG_TEST_HW_STACK_PROTECTION=n
portability.posix.common.dynamic_stack:
platform_exclude: nsim_sem_mpu_stack_guard
extra_configs:
- CONFIG_NEWLIB_LIBC=n
- CONFIG_THREAD_STACK_INFO=y
- CONFIG_DYNAMIC_THREAD_STACK=y
- CONFIG_DYNAMIC_PTHREAD_STACK=y
- CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=16384
portability.posix.common.dynamic_stack.newlib:
platform_exclude: nsim_sem_mpu_stack_guard
filter: TOOLCHAIN_HAS_NEWLIB == 1
extra_configs:
- CONFIG_NEWLIB_LIBC=y
- CONFIG_THREAD_STACK_INFO=y
- CONFIG_DYNAMIC_THREAD_STACKS=y
- CONFIG_DYNAMIC_PTHREAD_STACKS=y

0 comments on commit d288c14

Please sign in to comment.