diff mbox series

[v2,10/11] libstdc++ timed_mutex: Ensure that try_lock_for waits for long enough

Message ID a0098939f9f9eb5f8f1bc604614f19a496591320.1571162241.git-series.mac@mcrowe.com
State New
Headers show
Series timed_mutex, shared_timed_mutex: Add full steady clock support | expand

Commit Message

Mike Crowe Oct. 15, 2019, 5:57 p.m. UTC
The user-defined clock used with shared_mutex::try_lock_for and
shared_mutex::try_lock_shared_for may have higher precision than
__clock_t. We may need to round the duration up to ensure that the timeout
is long enough. (See __timed_mutex_impl::_M_try_lock_for)

	* include/std/shared_mutex: (try_lock_for) Round up wait duration
          if necessary. (try_lock_shared_for) Likewise.
---
 libstdc++-v3/include/std/shared_mutex | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/libstdc++-v3/include/std/shared_mutex b/libstdc++-v3/include/std/shared_mutex
index b637bdc..6e67324 100644
--- a/libstdc++-v3/include/std/shared_mutex
+++ b/libstdc++-v3/include/std/shared_mutex
@@ -471,9 +471,12 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     template<typename _Rep, typename _Period>
       bool
-      try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
+      try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
       {
-	return try_lock_until(__clock_t::now() + __rel_time);
+        auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime);
+        if (ratio_greater<__clock_t::period, _Period>())
+          ++__rt;
+	return try_lock_until(__clock_t::now() + __rt);
       }
 
     // Shared ownership
@@ -484,9 +487,12 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     template<typename _Rep, typename _Period>
       bool
-      try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
+      try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rtime)
       {
-	return try_lock_shared_until(__clock_t::now() + __rel_time);
+        auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime);
+        if (ratio_greater<__clock_t::period, _Period>())
+          ++__rt;
+	return try_lock_shared_until(__clock_t::now() + __rt);
       }
 
 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK