Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2008-04-10 10:07:40


Author: anthonyw
Date: 2008-04-10 10:07:39 EDT (Thu, 10 Apr 2008)
New Revision: 44149
URL: http://svn.boost.org/trac/boost/changeset/44149

Log:
added overloads of timed_lock_shared with a relative timeout to shared_mutex
Added:
   trunk/libs/thread/test/test_shared_mutex_timed_locks.cpp (contents, props changed)
Text files modified:
   trunk/boost/thread/pthread/shared_mutex.hpp | 18 +++++++++++
   trunk/boost/thread/win32/shared_mutex.hpp | 12 +++++++
   trunk/libs/thread/test/Jamfile.v2 | 1
   trunk/libs/thread/test/shared_mutex_locking_thread.hpp | 31 ++++++++++++++++++++
   trunk/libs/thread/test/test_shared_mutex_part_2.cpp | 62 ----------------------------------------
   5 files changed, 62 insertions(+), 62 deletions(-)

Modified: trunk/boost/thread/pthread/shared_mutex.hpp
==============================================================================
--- trunk/boost/thread/pthread/shared_mutex.hpp (original)
+++ trunk/boost/thread/pthread/shared_mutex.hpp 2008-04-10 10:07:39 EDT (Thu, 10 Apr 2008)
@@ -118,6 +118,12 @@
             }
         }
 
+ template<typename TimeDuration>
+ bool timed_lock_shared(TimeDuration const & relative_time)
+ {
+ return timed_lock_shared(get_system_time()+relative_time);
+ }
+
         void unlock_shared()
         {
             boost::mutex::scoped_lock lock(state_change);
@@ -196,6 +202,12 @@
             }
         }
 
+ template<typename TimeDuration>
+ bool timed_lock(TimeDuration const & relative_time)
+ {
+ return timed_lock(get_system_time()+relative_time);
+ }
+
         bool try_lock()
         {
             boost::mutex::scoped_lock lock(state_change);
@@ -271,6 +283,12 @@
             }
         }
 
+ template<typename TimeDuration>
+ bool timed_lock_upgrade(TimeDuration const & relative_time)
+ {
+ return timed_lock(get_system_time()+relative_time);
+ }
+
         bool try_lock_upgrade()
         {
             boost::mutex::scoped_lock lock(state_change);

Modified: trunk/boost/thread/win32/shared_mutex.hpp
==============================================================================
--- trunk/boost/thread/win32/shared_mutex.hpp (original)
+++ trunk/boost/thread/win32/shared_mutex.hpp 2008-04-10 10:07:39 EDT (Thu, 10 Apr 2008)
@@ -120,6 +120,12 @@
             BOOST_VERIFY(timed_lock_shared(::boost::detail::get_system_time_sentinel()));
         }
 
+ template<typename TimeDuration>
+ bool timed_lock_shared(TimeDuration const & relative_time)
+ {
+ return timed_lock_shared(get_system_time()+relative_time);
+ }
+
         bool timed_lock_shared(boost::system_time const& wait_until)
         {
 #ifdef BOOST_MSVC
@@ -269,6 +275,12 @@
             BOOST_VERIFY(timed_lock(::boost::detail::get_system_time_sentinel()));
         }
 
+ template<typename TimeDuration>
+ bool timed_lock(TimeDuration const & relative_time)
+ {
+ return timed_lock(get_system_time()+relative_time);
+ }
+
         bool timed_lock(boost::system_time const& wait_until)
         {
 #ifdef BOOST_MSVC

Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2 (original)
+++ trunk/libs/thread/test/Jamfile.v2 2008-04-10 10:07:39 EDT (Thu, 10 Apr 2008)
@@ -51,6 +51,7 @@
           [ thread-run test_barrier.cpp ]
           [ thread-run test_shared_mutex.cpp ]
           [ thread-run test_shared_mutex_part_2.cpp ]
+ [ thread-run test_shared_mutex_timed_locks.cpp ]
           [ thread-run test_lock_concept.cpp ]
     ;
 }

Modified: trunk/libs/thread/test/shared_mutex_locking_thread.hpp
==============================================================================
--- trunk/libs/thread/test/shared_mutex_locking_thread.hpp (original)
+++ trunk/libs/thread/test/shared_mutex_locking_thread.hpp 2008-04-10 10:07:39 EDT (Thu, 10 Apr 2008)
@@ -66,5 +66,36 @@
     void operator=(locking_thread&);
 };
 
