public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-5385] libstdc++: Remove redundant clock conversions in atomic waits
@ 2020-11-25 18:38 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2020-11-25 18:38 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:dfc537e554afa98b42a4b203ffd08c0eddba746e

commit r11-5385-gdfc537e554afa98b42a4b203ffd08c0eddba746e
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Nov 25 17:59:44 2020 +0000

    libstdc++: Remove redundant clock conversions in atomic waits
    
    For the case where a timeout is specified using the system_clock we
    perform a conversion to the preferred clock (which is either
    steady_clock or system_clock itself), wait using __cond_wait_until_impl,
    and then check the time by that clock again to see if it was reached.
    This is entirely redundant, as we can just call __cond_wait_until_impl
    directly. It will wait using the specified clock, and there's no need to
    check the time twice. For the no_timeout case this removes two
    unnecessary calls to the clock's now() function, and for the timeout
    case it removes three calls.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/atomic_timed_wait.h (__cond_wait_until): Do not
            perform redundant conversions to the same clock.

Diff:
---
 libstdc++-v3/include/bits/atomic_timed_wait.h | 40 ++++++++++++++++-----------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/libstdc++-v3/include/bits/atomic_timed_wait.h b/libstdc++-v3/include/bits/atomic_timed_wait.h
index 9e44114dd5b..1c91c858ce7 100644
--- a/libstdc++-v3/include/bits/atomic_timed_wait.h
+++ b/libstdc++-v3/include/bits/atomic_timed_wait.h
@@ -166,24 +166,32 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __cond_wait_until(__condvar& __cv, mutex& __mx,
 	  const chrono::time_point<_Clock, _Duration>& __atime)
       {
-#ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
-	using __clock_t = chrono::steady_clock;
-#else
+#ifndef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
 	using __clock_t = chrono::system_clock;
+#else
+	using __clock_t = chrono::steady_clock;
+	if constexpr (is_same_v<_Clock, chrono::steady_clock>)
+	  return __detail::__cond_wait_until_impl(__cv, __mx, __atime);
+	else
 #endif
-	const typename _Clock::time_point __c_entry = _Clock::now();
-	const __clock_t::time_point __s_entry = __clock_t::now();
-	const auto __delta = __atime - __c_entry;
-	const auto __s_atime = __s_entry + __delta;
-	if (__detail::__cond_wait_until_impl(__cv, __mx, __s_atime)
-	    == __atomic_wait_status::no_timeout)
-	  return __atomic_wait_status::no_timeout;
-	// We got a timeout when measured against __clock_t but
-	// we need to check against the caller-supplied clock
-	// to tell whether we should return a timeout.
-	if (_Clock::now() < __atime)
-	  return __atomic_wait_status::no_timeout;
-	return __atomic_wait_status::timeout;
+	if constexpr (is_same_v<_Clock, chrono::system_clock>)
+	  return __detail::__cond_wait_until_impl(__cv, __mx, __atime);
+	else
+	  {
+	    const typename _Clock::time_point __c_entry = _Clock::now();
+	    const __clock_t::time_point __s_entry = __clock_t::now();
+	    const auto __delta = __atime - __c_entry;
+	    const auto __s_atime = __s_entry + __delta;
+	    if (__detail::__cond_wait_until_impl(__cv, __mx, __s_atime)
+		== __atomic_wait_status::no_timeout)
+	      return __atomic_wait_status::no_timeout;
+	    // We got a timeout when measured against __clock_t but
+	    // we need to check against the caller-supplied clock
+	    // to tell whether we should return a timeout.
+	    if (_Clock::now() < __atime)
+	      return __atomic_wait_status::no_timeout;
+	    return __atomic_wait_status::timeout;
+	  }
       }
 #endif // FUTEX


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-11-25 18:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-25 18:38 [gcc r11-5385] libstdc++: Remove redundant clock conversions in atomic waits Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).