-
Notifications
You must be signed in to change notification settings - Fork 349
[DNM] test: userspace: add a sys_sem test #10481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Add a sys_sem test between kernel- and user-space. Signed-off-by: Guennadi Liakhovetski <[email protected]>
|
SOFCI TEST |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
| #include <zephyr/sys/sem.h> | ||
| #include <zephyr/app_memory/mem_domain.h> |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The include directives should be placed at the top of the file with the other includes, not in the middle of the file after a ZTEST function. This violates standard C coding conventions and can make the code harder to maintain.
| k_thread_create(&user_thread, user_stack, USER_STACKSIZE, | ||
| sys_sem_function, NULL, NULL, NULL, | ||
| -1, K_USER, K_FOREVER); | ||
| k_mem_domain_add_partition(&dp_mdom, &mpart); | ||
| k_mem_domain_add_thread(&dp_mdom, &user_thread); | ||
|
|
||
| k_thread_start(&user_thread); |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a potential race condition here. The user thread is started with K_FOREVER delay, then immediately started on line 104. The thread could execute sys_sem_give on line 78 before the kernel thread reaches sys_sem_take on line 107, causing the semaphore signal to be lost. This is likely why the test hangs. Consider adding synchronization to ensure the kernel thread is waiting on sem1 before the user thread is started, similar to how test_user_thread_with_sem uses K_MSEC(10) delay.
| __ASSERT(k_is_user_context(), "isn't user"); | ||
| /* This is the goal, but it hangs with this disabled too */ | ||
| sys_sem_give(&simple_sem.sem1); | ||
| int ret = sys_sem_take(&simple_sem.sem2, K_MSEC(20)); |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable 'ret' is declared and assigned but never used or checked. This return value from sys_sem_take should be validated with an assertion to ensure the semaphore operation behaves as expected, especially since this is a test.
| int ret = sys_sem_take(&simple_sem.sem2, K_MSEC(20)); | |
| int ret = sys_sem_take(&simple_sem.sem2, K_MSEC(20)); | |
| zassert_equal(ret, 0, "sys_sem_take failed (%d)", ret); |
| sys_sem_init(&simple_sem.sem1, 0, 1); | ||
| sys_sem_init(&simple_sem.sem2, 0, 1); | ||
|
|
||
| k_thread_create(&user_thread, user_stack, USER_STACKSIZE, |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The global variable 'user_thread' is being reused from the earlier tests in this file. This can cause issues if multiple tests run in sequence, as the thread structure may contain state from previous test runs. Consider using a separate thread structure for this test, such as 'sys_sem_thread', to avoid potential conflicts.
|
|
||
| k_thread_start(&user_thread); | ||
|
|
||
| /* This is what doesn't work: enabling this line crashes the DSP */ |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment style is inconsistent. This file uses both single-line comments starting with '/*' and multi-line comment style. The comment should follow the established pattern in the file, which appears to use multi-line comment style for longer explanations.
| /* This is what doesn't work: enabling this line crashes the DSP */ | |
| /* | |
| * This is what doesn't work: enabling this line crashes the DSP | |
| */ |
| }; | ||
|
|
||
| ztest_test_pass(); | ||
| k_mem_domain_init(&dp_mdom, 0, NULL); |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The memory domain is initialized but never cleaned up after the test completes. The memory domain should be destroyed with k_mem_domain_remove_thread or appropriate cleanup to avoid resource leaks, especially in a test suite where multiple tests may run.
Add a sys_sem test between kernel- and user-space. @dcpleung any idea why this might not be working? Just adding a
sys_sem_take()in the kernel thread with a timeout hangs the DSP, instead of returning with a timeout as expected.