diff --git a/tests/posix/common/prj.conf b/tests/posix/common/prj.conf index 130e950f754..f50d956f8b2 100644 --- a/tests/posix/common/prj.conf +++ b/tests/posix/common/prj.conf @@ -9,6 +9,8 @@ CONFIG_THREAD_NAME=y CONFIG_DYNAMIC_THREAD=y CONFIG_THREAD_STACK_INFO=y CONFIG_DYNAMIC_THREAD_POOL_SIZE=6 +CONFIG_POSIX_THREAD_KEYS_MAX=512 +CONFIG_TEST_EXTRA_STACK_SIZE=4096 # for fnmatch() CONFIG_POSIX_C_LIB_EXT=y diff --git a/tests/posix/common/src/key.c b/tests/posix/common/src/key.c index e90f4862ff7..693f4dccaa9 100644 --- a/tests/posix/common/src/key.c +++ b/tests/posix/common/src/key.c @@ -144,6 +144,44 @@ ZTEST(key, test_correct_key_is_deleted) } } +static void *setspecific_thread(void *count) +{ + int value = 42; + int *alloc_count = count; + + while (1) { + pthread_key_t key; + + zassert_ok(pthread_key_create(&key, NULL), "failed to create key"); + if (pthread_setspecific(key, &value) == ENOMEM) { + break; + }; + *alloc_count += 1; + } + + return NULL; +} + +ZTEST(key, test_thread_specific_data_deallocation) +{ + pthread_t thread; + static int alloc_count_t0; + static int alloc_count_t1; + + zassert_ok(pthread_create(&thread, NULL, setspecific_thread, &alloc_count_t0), + "attempt to create thread failed"); + zassert_ok(pthread_join(thread, NULL), "failed to join thread"); + printk("first thread allocated %d keys", alloc_count_t0); + + zassert_ok(pthread_create(&thread, NULL, setspecific_thread, &alloc_count_t1), + "attempt to create thread failed"); + zassert_ok(pthread_join(thread, NULL), "failed to join thread"); + printk("second thread allocated %d keys", alloc_count_t1); + + zassert_equal(alloc_count_t0, alloc_count_t1, + "failed to deallocate thread specific data"); +} + static void before(void *arg) { ARG_UNUSED(arg); diff --git a/tests/posix/common/testcase.yaml b/tests/posix/common/testcase.yaml index b5945df205e..0b3ab9db419 100644 --- a/tests/posix/common/testcase.yaml +++ b/tests/posix/common/testcase.yaml @@ -67,6 +67,8 @@ tests: - CONFIG_DYNAMIC_THREAD=y - CONFIG_THREAD_STACK_INFO=y - CONFIG_HEAP_MEM_POOL_SIZE=16384 + - CONFIG_POSIX_THREAD_KEYS_MAX=2048 + - CONFIG_TEST_EXTRA_STACK_SIZE=16384 portability.posix.common.static_stack: extra_configs: - CONFIG_DYNAMIC_THREAD=n