+class simple_writing_thread
+{
+ boost::shared_mutex& rwm;
+ boost::mutex& finish_mutex;
+ boost::mutex& unblocked_mutex;
+ unsigned& unblocked_count;
+
+ void operator=(simple_writing_thread&);
+
+public:
+ simple_writing_thread(boost::shared_mutex& rwm_,
+ boost::mutex& finish_mutex_,
+ boost::mutex& unblocked_mutex_,
+ unsigned& unblocked_count_):
+ rwm(rwm_),finish_mutex(finish_mutex_),
+ unblocked_mutex(unblocked_mutex_),unblocked_count(unblocked_count_)
+ {}
+
+ void operator()()
+ {
+ boost::unique_lock<boost::shared_mutex> lk(rwm);
+
+ {
+ boost::mutex::scoped_lock ulk(unblocked_mutex);
+ ++unblocked_count;
+ }
+
+ boost::mutex::scoped_lock flk(finish_mutex);
+ }
+};
+
 
 #endif

Modified: trunk/libs/thread/test/test_shared_mutex_part_2.cpp
==============================================================================
--- trunk/libs/thread/test/test_shared_mutex_part_2.cpp (original)
+++ trunk/libs/thread/test/test_shared_mutex_part_2.cpp 2008-04-10 10:07:39 EDT (Thu, 10 Apr 2008)
@@ -107,40 +107,6 @@
     CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,max_simultaneous_running,reader_count+1);
 }
 
-namespace
-{
- class simple_writing_thread
- {
- boost::shared_mutex& rwm;
- boost::mutex& finish_mutex;
- boost::mutex& unblocked_mutex;
- unsigned& unblocked_count;
-
- void operator=(simple_writing_thread&);
-
- public:
- simple_writing_thread(boost::shared_mutex& rwm_,
- boost::mutex& finish_mutex_,
- boost::mutex& unblocked_mutex_,
- unsigned& unblocked_count_):
- rwm(rwm_),finish_mutex(finish_mutex_),
- unblocked_mutex(unblocked_mutex_),unblocked_count(unblocked_count_)
- {}
-
- void operator()()
- {
- boost::unique_lock<boost::shared_mutex> lk(rwm);
-
- {
- boost::mutex::scoped_lock ulk(unblocked_mutex);
- ++unblocked_count;
- }
-
- boost::mutex::scoped_lock flk(finish_mutex);
- }
- };
-}
-
 void test_if_other_thread_has_write_lock_try_lock_shared_returns_false()
 {
 
@@ -232,33 +198,6 @@
     writer.join();
 }
 
-void test_timed_lock_shared_times_out_if_write_lock_held()
-{
- boost::shared_mutex rw_mutex;
- boost::mutex finish_mutex;
- boost::mutex unblocked_mutex;
- unsigned unblocked_count=0;
- boost::mutex::scoped_lock finish_lock(finish_mutex);
- boost::thread writer(simple_writing_thread(rw_mutex,finish_mutex,unblocked_mutex,unblocked_count));
- boost::thread::sleep(delay(1));
- CHECK_LOCKED_VALUE_EQUAL(unblocked_mutex,unblocked_count,1u);
-
- boost::system_time const start=boost::get_system_time();
- boost::system_time const timeout=start+boost::posix_time::milliseconds(2000);
- boost::posix_time::milliseconds const timeout_resolution(20);
- bool const timed_lock_succeeded=rw_mutex.timed_lock_shared(timeout);
- BOOST_CHECK((timeout-timeout_resolution)<boost::get_system_time());
- BOOST_CHECK(!timed_lock_succeeded);
- if(timed_lock_succeeded)
- {
- rw_mutex.unlock_shared();
- }
-
- finish_lock.unlock();
- writer.join();
-}
-
-
 boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
 {
     boost::unit_test_framework::test_suite* test =
@@ -269,7 +208,6 @@
     test->add(BOOST_TEST_CASE(&test_if_other_thread_has_write_lock_try_lock_shared_returns_false));
     test->add(BOOST_TEST_CASE(&test_if_no_thread_has_lock_try_lock_shared_returns_true));
     test->add(BOOST_TEST_CASE(&test_if_other_thread_has_shared_lock_try_lock_shared_returns_true));
- test->add(BOOST_TEST_CASE(&test_timed_lock_shared_times_out_if_write_lock_held));
 
     return test;
 }

Added: trunk/libs/thread/test/test_shared_mutex_timed_locks.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_shared_mutex_timed_locks.cpp 2008-04-10 10:07:39 EDT (Thu, 10 Apr 2008)
@@ -0,0 +1,94 @@
+// (C) Copyright 2006-7 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)
+
+#include <boost/test/unit_test.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/thread/xtime.hpp>
+#include "util.inl"
+#include "shared_mutex_locking_thread.hpp"
+
+#define CHECK_LOCKED_VALUE_EQUAL(mutex_name,value,expected_value) \
+ { \
+ boost::mutex::scoped_lock lock(mutex_name); \
+ BOOST_CHECK_EQUAL(value,expected_value); \
+ }
+
+
+void test_timed_lock_shared_times_out_if_write_lock_held()
+{
+ boost::shared_mutex rw_mutex;
+ boost::mutex finish_mutex;
+ boost::mutex unblocked_mutex;
+ unsigned unblocked_count=0;
+ boost::mutex::scoped_lock finish_lock(finish_mutex);
+ boost::thread writer(simple_writing_thread(rw_mutex,finish_mutex,unblocked_mutex,unblocked_count));
+ boost::thread::sleep(delay(1));
+ CHECK_LOCKED_VALUE_EQUAL(unblocked_mutex,unblocked_count,1u);
+
+ boost::system_time const start=boost::get_system_time();
+ boost::system_time const timeout=start+boost::posix_time::milliseconds(500);
+ boost::posix_time::milliseconds const timeout_resolution(50);
+ bool timed_lock_succeeded=rw_mutex.timed_lock_shared(timeout);
+ BOOST_CHECK((timeout-timeout_resolution)<boost::get_system_time());
+ BOOST_CHECK(!timed_lock_succeeded);
+ if(timed_lock_succeeded)
+ {
+ rw_mutex.unlock_shared();
+ }
+
+ boost::posix_time::milliseconds const wait_duration(500);
+ boost::system_time const timeout2=boost::get_system_time()+wait_duration;
+ timed_lock_succeeded=rw_mutex.timed_lock_shared(wait_duration);
+ BOOST_CHECK((timeout2-timeout_resolution)<boost::get_system_time());
+ BOOST_CHECK(!timed_lock_succeeded);
+ if(timed_lock_succeeded)
+ {
+ rw_mutex.unlock_shared();
+ }
+
+ finish_lock.unlock();
+ writer.join();
+}
+
+void test_timed_lock_shared_succeeds_if_no_lock_held()
+{
+ boost::shared_mutex rw_mutex;
+ boost::mutex finish_mutex;
+ boost::mutex unblocked_mutex;
+
+ boost::system_time const start=boost::get_system_time();
+ boost::system_time const timeout=start+boost::posix_time::milliseconds(500);
+ boost::posix_time::milliseconds const timeout_resolution(50);
+ bool timed_lock_succeeded=rw_mutex.timed_lock_shared(timeout);
+ BOOST_CHECK(boost::get_system_time()<timeout);
+ BOOST_CHECK(timed_lock_succeeded);
+ if(timed_lock_succeeded)
+ {
+ rw_mutex.unlock_shared();
+ }
+
+ boost::posix_time::milliseconds const wait_duration(500);
+ boost::system_time const timeout2=boost::get_system_time()+wait_duration;
+ timed_lock_succeeded=rw_mutex.timed_lock_shared(wait_duration);
+ BOOST_CHECK(boost::get_system_time()<timeout2);
+ BOOST_CHECK(timed_lock_succeeded);
+ if(timed_lock_succeeded)
+ {
+ rw_mutex.unlock_shared();
+ }
+
+}
+
+
+boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
+{
+ boost::unit_test_framework::test_suite* test =
+ BOOST_TEST_SUITE("Boost.Threads: shared_mutex test suite");
+
+ test->add(BOOST_TEST_CASE(&test_timed_lock_shared_times_out_if_write_lock_held));
+ test->add(BOOST_TEST_CASE(&test_timed_lock_shared_succeeds_if_no_lock_held));
+
+ return test;
+}


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