Skip to content

Commit 77d9292

Browse files
committed
pthread: test: facilitate dynamically allocated thread stacks
Tests for dynamically allocated POSIX thread stacks. Fixes #25973 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 25baef0 commit 77d9292

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

tests/posix/common/src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern void test_posix_pthread_execution(void);
1919
extern void test_posix_pthread_termination(void);
2020
extern void test_posix_multiple_threads_single_key(void);
2121
extern void test_posix_single_thread_multiple_keys(void);
22+
extern void test_posix_thread_attr_stacksize(void);
2223
extern void test_nanosleep_NULL_NULL(void);
2324
extern void test_nanosleep_NULL_notNULL(void);
2425
extern void test_nanosleep_notNULL_NULL(void);
@@ -42,6 +43,7 @@ void test_main(void)
4243
ztest_unit_test(test_posix_pthread_termination),
4344
ztest_unit_test(test_posix_multiple_threads_single_key),
4445
ztest_unit_test(test_posix_single_thread_multiple_keys),
46+
ztest_unit_test(test_posix_thread_attr_stacksize),
4547
ztest_unit_test(test_posix_clock),
4648
ztest_unit_test(test_posix_semaphore),
4749
ztest_unit_test(test_posix_normal_mutex),

tests/posix/common/src/pthread.c

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ void *thread_top_term(void *p1)
214214
}
215215

216216
if (id >= 2) {
217+
if (IS_ENABLED(CONFIG_PTHREAD_DYNAMIC_STACK)) {
218+
zassert_false(pthread_detach(self), "failed to set detach state");
219+
}
217220
ret = pthread_detach(self);
218221
if (id == 2) {
219222
zassert_equal(ret, EINVAL, "re-detached thread!");
@@ -301,18 +304,20 @@ void test_posix_pthread_execution(void)
301304
ret = pthread_setname_np(NULL, thr_name);
302305
zassert_equal(ret, ESRCH, "uninitialized setname!");
303306

304-
/* TESTPOINT: Try creating thread before attr init */
305-
ret = pthread_create(&newthread[0], &attr[0],
306-
thread_top_exec, NULL);
307-
zassert_equal(ret, EINVAL, "thread created before attr init!");
307+
if (!IS_ENABLED(CONFIG_PTHREAD_DYNAMIC_STACK)) {
308+
/* TESTPOINT: Try creating thread before attr init */
309+
ret = pthread_create(&newthread[0], &attr[0],
310+
thread_top_exec, NULL);
311+
zassert_equal(ret, EINVAL, "thread created before attr init!");
312+
}
308313

309314
for (i = 0; i < N_THR_E; i++) {
310315
ret = pthread_attr_init(&attr[i]);
311316
if (ret != 0) {
312317
zassert_false(pthread_attr_destroy(&attr[i]),
313-
"Unable to destroy pthread object attrib");
318+
"Unable to destroy pthread object attrib");
314319
zassert_false(pthread_attr_init(&attr[i]),
315-
"Unable to create pthread object attrib");
320+
"Unable to create pthread object attrib");
316321
}
317322

318323
/* TESTPOINTS: Retrieve set stack attributes and compare */
@@ -333,11 +338,16 @@ void test_posix_pthread_execution(void)
333338
pthread_attr_setschedparam(&attr[i], &schedparam);
334339
pthread_attr_getschedparam(&attr[i], &getschedparam);
335340
zassert_equal(schedparam.sched_priority,
336-
getschedparam.sched_priority,
337-
"scheduling priorities do not match!");
341+
getschedparam.sched_priority,
342+
"scheduling priorities do not match!");
338343

339-
ret = pthread_create(&newthread[i], &attr[i], thread_top_exec,
340-
INT_TO_POINTER(i));
344+
if (IS_ENABLED(CONFIG_PTHREAD_DYNAMIC_STACK)) {
345+
ret = pthread_create(&newthread[i], NULL, thread_top_exec,
346+
INT_TO_POINTER(i));
347+
} else {
348+
ret = pthread_create(&newthread[i], &attr[i], thread_top_exec,
349+
INT_TO_POINTER(i));
350+
}
341351

342352
/* TESTPOINT: Check if thread is created successfully */
343353
zassert_false(ret, "Number of threads exceed max limit");
@@ -429,8 +439,13 @@ void test_posix_pthread_termination(void)
429439
schedparam.sched_priority = 2;
430440
pthread_attr_setschedparam(&attr[i], &schedparam);
431441
pthread_attr_setstack(&attr[i], &stack_t[i][0], STACKS);
432-
ret = pthread_create(&newthread[i], &attr[i], thread_top_term,
433-
INT_TO_POINTER(i));
442+
if (IS_ENABLED(CONFIG_PTHREAD_DYNAMIC_STACK)) {
443+
ret = pthread_create(&newthread[i], NULL, thread_top_term,
444+
INT_TO_POINTER(i));
445+
} else {
446+
ret = pthread_create(&newthread[i], &attr[i], thread_top_term,
447+
INT_TO_POINTER(i));
448+
}
434449

435450
zassert_false(ret, "Not enough space to create new thread");
436451
}
@@ -464,3 +479,29 @@ void test_posix_pthread_termination(void)
464479
ret = pthread_getschedparam(newthread[N_THR_T/2], &policy, &schedparam);
465480
zassert_equal(ret, ESRCH, "got attr from terminated thread!");
466481
}
482+
483+
#ifdef CONFIG_PTHREAD_DYNAMIC_STACK
484+
static void *fun(void *arg)
485+
{
486+
*((uint32_t *)arg) = 0xB105F00D;
487+
return NULL;
488+
}
489+
490+
void test_posix_thread_attr_stacksize(void)
491+
{
492+
uint32_t x = 0;
493+
pthread_attr_t attr;
494+
pthread_t th;
495+
496+
/* TESTPOINT: specify a custom stack size via pthread_attr_t */
497+
zassert_equal(0, pthread_attr_init(&attr), "");
498+
zassert_equal(0, pthread_attr_setstacksize(&attr, 256), "");
499+
zassert_equal(0, pthread_create(&th, &attr, fun, &x), "");
500+
zassert_equal(0, pthread_join(th, NULL), "");
501+
zassert_equal(0xB105F00D, x, "");
502+
}
503+
#else
504+
void test_posix_thread_attr_stacksize(void)
505+
{
506+
}
507+
#endif

tests/posix/common/testcase.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,18 @@ tests:
3636
extra_configs:
3737
- CONFIG_NEWLIB_LIBC=y
3838
- CONFIG_TEST_HW_STACK_PROTECTION=n
39+
portability.posix.common.dynamic_stack:
40+
platform_exclude: nsim_sem_mpu_stack_guard nsim_em_mpu_stack_guard
41+
extra_configs:
42+
- CONFIG_NEWLIB_LIBC=n
43+
- CONFIG_PTHREAD_DYNAMIC_STACKS=y
44+
- CONFIG_IDLE_STACK_SIZE=768
45+
- CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=16384
46+
portability.posix.common.dynamic_stack.newlib:
47+
platform_exclude: nsim_sem_mpu_stack_guard nsim_em_mpu_stack_guard
48+
filter: TOOLCHAIN_HAS_NEWLIB == 1
49+
extra_configs:
50+
- CONFIG_NEWLIB_LIBC=y
51+
- CONFIG_PTHREAD_DYNAMIC_STACKS=y
52+
- CONFIG_IDLE_STACK_SIZE=768
53+
- CONFIG_HEAP_MEM_POOL_SIZE=16384

0 commit comments

Comments
 (0)