public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-67] libstdc++: Fix semaphore to work with system_clock timeouts
@ 2021-04-22 16:44 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-04-22 16:44 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:6924588774a02dec63fb4b0ad19aed303accc2d2

commit r12-67-g6924588774a02dec63fb4b0ad19aed303accc2d2
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Apr 22 17:26:50 2021 +0100

    libstdc++: Fix semaphore to work with system_clock timeouts
    
    The __cond_wait_until_impl function takes a steady_clock timeout, but
    then sometimes tries to compare it to a time from the system_clock,
    which won't compile.  Additionally, that function gets called with
    system_clock timeouts, which also won't compile. This makes the function
    accept timeouts for either clock, and compare to the time from the right
    clock.
    
    This fixes the compilation error that was causing two tests to fail on
    non-futex targets, so we can revert the r12-11 change to disable them.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/atomic_timed_wait.h (__cond_wait_until_impl):
            Handle system_clock as well as steady_clock.
            * testsuite/30_threads/semaphore/try_acquire_for.cc: Re-enable.
            * testsuite/30_threads/semaphore/try_acquire_until.cc:
            Re-enable.

Diff:
---
 libstdc++-v3/include/bits/atomic_timed_wait.h      | 22 +++++++++++++---------
 .../30_threads/semaphore/try_acquire_for.cc        |  2 --
 .../30_threads/semaphore/try_acquire_until.cc      |  2 --
 3 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/libstdc++-v3/include/bits/atomic_timed_wait.h b/libstdc++-v3/include/bits/atomic_timed_wait.h
index 70e5335cfd7..ec7ff51cdbc 100644
--- a/libstdc++-v3/include/bits/atomic_timed_wait.h
+++ b/libstdc++-v3/include/bits/atomic_timed_wait.h
@@ -139,12 +139,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 // (e.g. __ulock_wait())which is better than pthread_cond_clockwait
 #endif // ! PLATFORM_TIMED_WAIT
 
-    // returns true if wait ended before timeout
-    template<typename _Dur>
+    // Returns true if wait ended before timeout.
+    // _Clock must be either steady_clock or system_clock.
+    template<typename _Clock, typename _Dur>
       bool
       __cond_wait_until_impl(__condvar& __cv, mutex& __mx,
-	  const chrono::time_point<chrono::steady_clock, _Dur>& __atime)
+			     const chrono::time_point<_Clock, _Dur>& __atime)
       {
+	static_assert(std::__is_one_of<_Clock, chrono::steady_clock,
+					       chrono::system_clock>::value);
+
 	auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
 	auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
 
@@ -155,12 +159,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  };
 
 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
-	__cv.wait_until(__mx, CLOCK_MONOTONIC, __ts);
-	return chrono::steady_clock::now() < __atime;
-#else
-	__cv.wait_until(__mx, __ts);
-	return chrono::system_clock::now() < __atime;
-#endif // ! _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
+	if constexpr (is_same_v<chrono::steady_clock, _Clock>)
+	  __cv.wait_until(__mx, CLOCK_MONOTONIC, __ts);
+	else
+#endif
+	  __cv.wait_until(__mx, __ts);
+	return _Clock::now() < __atime;
       }
 
     // returns true if wait ended before timeout
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc
index 248ecb07e56..e7edc9eeef1 100644
--- a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc
@@ -21,8 +21,6 @@
 // { dg-require-gthreads "" }
 // { dg-add-options libatomic }
 
-// { dg-skip-if "FIXME: fails" { ! futex } }
-
 #include <semaphore>
 #include <chrono>
 #include <thread>
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc
index eb1351cd2bf..49ba33b4999 100644
--- a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc
@@ -21,8 +21,6 @@
 // { dg-additional-options "-pthread" { target pthread } }
 // { dg-add-options libatomic }
 
-// { dg-skip-if "FIXME: fails" { ! futex } }
-
 #include <semaphore>
 #include <chrono>
 #include <thread>


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

only message in thread, other threads:[~2021-04-22 16:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-22 16:44 [gcc r12-67] libstdc++: Fix semaphore to work with system_clock timeouts 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).