[PATCH] [libstdc++] Refactor/cleanup of atomic wait implementation

Jonathan Wakely jwakely@redhat.com
Tue Apr 20 13:38:02 GMT 2021


On 19/04/21 12:23 -0700, Thomas Rodgers wrote:
>+    struct __timed_backoff_spin_policy
>+    {
>+      __wait_clock_t::time_point _M_deadline;
>+      __wait_clock_t::time_point _M_t0;
>+
>+      template<typename _Clock, typename _Dur>
>+	__timed_backoff_spin_policy(chrono::time_point<_Clock, _Dur>
>+				      __deadline = _Clock::time_point::max(),
>+				    chrono::time_point<_Clock, _Dur>
>+				      __t0 = _Clock::now()) noexcept
>+	  : _M_deadline(__to_wait_clock(__deadline))
>+	  , _M_t0(__to_wait_clock(__t0))
>+	{ }
>+
>+      bool
>+      operator()() const noexcept
>       {
>-	static_assert(sizeof(__timed_waiters) == sizeof(__waiters));
>-	return static_cast<__timed_waiters&>(__waiters::_S_for(__t));
>+	using namespace literals::chrono_literals;
>+	auto __now = __wait_clock_t::now();
>+	if (_M_deadline <= __now)
>+	  return false;
>+
>+	auto __elapsed = __now - _M_t0;
>+	if (__elapsed > 128ms)
>+	  {
>+	    this_thread::sleep_for(64ms);
>+	  }
>+	else if (__elapsed > 64us)
>+	  {
>+	    this_thread::sleep_for(__elapsed / 2);
>+	  }
>+	else if (__elapsed > 4us)
>+	  {
>+	    __thread_yield();
>+	  }
>+	else
>+	  return false;

Ah, the reason for some of the time outs I'm seeing is that this
function doesn't return anything!

/home/jwakely/gcc/12/include/c++/12.0.0/bits/atomic_timed_wait.h: In member function 'bool std::__detail::__timed_backoff_spin_policy::operator()() const':
/home/jwakely/gcc/12/include/c++/12.0.0/bits/atomic_timed_wait.h:259:7: warning: control reaches end of non-void function [-Wreturn-type]
   259 |       }
       |       ^

Should it return true if it waited?



More information about the Libstdc++ mailing list