Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2008-07-05 17:55:37


Author: anthonyw
Date: 2008-07-05 17:55:36 EDT (Sat, 05 Jul 2008)
New Revision: 47120
URL: http://svn.boost.org/trac/boost/changeset/47120

Log:
Test and fix for issue #2076
Text files modified:
   trunk/boost/thread/pthread/condition_variable.hpp | 11 +++++
   trunk/boost/thread/pthread/condition_variable_fwd.hpp | 10 ++++
   trunk/libs/thread/test/test_condition_timed_wait_times_out.cpp | 88 +++++++++++++++++++++++++++++++++++++++
   3 files changed, 107 insertions(+), 2 deletions(-)

Modified: trunk/boost/thread/pthread/condition_variable.hpp
==============================================================================
--- trunk/boost/thread/pthread/condition_variable.hpp (original)
+++ trunk/boost/thread/pthread/condition_variable.hpp 2008-07-05 17:55:36 EDT (Sat, 05 Jul 2008)
@@ -121,6 +121,17 @@
             }
             return true;
         }
+ template<typename lock_type>
+ bool timed_wait(lock_type& m,xtime const& wait_until)
+ {
+ return timed_wait(m,system_time(wait_until));
+ }
+
+ template<typename lock_type,typename duration_type>
+ bool timed_wait(lock_type& m,duration_type const& wait_duration)
+ {
+ return timed_wait(m,get_system_time()+wait_duration);
+ }
 
         template<typename lock_type,typename predicate_type>
         bool timed_wait(lock_type& m,boost::system_time const& wait_until,predicate_type pred)

Modified: trunk/boost/thread/pthread/condition_variable_fwd.hpp
==============================================================================
--- trunk/boost/thread/pthread/condition_variable_fwd.hpp (original)
+++ trunk/boost/thread/pthread/condition_variable_fwd.hpp 2008-07-05 17:55:36 EDT (Sat, 05 Jul 2008)
@@ -47,6 +47,16 @@
         }
 
         bool timed_wait(unique_lock<mutex>& m,boost::system_time const& wait_until);
+ bool timed_wait(unique_lock<mutex>& m,xtime const& wait_until)
+ {
+ return timed_wait(m,system_time(wait_until));
+ }
+
+ template<typename duration_type>
+ bool timed_wait(unique_lock<mutex>& m,duration_type const& wait_duration)
+ {
+ return timed_wait(m,get_system_time()+wait_duration);
+ }
 
         template<typename predicate_type>
         bool timed_wait(unique_lock<mutex>& m,boost::system_time const& wait_until,predicate_type pred)

Modified: trunk/libs/thread/test/test_condition_timed_wait_times_out.cpp
==============================================================================
--- trunk/libs/thread/test/test_condition_timed_wait_times_out.cpp (original)
+++ trunk/libs/thread/test/test_condition_timed_wait_times_out.cpp 2008-07-05 17:55:36 EDT (Sat, 05 Jul 2008)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007 Anthony Williams
+// Copyright (C) 2007-8 Anthony Williams
 //
 // Distributed under the Boost Software License, Version 1.0. (See accompanying
 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -16,7 +16,7 @@
     return false;
 }
 
-unsigned const timeout_seconds=5;
+unsigned const timeout_seconds=2;
 unsigned const timeout_grace=1;
 boost::posix_time::milliseconds const timeout_resolution(100);
 
@@ -70,12 +70,96 @@
     BOOST_CHECK((delay-timeout_resolution)<=(end-start));
 }
 
+void do_test_timed_wait_relative_times_out()
+{
+ boost::condition_variable cond;
+ boost::mutex m;
+
+ boost::posix_time::seconds const delay(timeout_seconds);
+ boost::mutex::scoped_lock lock(m);
+ boost::system_time const start=boost::get_system_time();
+
+ while(cond.timed_wait(lock,delay));
+
+ boost::system_time const end=boost::get_system_time();
+ BOOST_CHECK((delay-timeout_resolution)<=(end-start));
+}
+
+void do_test_cv_any_timed_wait_times_out()
+{
+ boost::condition_variable_any cond;
+ boost::mutex m;
+
+ boost::posix_time::seconds const delay(timeout_seconds);
+ boost::mutex::scoped_lock lock(m);
+ boost::system_time const start=boost::get_system_time();
+ boost::system_time const timeout=start+delay;
+
+ while(cond.timed_wait(lock,timeout));
+
+ boost::system_time const end=boost::get_system_time();
+ BOOST_CHECK((delay-timeout_resolution)<=(end-start));
+}
+
+void do_test_cv_any_timed_wait_with_predicate_times_out()
+{
+ boost::condition_variable_any cond;
+ boost::mutex m;
+
+ boost::posix_time::seconds const delay(timeout_seconds);
+ boost::mutex::scoped_lock lock(m);
+ boost::system_time const start=boost::get_system_time();
+ boost::system_time const timeout=start+delay;
+
+ bool const res=cond.timed_wait(lock,timeout,fake_predicate);
+
+ boost::system_time const end=boost::get_system_time();
+ BOOST_CHECK(!res);
+ BOOST_CHECK((delay-timeout_resolution)<=(end-start));
+}
+
+void do_test_cv_any_relative_timed_wait_with_predicate_times_out()
+{
+ boost::condition_variable_any cond;
+ boost::mutex m;
+
+ boost::posix_time::seconds const delay(timeout_seconds);
+ boost::mutex::scoped_lock lock(m);
+ boost::system_time const start=boost::get_system_time();
+
+ bool const res=cond.timed_wait(lock,delay,fake_predicate);
+
+ boost::system_time const end=boost::get_system_time();
+ BOOST_CHECK(!res);
+ BOOST_CHECK((delay-timeout_resolution)<=(end-start));
+}
+
+void do_test_cv_any_timed_wait_relative_times_out()
+{
+ boost::condition_variable_any cond;
+ boost::mutex m;
+
+ boost::posix_time::seconds const delay(timeout_seconds);
+ boost::mutex::scoped_lock lock(m);
+ boost::system_time const start=boost::get_system_time();
+
+ while(cond.timed_wait(lock,delay));
+
+ boost::system_time const end=boost::get_system_time();
+ BOOST_CHECK((delay-timeout_resolution)<=(end-start));
+}
+
 
 void test_timed_wait_times_out()
 {
     timed_test(&do_test_timed_wait_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
     timed_test(&do_test_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
     timed_test(&do_test_relative_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
+ timed_test(&do_test_timed_wait_relative_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
+ timed_test(&do_test_cv_any_timed_wait_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
+ timed_test(&do_test_cv_any_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
+ timed_test(&do_test_cv_any_relative_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
+ timed_test(&do_test_cv_any_timed_wait_relative_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
 }
 
 boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk