[PATCH] Remove binary_semaphore implementation from stop_token

Thomas Rodgers rodgert@appliantology.com
Sat Jun 6 21:26:51 GMT 2020


        * include/std/stop_token: Remove local binary_semaphore implementation.
          (_Stop_state_t::_M_do_try_lock): Use __thread_yield() from
          bits/atomic_wait.h.
---
 libstdc++-v3/include/std/stop_token | 40 ++---------------------------
 1 file changed, 2 insertions(+), 38 deletions(-)

diff --git a/libstdc++-v3/include/std/stop_token b/libstdc++-v3/include/std/stop_token
index 847d12f7454..40a71574a7e 100644
--- a/libstdc++-v3/include/std/stop_token
+++ b/libstdc++-v3/include/std/stop_token
@@ -36,9 +36,7 @@
 #ifdef _GLIBCXX_HAS_GTHREADS
 # define __cpp_lib_jthread 201911L
 # include <bits/gthr.h>
-# if __has_include(<semaphore>)
-#  include <semaphore>
-# endif
+# include <semaphore>
 #endif
 
 namespace std _GLIBCXX_VISIBILITY(default)
@@ -100,40 +98,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     template<typename _Callback>
       friend class stop_callback;
 
-    static void
-    _S_yield() noexcept
-    {
-#if defined __i386__ || defined __x86_64__
-      __builtin_ia32_pause();
-#elif defined _GLIBCXX_USE_SCHED_YIELD
-      __gthread_yield();
-#endif
-    }
-
-#ifndef __cpp_lib_semaphore
-    // TODO: replace this with a real implementation of std::binary_semaphore
-    struct binary_semaphore
-    {
-      explicit binary_semaphore(int __d) : _M_counter(__d > 0) { }
-
-      void release() { _M_counter.fetch_add(1, memory_order::release); }
-
-      void acquire()
-      {
-	int __old = 1;
-	while (!_M_counter.compare_exchange_weak(__old, 0,
-						 memory_order::acquire,
-						 memory_order::relaxed))
-	  {
-	    __old = 1;
-	    _S_yield();
-	  }
-      }
-
-      atomic<int> _M_counter;
-    };
-#endif
-
     struct _Stop_cb
     {
       using __cb_type = void(_Stop_cb*) noexcept;
@@ -389,7 +353,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       {
 	if (__curval & _S_locked_bit)
 	  {
-	    _S_yield();
+	    __detail::__thread_yield();
 	    __curval = _M_value.load(__failure);
 	    return false;
 	  }
-- 
2.26.2



More information about the Gcc-patches mailing list