]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Ensure counting_semaphore::try_acquire_for times out [PR122878]
authorJonathan Wakely <jwakely@redhat.com>
Fri, 9 Jan 2026 15:29:13 +0000 (15:29 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 9 Jan 2026 19:18:21 +0000 (19:18 +0000)
As noted in Bug 122878 comment 2, the _M_try_acquire_for implementation
doesn't reduce the remaining timeout each time it returns from an atomic
waiting function. This means that it can wait longer than requested, or
even loop forever. If there is a spurious wake from the timed waiting
function (__wait_until_impl) it will return indicating no timeout
occurred, which means the caller will check the value and potentially
sleep again. If spurious wakes happen every time, it will just keep
sleeping in a loop forever. This is observed to actually happen on
FreeBSD 14.0-STABLE where pthread_cond_timedwait gets a spurious wake
and so never times out.

The solution in this commit is to replace the implementation of
_M_try_acquire_for with a call to _M_try_acquire_until, converting the
relative timeout to an absolute timeout against the steady clock. This
is what ends up happening anyway, because we only have a
__wait_until_impl entry point into the library internals, so
__atomic_wait_address_for already converts the relative timeout to an
absolute timeout (except for the special case of a zero-value duration,
which only checks for an update while spinning for a finite number of
iterations, and doesn't sleep).

As noted in comment 4 of the PR, this requires some changes to
_M_try_acquire which was relying on the behaviour of _M_try_acquire_for
for zero-value durations.  That behaviour is desirable for
_M_try_acquire so that it can handle short-lived contention without
failing immediately. To preserve that behaviour of _M_try_acquire it is
changed to do its own loop and to call __atomic_wait_address_for
directly with a zero duration, to do the spinloop.

libstdc++-v3/ChangeLog:

PR libstdc++/122878
* include/bits/semaphore_base.h (_M_try_acquire): Replace
_M_try_acquire_for call with explicit loop and call to
__atomic_wait_address_for.
(_M_try_acquire_for): Replace loop with call to
_M_try_acquire_until.

Co-authored-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/semaphore_base.h

index 5a1e0a4162493ed75031759a355911728ed7feee..679b43e4cb1c93ace766beb62de098a3cf20f004 100644 (file)
@@ -100,8 +100,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       // The fastest implementation of this function is just _M_do_try_acquire
       // but that can fail under contention even when _M_count > 0.
-      // Using _M_try_acquire_for(0ns) will retry a few times in a loop.
-      return _M_try_acquire_for(__detail::__wait_clock_t::duration{});
+
+      auto __vfn = [this]{ return _M_get_current(); };
+      _Available __is_available{__vfn()};
+
+      // Retry the compare exchange a few times in case of contention:
+      for (int __i = 0; __i < 10 && __is_available(); ++__i)
+       if (_M_do_try_acquire(__is_available._M_val))
+         return true;
+
+      // Spinloop to see if it becomes available.
+      constexpr auto __zero = __detail::__wait_clock_t::duration{};
+      if (std::__atomic_wait_address_for(&_M_counter, __is_available,
+                                        __vfn, __zero, true))
+       return false; // timed out
+
+      // Should be available, try once more to acquire it:
+      return _M_do_try_acquire(__is_available._M_val);
     }
 
     template<typename _Clock, typename _Duration>
@@ -122,14 +137,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       bool
       _M_try_acquire_for(const chrono::duration<_Rep, _Period>& __rtime) noexcept
       {
-       auto __vfn = [this]{ return _M_get_current(); };
-       _Available __is_available{__vfn()};
-       while (!_M_do_try_acquire(__is_available._M_val))
-         if (!__is_available())
-           if (!std::__atomic_wait_address_for(&_M_counter, __is_available,
-                                               __vfn, __rtime, true))
-             return false; // timed out
-       return true;
+       return _M_try_acquire_until(__detail::__wait_clock_t::now() + __rtime);
       }
 
     _GLIBCXX_ALWAYS_INLINE ptrdiff_t