[libc-commits] [libc] [libc] fix lost signal issue in cnd_test (PR #201721)
via libc-commits
libc-commits at lists.llvm.org
Thu Jun 4 21:11:48 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Schrodinger ZHU Yifan (SchrodingerZhu)
<details>
<summary>Changes</summary>
Fix another spot of potential hanging test.
The timeout time is too short in cnd_test which results in missed signals. This patch adds an atomic flag and make the main thread looping on it to ensure the last signal to unblock waiter is delivered.
---
Full diff: https://github.com/llvm/llvm-project/pull/201721.diff
1 Files Affected:
- (modified) libc/test/integration/src/threads/cnd_test.cpp (+12-5)
``````````diff
diff --git a/libc/test/integration/src/threads/cnd_test.cpp b/libc/test/integration/src/threads/cnd_test.cpp
index 297d95e3149a2..03c9ff3b45dd2 100644
--- a/libc/test/integration/src/threads/cnd_test.cpp
+++ b/libc/test/integration/src/threads/cnd_test.cpp
@@ -116,7 +116,9 @@ enum class WaitMode {
Timed,
};
-int waiter_thread_func([[maybe_unused]] void *unused) {
+int waiter_thread_func(void *arg) {
+ auto *waiter_unblocked =
+ static_cast<LIBC_NAMESPACE::cpp::Atomic<bool> *>(arg);
LIBC_NAMESPACE::mtx_lock(&waiter_mtx);
LIBC_NAMESPACE::mtx_lock(&main_thread_mtx);
@@ -124,6 +126,7 @@ int waiter_thread_func([[maybe_unused]] void *unused) {
LIBC_NAMESPACE::mtx_unlock(&main_thread_mtx);
LIBC_NAMESPACE::cnd_wait(&waiter_cnd, &waiter_mtx);
+ waiter_unblocked->store(true);
LIBC_NAMESPACE::mtx_unlock(&waiter_mtx);
return 0x600D;
@@ -140,7 +143,9 @@ void single_waiter_test(WaitMode wait_mode) {
ASSERT_EQ(LIBC_NAMESPACE::mtx_lock(&main_thread_mtx), int(thrd_success));
thrd_t waiter_thread;
- LIBC_NAMESPACE::thrd_create(&waiter_thread, waiter_thread_func, nullptr);
+ LIBC_NAMESPACE::cpp::Atomic<bool> waiter_unblocked(false);
+ LIBC_NAMESPACE::thrd_create(&waiter_thread, waiter_thread_func,
+ &waiter_unblocked);
if (wait_mode == WaitMode::Default) {
ASSERT_EQ(LIBC_NAMESPACE::cnd_wait(&main_thread_cnd, &main_thread_mtx),
@@ -156,9 +161,11 @@ void single_waiter_test(WaitMode wait_mode) {
}
ASSERT_EQ(LIBC_NAMESPACE::mtx_unlock(&main_thread_mtx), int(thrd_success));
- ASSERT_EQ(LIBC_NAMESPACE::mtx_lock(&waiter_mtx), int(thrd_success));
- ASSERT_EQ(LIBC_NAMESPACE::cnd_signal(&waiter_cnd), int(thrd_success));
- ASSERT_EQ(LIBC_NAMESPACE::mtx_unlock(&waiter_mtx), int(thrd_success));
+ while (!waiter_unblocked.load()) {
+ ASSERT_EQ(LIBC_NAMESPACE::mtx_lock(&waiter_mtx), int(thrd_success));
+ ASSERT_EQ(LIBC_NAMESPACE::cnd_signal(&waiter_cnd), int(thrd_success));
+ ASSERT_EQ(LIBC_NAMESPACE::mtx_unlock(&waiter_mtx), int(thrd_success));
+ }
int retval;
LIBC_NAMESPACE::thrd_join(waiter_thread, &retval);
``````````
</details>
https://github.com/llvm/llvm-project/pull/201721
More information about the libc-commits
mailing list