[libcxx-commits] [libcxx] [libc++] stop_token uses yield instead of wait in the locking mechanism (PR #191184)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Apr 9 06:02:04 PDT 2026


https://github.com/huixie90 created https://github.com/llvm/llvm-project/pull/191184

None

>From 0608cfcb3fbfa9422ef5e590e41cbff4f62ec5c7 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Thu, 9 Apr 2026 14:01:38 +0100
Subject: [PATCH] [libc++] stop_token uses yield instead of wait in the locking
 mechanism

---
 libcxx/include/__stop_token/atomic_unique_lock.h | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__stop_token/atomic_unique_lock.h b/libcxx/include/__stop_token/atomic_unique_lock.h
index 0792331413f52..6da78658a97ad 100644
--- a/libcxx/include/__stop_token/atomic_unique_lock.h
+++ b/libcxx/include/__stop_token/atomic_unique_lock.h
@@ -12,6 +12,7 @@
 
 #include <__bit/has_single_bit.h>
 #include <__config>
+#include <__thread/this_thread.h>
 #include <atomic>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -81,7 +82,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;
   }
 
@@ -103,9 +103,8 @@ 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();
+          // the __state_ might have changed. reload the state
           // Note that the new state's _LockedBit may or may not equal to 0
           __current_state = __state_.load(std::memory_order_relaxed);
         } else {



More information about the libcxx-commits mailing list