[libcxx-commits] [libcxx] [libc++] debug semaaphore (PR #180353)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 11 06:24:04 PST 2026


https://github.com/huixie90 updated https://github.com/llvm/llvm-project/pull/180353

>From 79cc6e2cc0b253b0a34a3b5ad302341fa943170b Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sat, 7 Feb 2026 16:19:16 +0000
Subject: [PATCH 1/8] [libc++] debug semaaphore

---
 .../thread.semaphore/timed.debug.pass.cpp     | 84 +++++++++++++++++++
 1 file changed, 84 insertions(+)
 create mode 100644 libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp

diff --git a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
new file mode 100644
index 0000000000000..d7833194927e4
--- /dev/null
+++ b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <semaphore>
+
+#include <semaphore>
+#include <thread>
+#include <chrono>
+#include <cassert>
+#include <iostream>
+#include <iomanip>
+
+
+#include "make_test_thread.h"
+#include "test_macros.h"
+
+int main(int, char**)
+{
+  auto const start = std::chrono::steady_clock::now();
+  auto log = [start] ()-> auto& {
+    using namespace std::chrono;
+
+    auto elapsed = steady_clock::now() - start;
+
+    auto hours = duration_cast<std::chrono::hours>(elapsed);
+    elapsed -= hours;
+
+    auto minutes = duration_cast<std::chrono::minutes>(elapsed);
+    elapsed -= minutes;
+
+    auto seconds = duration_cast<std::chrono::seconds>(elapsed);
+    elapsed -= seconds;
+
+    auto milliseconds = duration_cast<std::chrono::milliseconds>(elapsed);
+
+    std::cerr << "["
+              << std::setw(2) << std::setfill('0') << hours.count() << ":"
+              << std::setw(2) << std::setfill('0') << minutes.count() << ":"
+              << std::setw(2) << std::setfill('0') << seconds.count() << "."
+              << std::setw(3) << std::setfill('0') << milliseconds.count()
+              << "] ";
+
+    return std::cerr;
+  };
+
+  std::counting_semaphore<> s(0);
+
+  log() << "try_acquire_until: start + " <<  std::chrono::milliseconds(250)  << "\n";
+  assert(!s.try_acquire_until(start + std::chrono::milliseconds(250)));
+  log() << "done: try_acquire_until: start + " <<  std::chrono::milliseconds(250)  << "\n";
+
+  log() << "try_acquire_for: " << std::chrono::milliseconds(250) << "\n";
+  assert(!s.try_acquire_for(std::chrono::milliseconds(250)));
+  log() << "done: try_acquire_for: " << std::chrono::milliseconds(250) << "\n";
+
+  std::thread t = support::make_test_thread([&](){
+    std::this_thread::sleep_for(std::chrono::milliseconds(250));
+    s.release();
+    std::this_thread::sleep_for(std::chrono::milliseconds(250));
+    s.release();
+  });
+
+  log() << "try_acquire_until: start + " <<  std::chrono::seconds(2)  << "\n";
+  assert(s.try_acquire_until(start + std::chrono::seconds(2)));
+  log() << "done: try_acquire_until: start + " <<  std::chrono::seconds(2)  << "\n";
+
+  log() << "try_acquire_for: " << std::chrono::seconds(2) << "\n";
+  assert(s.try_acquire_for(std::chrono::seconds(2)));
+  log() << "done: try_acquire_for: " << std::chrono::seconds(2) << "\n";
+  t.join();
+
+  auto const end = std::chrono::steady_clock::now();
+  assert(end - start < std::chrono::seconds(10));
+
+  return 1;
+}

>From 15caf8d017581b2ac398c6e89c3b9bd9cb81180d Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sat, 7 Feb 2026 16:23:48 +0000
Subject: [PATCH 2/8] code to debug

---
 .../thread.semaphore/timed.debug.pass.cpp     | 34 ++++++++++++-------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
index d7833194927e4..904da25a49940 100644
--- a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
+++ b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
@@ -22,13 +22,11 @@
 #include "make_test_thread.h"
 #include "test_macros.h"
 
-int main(int, char**)
-{
-  auto const start = std::chrono::steady_clock::now();
-  auto log = [start] ()-> auto& {
+void test(auto log_start){
+  auto log = [log_start] ()-> auto& {
     using namespace std::chrono;
 
-    auto elapsed = steady_clock::now() - start;
+    auto elapsed = steady_clock::now() - log_start;
 
     auto hours = duration_cast<std::chrono::hours>(elapsed);
     elapsed -= hours;
@@ -51,15 +49,16 @@ int main(int, char**)
     return std::cerr;
   };
 
+  auto const start = std::chrono::steady_clock::now();
   std::counting_semaphore<> s(0);
 
-  log() << "try_acquire_until: start + " <<  std::chrono::milliseconds(250)  << "\n";
+  log() << "start: try_acquire_until: start + " <<  std::chrono::milliseconds(250)  << "\n";
   assert(!s.try_acquire_until(start + std::chrono::milliseconds(250)));
-  log() << "done: try_acquire_until: start + " <<  std::chrono::milliseconds(250)  << "\n";
+  log() << "done:  try_acquire_until: start + " <<  std::chrono::milliseconds(250)  << "\n";
 
-  log() << "try_acquire_for: " << std::chrono::milliseconds(250) << "\n";
+  log() << "start: try_acquire_for: " << std::chrono::milliseconds(250) << "\n";
   assert(!s.try_acquire_for(std::chrono::milliseconds(250)));
-  log() << "done: try_acquire_for: " << std::chrono::milliseconds(250) << "\n";
+  log() << "done:  try_acquire_for: " << std::chrono::milliseconds(250) << "\n";
 
   std::thread t = support::make_test_thread([&](){
     std::this_thread::sleep_for(std::chrono::milliseconds(250));
@@ -68,17 +67,26 @@ int main(int, char**)
     s.release();
   });
 
-  log() << "try_acquire_until: start + " <<  std::chrono::seconds(2)  << "\n";
+  log() << "start: try_acquire_until: start + " <<  std::chrono::seconds(2)  << "\n";
   assert(s.try_acquire_until(start + std::chrono::seconds(2)));
-  log() << "done: try_acquire_until: start + " <<  std::chrono::seconds(2)  << "\n";
+  log() << "done:  try_acquire_until: start + " <<  std::chrono::seconds(2)  << "\n";
 
-  log() << "try_acquire_for: " << std::chrono::seconds(2) << "\n";
+  log() << "start: try_acquire_for: " << std::chrono::seconds(2) << "\n";
   assert(s.try_acquire_for(std::chrono::seconds(2)));
-  log() << "done: try_acquire_for: " << std::chrono::seconds(2) << "\n";
+  log() << "done:  try_acquire_for: " << std::chrono::seconds(2) << "\n";
   t.join();
 
   auto const end = std::chrono::steady_clock::now();
   assert(end - start < std::chrono::seconds(10));
+}
+
+int main(int, char**)
+{
+  auto const log_start = std::chrono::steady_clock::now();
+  for (auto i = 0; i < 10; ++i) {
+    std::cerr << "=== Iteration " << i << " ===\n";
+    test(log_start);
+  }
 
   return 1;
 }

>From 09616457c9c0868806e5a3735ca9706f963e6066 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sat, 7 Feb 2026 16:44:11 +0000
Subject: [PATCH 3/8] stage3

---
 libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
index 904da25a49940..d032145f9824d 100644
--- a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
+++ b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
@@ -88,5 +88,10 @@ int main(int, char**)
     test(log_start);
   }
 
+#if defined(_WIN32)
   return 1;
+#else
+  return 0;
+#endif
+
 }

>From d77abc88ea2e0255caf69a682ef7dd896e1dca56 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 8 Feb 2026 08:39:09 +0000
Subject: [PATCH 4/8] debug small

---
 .../thread.semaphore/timed.debug.pass.cpp     | 39 ++++++-------------
 1 file changed, 12 insertions(+), 27 deletions(-)

diff --git a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
index d032145f9824d..0294d1fef680d 100644
--- a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
+++ b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
@@ -37,13 +37,13 @@ void test(auto log_start){
     auto seconds = duration_cast<std::chrono::seconds>(elapsed);
     elapsed -= seconds;
 
-    auto milliseconds = duration_cast<std::chrono::milliseconds>(elapsed);
+    auto nanoseconds = duration_cast<std::chrono::nanoseconds>(elapsed);
 
     std::cerr << "["
               << std::setw(2) << std::setfill('0') << hours.count() << ":"
               << std::setw(2) << std::setfill('0') << minutes.count() << ":"
               << std::setw(2) << std::setfill('0') << seconds.count() << "."
-              << std::setw(3) << std::setfill('0') << milliseconds.count()
+              << std::setw(9) << std::setfill('0') << nanoseconds.count()
               << "] ";
 
     return std::cerr;
@@ -52,29 +52,18 @@ void test(auto log_start){
   auto const start = std::chrono::steady_clock::now();
   std::counting_semaphore<> s(0);
 
-  log() << "start: try_acquire_until: start + " <<  std::chrono::milliseconds(250)  << "\n";
-  assert(!s.try_acquire_until(start + std::chrono::milliseconds(250)));
-  log() << "done:  try_acquire_until: start + " <<  std::chrono::milliseconds(250)  << "\n";
+  log() << "start: try_acquire_for: " << std::chrono::nanoseconds(1) << "\n";
+  assert(!s.try_acquire_for(std::chrono::nanoseconds(1)));
+  log() << "done:  try_acquire_for: " << std::chrono::nanoseconds(1) << "\n";
 
-  log() << "start: try_acquire_for: " << std::chrono::milliseconds(250) << "\n";
-  assert(!s.try_acquire_for(std::chrono::milliseconds(250)));
-  log() << "done:  try_acquire_for: " << std::chrono::milliseconds(250) << "\n";
+  log() << "start: try_acquire_for: " << std::chrono::microseconds(1) << "\n";
+  assert(!s.try_acquire_for(std::chrono::microseconds(1)));
+  log() << "done:  try_acquire_for: " << std::chrono::microseconds(1) << "\n";
 
-  std::thread t = support::make_test_thread([&](){
-    std::this_thread::sleep_for(std::chrono::milliseconds(250));
-    s.release();
-    std::this_thread::sleep_for(std::chrono::milliseconds(250));
-    s.release();
-  });
+  log() << "start: try_acquire_for: " << std::chrono::milliseconds(1) << "\n";
+  assert(!s.try_acquire_for(std::chrono::milliseconds(1)));
+  log() << "done:  try_acquire_for: " << std::chrono::milliseconds(1) << "\n";
 
-  log() << "start: try_acquire_until: start + " <<  std::chrono::seconds(2)  << "\n";
-  assert(s.try_acquire_until(start + std::chrono::seconds(2)));
-  log() << "done:  try_acquire_until: start + " <<  std::chrono::seconds(2)  << "\n";
-
-  log() << "start: try_acquire_for: " << std::chrono::seconds(2) << "\n";
-  assert(s.try_acquire_for(std::chrono::seconds(2)));
-  log() << "done:  try_acquire_for: " << std::chrono::seconds(2) << "\n";
-  t.join();
 
   auto const end = std::chrono::steady_clock::now();
   assert(end - start < std::chrono::seconds(10));
@@ -83,15 +72,11 @@ void test(auto log_start){
 int main(int, char**)
 {
   auto const log_start = std::chrono::steady_clock::now();
-  for (auto i = 0; i < 10; ++i) {
+  for (auto i = 0; i < 100; ++i) {
     std::cerr << "=== Iteration " << i << " ===\n";
     test(log_start);
   }
 
-#if defined(_WIN32)
-  return 1;
-#else
   return 0;
-#endif
 
 }

>From 93cace578c2f07f0ce642ac5deb664074c056025 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Wed, 11 Feb 2026 08:58:27 +0000
Subject: [PATCH 5/8] debug

---
 libcxx/include/__thread/poll_with_backoff.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__thread/poll_with_backoff.h b/libcxx/include/__thread/poll_with_backoff.h
index e007e7746ca52..d47ca90c663f4 100644
--- a/libcxx/include/__thread/poll_with_backoff.h
+++ b/libcxx/include/__thread/poll_with_backoff.h
@@ -13,6 +13,8 @@
 #include <__chrono/duration.h>
 #include <__chrono/high_resolution_clock.h>
 #include <__config>
+    #include <cstdio>
+      
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -59,7 +61,9 @@ _LIBCPP_HIDE_FROM_ABI __poll_with_backoff_results __libcpp_thread_poll_with_back
       __count += 1;
       continue;
     }
-    chrono::nanoseconds const __elapsed = chrono::high_resolution_clock::now() - __start;
+    auto __now = chrono::high_resolution_clock::now();
+    chrono::nanoseconds const __elapsed = __now - __start;
+    std::fprintf(stderr, "start: %lld ns\nnow: %lld ns\nelapsed: %lld ns\n", __start.time_since_epoch().count(), __now.time_since_epoch().count(), __elapsed.count());
     if (__max_elapsed != chrono::nanoseconds::zero() && __max_elapsed < __elapsed)
       return __poll_with_backoff_results::__timeout;
     if (auto __backoff_res = __backoff(__elapsed); __backoff_res == __backoff_results::__continue_poll)

>From 4101ae47509675e6298a2ff7acb038292b6a71ba Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Wed, 11 Feb 2026 10:00:13 +0000
Subject: [PATCH 6/8] debug

---
 libcxx/include/__thread/poll_with_backoff.h                  | 4 ++++
 libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp | 4 +++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__thread/poll_with_backoff.h b/libcxx/include/__thread/poll_with_backoff.h
index d47ca90c663f4..e9fd784a25674 100644
--- a/libcxx/include/__thread/poll_with_backoff.h
+++ b/libcxx/include/__thread/poll_with_backoff.h
@@ -13,7 +13,9 @@
 #include <__chrono/duration.h>
 #include <__chrono/high_resolution_clock.h>
 #include <__config>
+    #if defined (TEST_DEBUG)
     #include <cstdio>
+    #endif
       
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -63,7 +65,9 @@ _LIBCPP_HIDE_FROM_ABI __poll_with_backoff_results __libcpp_thread_poll_with_back
     }
     auto __now = chrono::high_resolution_clock::now();
     chrono::nanoseconds const __elapsed = __now - __start;
+    #if defined (TEST_DEBUG)
     std::fprintf(stderr, "start: %lld ns\nnow: %lld ns\nelapsed: %lld ns\n", __start.time_since_epoch().count(), __now.time_since_epoch().count(), __elapsed.count());
+    #endif
     if (__max_elapsed != chrono::nanoseconds::zero() && __max_elapsed < __elapsed)
       return __poll_with_backoff_results::__timeout;
     if (auto __backoff_res = __backoff(__elapsed); __backoff_res == __backoff_results::__continue_poll)
diff --git a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
index 0294d1fef680d..96d10e02b4986 100644
--- a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
+++ b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
@@ -8,9 +8,11 @@
 //
 // UNSUPPORTED: no-threads
 // UNSUPPORTED: c++03, c++11, c++14, c++17
+// ADDITIONAL_COMPILE_FLAGS: -DTEST_DEBUG
 
 // <semaphore>
 
+
 #include <semaphore>
 #include <thread>
 #include <chrono>
@@ -77,6 +79,6 @@ int main(int, char**)
     test(log_start);
   }
 
-  return 0;
+  return 1;
 
 }

>From b35dcaec5023c6fbbc016cd71970b434a3277349 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Wed, 11 Feb 2026 12:16:00 +0000
Subject: [PATCH 7/8] ci

---
 libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
index 96d10e02b4986..42c34a38362d3 100644
--- a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
+++ b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
@@ -79,6 +79,6 @@ int main(int, char**)
     test(log_start);
   }
 
