]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix semaphore to work with system_clock timeouts
authorJonathan Wakely <jwakely@redhat.com>
Thu, 22 Apr 2021 16:26:50 +0000 (17:26 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 23 Apr 2021 09:39:16 +0000 (10:39 +0100)
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.

(cherry picked from commit 6924588774a02dec63fb4b0ad19aed303accc2d2)

libstdc++-v3/include/bits/atomic_timed_wait.h
libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc
libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc

index 70e5335cfd7485c3a3bce428e74bda97097e77b3..ec7ff51cdbc6ef33db6968a591d7993cbdcc213a 100644 (file)
@@ -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
index 248ecb07e563aff1c38b393c0ce13b5e78243a04..e7edc9eeef1b9f8bae12a5ce1179b68734264f26 100644 (file)
@@ -21,8 +21,6 @@
 // { dg-require-gthreads "" }
 // { dg-add-options libatomic }
 
-// { dg-skip-if "FIXME: fails" { ! futex } }
-
 #include <semaphore>
 #include <chrono>
 #include <thread>
index eb1351cd2bf7f42c0ff4cc07af4dee8959659704..49ba33b499981ba760cf32ffffecfa8e461562e1 100644 (file)
@@ -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>