[libcxx-commits] [libcxx] [libc++] test for atomic::wait (PR #169177)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Nov 22 09:37:44 PST 2025
https://github.com/huixie90 created https://github.com/llvm/llvm-project/pull/169177
None
>From bf78bf3d839bbcb4a6555e32bbc150ef4371cbf4 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sat, 22 Nov 2025 17:37:22 +0000
Subject: [PATCH] [libc++] test for atomic::wait
---
.../lost_wakeup.pass.cpp | 58 +++++++++++++++++++
1 file changed, 58 insertions(+)
create mode 100644 libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/lost_wakeup.pass.cpp
diff --git a/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/lost_wakeup.pass.cpp b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/lost_wakeup.pass.cpp
new file mode 100644
index 0000000000000..5a0d587815828
--- /dev/null
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/lost_wakeup.pass.cpp
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: no-threads
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// This is a stress test for std::atomic::wait for lost wake ups.
+
+// <atomic>
+
+#include <atomic>
+#include <functional>
+#include <thread>
+#include <vector>
+
+#include "make_test_thread.h"
+
+constexpr int num_waiters = 8;
+constexpr int num_iterations = 10'000;
+
+int main(int, char**) {
+ for (int run = 0; run < 20; ++run) {
+ std::atomic<int> waiter_ready(0);
+ std::atomic<int> state(0);
+
+ auto wait = [&]() {
+ for (int i = 0; i < num_iterations; ++i) {
+ auto old_state = state.load(std::memory_order_acquire);
+ waiter_ready.fetch_add(1, std::memory_order_acq_rel);
+ state.wait(old_state, std::memory_order_acquire);
+ }
+ };
+
+ auto notify = [&] {
+ for (int i = 0; i < num_iterations; ++i) {
+ while (waiter_ready.load(std::memory_order_acquire) < num_waiters) {
+ std::this_thread::yield();
+ }
+ waiter_ready.store(0, std::memory_order_release);
+ state.fetch_add(1, std::memory_order_acq_rel);
+ state.notify_all();
+ }
+ };
+
+ std::vector<std::jthread> threads;
+ for (int i = 0; i < num_waiters; ++i)
+ threads.push_back(support::make_test_jthread(wait));
+
+ threads.push_back(support::make_test_jthread(notify));
+ }
+
+ return 0;
+}
More information about the libcxx-commits
mailing list