[libcxx-commits] [libcxx] [libc++] optimise stop_token (PR #236943)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Nov 1 01:30:27 PST 2026


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

>From 012579ab87a17f38abfcce32011ab9929a685094 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Thu, 29 Oct 2026 20:53:44 +0000
Subject: [PATCH 1/2] [libc++] optimise stop_token

---
 libcxx/include/__stop_token/atomic_unique_lock.h | 6 ++----
 libcxx/include/__stop_token/stop_state.h         | 2 +-
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/libcxx/include/__stop_token/atomic_unique_lock.h b/libcxx/include/__stop_token/atomic_unique_lock.h
index 99f94b8a789371..f0b0ddee013555 100644
--- a/libcxx/include/__stop_token/atomic_unique_lock.h
+++ b/libcxx/include/__stop_token/atomic_unique_lock.h
@@ -14,6 +14,7 @@
 #include <__atomic/memory_order.h>
 #include <__bit/has_single_bit.h>
 #include <__config>
+#include <__thread/this_thread.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -82,7 +83,6 @@ class __atomic_unique_lock {
     // unset the _LockedBit. `memory_order_release` because we need to make sure all the write operations before calling
     // `__unlock` will be made visible to other threads
     __state_.fetch_and(static_cast<_State>(~_LockedBit), std::memory_order_release);
-    __state_.notify_all();
     __is_locked_ = false;
   }
 
@@ -104,9 +104,7 @@ class __atomic_unique_lock {
           return false;
         } else if ((__current_state & _LockedBit) != 0) {
           // another thread has locked the state, we need to wait
-          __state_.wait(__current_state, std::memory_order_relaxed);
-          // when it is woken up by notifyAll or spuriously, the __state_
-          // might have changed. reload the state
+          this_thread::yield();
           // Note that the new state's _LockedBit may or may not equal to 0
           __current_state = __state_.load(std::memory_order_relaxed);
         } else {
diff --git a/libcxx/include/__stop_token/stop_state.h b/libcxx/include/__stop_token/stop_state.h
index 762cf203b0af97..fa93bbe1f8c8be 100644
--- a/libcxx/include/__stop_token/stop_state.h
+++ b/libcxx/include/__stop_token/stop_state.h
@@ -171,7 +171,7 @@ class __stop_state {
     // under below condition, the request_stop call just popped __cb from the list and could execute it now
     bool __potentially_executing_now = __cb->__prev_ == nullptr && !__callback_list_.__is_head(__cb);
 
-    if (__potentially_executing_now) {
+    if (__potentially_executing_now) [[unlikely]] {
       auto __requested_thread = __requesting_thread_;
       __cb_list_lock.__unlock();
 

>From de648aed8a27995e59e7917280e2cb5f3d9cb954 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sun, 1 Nov 2026 09:30:15 +0000
Subject: [PATCH 2/2] test

---
 libcxx/test/libcxx/transitive_includes/cxx20.csv | 1 +
 libcxx/test/libcxx/transitive_includes/cxx23.csv | 1 +
 libcxx/test/libcxx/transitive_includes/cxx26.csv | 1 +
 3 files changed, 3 insertions(+)

diff --git a/libcxx/test/libcxx/transitive_includes/cxx20.csv b/libcxx/test/libcxx/transitive_includes/cxx20.csv
index b9d1dd17aa016f..75c1ef4bd34438 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx20.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx20.csv
@@ -859,6 +859,7 @@ stack limits
 stack stdexcept
 stack tuple
 stack version
+stop_token cerrno
 stop_token climits
 stop_token cstdint
 stop_token cstring
diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv
index 83125257f35f54..71d8949e4f6476 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx23.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv
@@ -902,6 +902,7 @@ stack limits
 stack stdexcept
 stack tuple
 stack version
+stop_token cerrno
 stop_token climits
 stop_token cstdint
 stop_token cstring
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index 229e85afd106b7..5702af75bedfab 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -883,6 +883,7 @@ stack limits
 stack stdexcept
 stack tuple
 stack version
+stop_token cerrno
 stop_token climits
 stop_token cstdint
 stop_token cstring



More information about the libcxx-commits mailing list