Skip to content

Commit 43f9171

Browse files
jmichalski-antkartben
authored andcommitted
posix: add pthread specific data deallocation test
This commit adds test to assert that thread specific data is deallocated on thread termination. Signed-off-by: Jakub Michalski <[email protected]>
1 parent 044b702 commit 43f9171

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

tests/posix/common/prj.conf

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ CONFIG_THREAD_NAME=y
99
CONFIG_DYNAMIC_THREAD=y
1010
CONFIG_THREAD_STACK_INFO=y
1111
CONFIG_DYNAMIC_THREAD_POOL_SIZE=6
12+
CONFIG_POSIX_THREAD_KEYS_MAX=512
13+
CONFIG_TEST_EXTRA_STACK_SIZE=4096
1214

1315
# for fnmatch()
1416
CONFIG_POSIX_C_LIB_EXT=y

tests/posix/common/src/key.c

+38
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,44 @@ ZTEST(key, test_correct_key_is_deleted)
144144
}
145145
}
146146

147+
static void *setspecific_thread(void *count)
148+
{
149+
int value = 42;
150+
int *alloc_count = count;
151+
152+
while (1) {
153+
pthread_key_t key;
154+
155+
zassert_ok(pthread_key_create(&key, NULL), "failed to create key");
156+
if (pthread_setspecific(key, &value) == ENOMEM) {
157+
break;
158+
};
159+
*alloc_count += 1;
160+
}
161+
162+
return NULL;
163+
}
164+
165+
ZTEST(key, test_thread_specific_data_deallocation)
166+
{
167+
pthread_t thread;
168+
static int alloc_count_t0;
169+
static int alloc_count_t1;
170+
171+
zassert_ok(pthread_create(&thread, NULL, setspecific_thread, &alloc_count_t0),
172+
"attempt to create thread failed");
173+
zassert_ok(pthread_join(thread, NULL), "failed to join thread");
174+
printk("first thread allocated %d keys", alloc_count_t0);
175+
176+
zassert_ok(pthread_create(&thread, NULL, setspecific_thread, &alloc_count_t1),
177+
"attempt to create thread failed");
178+
zassert_ok(pthread_join(thread, NULL), "failed to join thread");
179+
printk("second thread allocated %d keys", alloc_count_t1);
180+
181+
zassert_equal(alloc_count_t0, alloc_count_t1,
182+
"failed to deallocate thread specific data");
183+
}
184+
147185
static void before(void *arg)
148186
{
149187
ARG_UNUSED(arg);

tests/posix/common/testcase.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ tests:
6767
- CONFIG_DYNAMIC_THREAD=y
6868
- CONFIG_THREAD_STACK_INFO=y
6969
- CONFIG_HEAP_MEM_POOL_SIZE=16384
70+
- CONFIG_POSIX_THREAD_KEYS_MAX=2048
71+
- CONFIG_TEST_EXTRA_STACK_SIZE=16384
7072
portability.posix.common.static_stack:
7173
extra_configs:
7274
- CONFIG_DYNAMIC_THREAD=n

0 commit comments

Comments
 (0)