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

Message ID bfc5aa5bb099c28f73d366af396d8c2e253c195c.1569660153.git-series.mac@mcrowe.com
State Superseded
Headers show
Series
  • timed_mutex, shared_timed_mutex: Add full steady clock support
Related show

Commit Message

Mike Crowe Sept. 28, 2019, 8:44 a.m.
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(-)

-- 
git-series 0.9.1

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