-  return 1;
+  return 0;
 
 }

>From 445005b75528ec845aaf59ba7017b06bdcd2cfdb Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Wed, 11 Feb 2026 14:23:47 +0000
Subject: [PATCH 8/8] timed

---
 .../thread.semaphore/timed.debug.pass.cpp     | 24 ++++++++-----------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
index 42c34a38362d3..eef5dfb698295 100644
--- a/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
+++ b/libcxx/test/std/thread/thread.semaphore/timed.debug.pass.cpp
@@ -12,7 +12,6 @@
 
 // <semaphore>
 
-
 #include <semaphore>
 #include <thread>
 #include <chrono>
@@ -20,12 +19,11 @@
 #include <iostream>
 #include <iomanip>
 
-
 #include "make_test_thread.h"
 #include "test_macros.h"
 
-void test(auto log_start){
-  auto log = [log_start] ()-> auto& {
+void test(auto log_start) {
+  auto log = [log_start]() -> auto& {
     using namespace std::chrono;
 
     auto elapsed = steady_clock::now() - log_start;
@@ -41,12 +39,9 @@ void test(auto log_start){
 
     auto nanoseconds = duration_cast<std::chrono::nanoseconds>(elapsed);
 
-    std::cerr << "["
-              << std::setw(2) << std::setfill('0') << hours.count() << ":"
-              << std::setw(2) << std::setfill('0') << minutes.count() << ":"
-              << std::setw(2) << std::setfill('0') << seconds.count() << "."
-              << std::setw(9) << std::setfill('0') << nanoseconds.count()
-              << "] ";
+    std::cerr << "[" << std::setw(2) << std::setfill('0') << hours.count() << ":" << std::setw(2) << std::setfill('0')
+              << minutes.count() << ":" << std::setw(2) << std::setfill('0') << seconds.count() << "." << std::setw(9)
+              << std::setfill('0') << nanoseconds.count() << "] ";
 
     return std::cerr;
   };
@@ -66,19 +61,20 @@ void test(auto log_start){
   assert(!s.try_acquire_for(std::chrono::milliseconds(1)));
   log() << "done:  try_acquire_for: " << std::chrono::milliseconds(1) << "\n";
 
-
   auto const end = std::chrono::steady_clock::now();
   assert(end - start < std::chrono::seconds(10));
 }
 
-int main(int, char**)
-{
+int main(int, char**) {
   auto const log_start = std::chrono::steady_clock::now();
   for (auto i = 0; i < 100; ++i) {
     std::cerr << "=== Iteration " << i << " ===\n";
     test(log_start);
   }
 
+#if defined(_WIN32)
+  return 1;
+#else
   return 0;
-
+#endif
 }



More information about the libcxx-commits mailing list