Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2007-12-04 05:04:32


Author: anthonyw
Date: 2007-12-04 05:04:30 EST (Tue, 04 Dec 2007)
New Revision: 41682
URL: http://svn.boost.org/trac/boost/changeset/41682

Log:
split shared mutex tests in two to take less time
Added:
   trunk/libs/thread/test/shared_mutex_locking_thread.hpp (contents, props changed)
   trunk/libs/thread/test/test_shared_mutex_part_2.cpp (contents, props changed)
Text files modified:
   trunk/libs/thread/test/Jamfile.v2 | 1
   trunk/libs/thread/test/test_shared_mutex.cpp | 310 ---------------------------------------
   2 files changed, 2 insertions(+), 309 deletions(-)

Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2 (original)
+++ trunk/libs/thread/test/Jamfile.v2 2007-12-04 05:04:30 EST (Tue, 04 Dec 2007)
@@ -45,6 +45,7 @@
           [ thread-run test_xtime.cpp ]
           [ thread-run test_barrier.cpp ]
           [ thread-run test_shared_mutex.cpp ]
+ [ thread-run test_shared_mutex_part_2.cpp ]
           [ thread-run test_lock_concept.cpp ]
     ;
 }

Added: trunk/libs/thread/test/shared_mutex_locking_thread.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/shared_mutex_locking_thread.hpp 2007-12-04 05:04:30 EST (Tue, 04 Dec 2007)
@@ -0,0 +1,62 @@
+#ifndef SHARED_MUTEX_LOCKING_THREAD_HPP
+#define SHARED_MUTEX_LOCKING_THREAD_HPP
+
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/condition_variable.hpp>
+#include <boost/thread/shared_mutex.hpp>
+
+template<typename lock_type>
+class locking_thread
+{
+ boost::shared_mutex& rw_mutex;
+ unsigned& unblocked_count;
+ unsigned& simultaneous_running_count;
+ unsigned& max_simultaneous_running;
+ boost::mutex& unblocked_count_mutex;
+ boost::condition_variable& unblocked_condition;
+ boost::mutex& finish_mutex;
+public:
+ locking_thread(boost::shared_mutex& rw_mutex_,
+ unsigned& unblocked_count_,
+ boost::mutex& unblocked_count_mutex_,
+ boost::condition_variable& unblocked_condition_,
+ boost::mutex& finish_mutex_,
+ unsigned& simultaneous_running_count_,
+ unsigned& max_simultaneous_running_):
+ rw_mutex(rw_mutex_),
+ unblocked_count(unblocked_count_),
+ unblocked_condition(unblocked_condition_),
+ simultaneous_running_count(simultaneous_running_count_),
+ max_simultaneous_running(max_simultaneous_running_),
+ unblocked_count_mutex(unblocked_count_mutex_),
+ finish_mutex(finish_mutex_)
+ {}
+
+ void operator()()
+ {
+ // acquire lock
+ lock_type lock(rw_mutex);
+
+ // increment count to show we're unblocked
+ {
+ boost::mutex::scoped_lock ublock(unblocked_count_mutex);
+ ++unblocked_count;
+ unblocked_condition.notify_one();
+ ++simultaneous_running_count;
+ if(simultaneous_running_count>max_simultaneous_running)
+ {
+ max_simultaneous_running=simultaneous_running_count;
+ }
+ }
+
+ // wait to finish
+ boost::mutex::scoped_lock finish_lock(finish_mutex);
+ {
+ boost::mutex::scoped_lock ublock(unblocked_count_mutex);
+ --simultaneous_running_count;
+ }
+ }
+};
+
+
+#endif

Modified: trunk/libs/thread/test/test_shared_mutex.cpp
==============================================================================
--- trunk/libs/thread/test/test_shared_mutex.cpp (original)
+++ trunk/libs/thread/test/test_shared_mutex.cpp 2007-12-04 05:04:30 EST (Tue, 04 Dec 2007)
@@ -5,13 +5,9 @@
 
 #include <boost/test/unit_test.hpp>
 #include <boost/thread/thread.hpp>
-#include <boost/thread/mutex.hpp>
-#include <boost/thread/condition_variable.hpp>
-#include <boost/thread/shared_mutex.hpp>
 #include <boost/thread/xtime.hpp>
 #include "util.inl"
-#include <iostream>
-#include <boost/date_time/posix_time/posix_time_io.hpp>
+#include "shared_mutex_locking_thread.hpp"
 
 #define CHECK_LOCKED_VALUE_EQUAL(mutex_name,value,expected_value) \
     { \
@@ -19,65 +15,6 @@
         BOOST_CHECK_EQUAL(value,expected_value); \
     }
 
-
-namespace
-{
- template<typename lock_type>
- class locking_thread
- {
- boost::shared_mutex& rw_mutex;
- unsigned& unblocked_count;
- unsigned& simultaneous_running_count;
- unsigned& max_simultaneous_running;
- boost::mutex& unblocked_count_mutex;
- boost::condition_variable& unblocked_condition;
- boost::mutex& finish_mutex;
- public:
- locking_thread(boost::shared_mutex& rw_mutex_,
- unsigned& unblocked_count_,
- boost::mutex& unblocked_count_mutex_,
- boost::condition_variable& unblocked_condition_,
- boost::mutex& finish_mutex_,
- unsigned& simultaneous_running_count_,
- unsigned& max_simultaneous_running_):
- rw_mutex(rw_mutex_),
- unblocked_count(unblocked_count_),
- unblocked_condition(unblocked_condition_),
- simultaneous_running_count(simultaneous_running_count_),
- max_simultaneous_running(max_simultaneous_running_),
- unblocked_count_mutex(unblocked_count_mutex_),
- finish_mutex(finish_mutex_)
- {}
-
- void operator()()
- {
- // acquire lock
- lock_type lock(rw_mutex);
-
- // increment count to show we're unblocked
- {
- boost::mutex::scoped_lock ublock(unblocked_count_mutex);
- ++unblocked_count;
- unblocked_condition.notify_one();
- ++simultaneous_running_count;
- if(simultaneous_running_count>max_simultaneous_running)
- {
- max_simultaneous_running=simultaneous_running_count;
- }
- }
-
- // wait to finish
- boost::mutex::scoped_lock finish_lock(finish_mutex);
- {
- boost::mutex::scoped_lock ublock(unblocked_count_mutex);
- --simultaneous_running_count;
- }
- }
- };
-
-}
-
-
 void test_multiple_readers()
 {
     unsigned const number_of_threads=100;
@@ -328,250 +265,11 @@
         throw;
     }
 
-
     CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,reader_count+writer_count);
     CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,max_simultaneous_readers,reader_count);
     CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,max_simultaneous_writers,1u);
 }
 
-void test_only_one_upgrade_lock_permitted()
-{
- unsigned const number_of_threads=100;
-
- boost::thread_group pool;
-
- boost::shared_mutex rw_mutex;
- unsigned unblocked_count=0;
- unsigned simultaneous_running_count=0;
- unsigned max_simultaneous_running=0;
- boost::mutex unblocked_count_mutex;
- boost::condition_variable unblocked_condition;
- boost::mutex finish_mutex;
- boost::mutex::scoped_lock finish_lock(finish_mutex);
-
- try
- {
- for(unsigned i=0;i<number_of_threads;++i)
- {
- pool.create_thread(locking_thread<boost::upgrade_lock<boost::shared_mutex> >(rw_mutex,unblocked_count,unblocked_count_mutex,unblocked_condition,
- finish_mutex,simultaneous_running_count,max_simultaneous_running));
- }
-
- boost::thread::sleep(delay(1));
-
- CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,1U);
-
- finish_lock.unlock();
-
- pool.join_all();
- }
- catch(...)
- {
- pool.interrupt_all();
- pool.join_all();
- throw;
- }
-
- CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,number_of_threads);
- CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,max_simultaneous_running,1u);
-}
-
-void test_can_lock_upgrade_if_currently_locked_shared()
-{
- boost::thread_group pool;
-
- boost::shared_mutex rw_mutex;
- unsigned unblocked_count=0;
- unsigned simultaneous_running_count=0;
- unsigned max_simultaneous_running=0;
- boost::mutex unblocked_count_mutex;
- boost::condition_variable unblocked_condition;
- boost::mutex finish_mutex;
- boost::mutex::scoped_lock finish_lock(finish_mutex);
-
- unsigned const reader_count=100;
-
- try
- {
- for(unsigned i=0;i<reader_count;++i)
- {
- pool.create_thread(locking_thread<boost::shared_lock<boost::shared_mutex> >(rw_mutex,unblocked_count,unblocked_count_mutex,unblocked_condition,
- finish_mutex,simultaneous_running_count,max_simultaneous_running));
- }
- boost::thread::sleep(delay(1));
- pool.create_thread(locking_thread<boost::upgrade_lock<boost::shared_mutex> >(rw_mutex,unblocked_count,unblocked_count_mutex,unblocked_condition,
- finish_mutex,simultaneous_running_count,max_simultaneous_running));
- {
- boost::mutex::scoped_lock lk(unblocked_count_mutex);
- while(unblocked_count<(reader_count+1))
- {
- unblocked_condition.wait(lk);
- }
- }
- CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,reader_count+1);
-
- finish_lock.unlock();
- pool.join_all();
- }
- catch(...)
- {
- pool.interrupt_all();
- pool.join_all();
- throw;
- }
-
-
- CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,reader_count+1);
- 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;
-
- 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()
-{
-
- 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::this_thread::sleep(boost::posix_time::seconds(1));
- CHECK_LOCKED_VALUE_EQUAL(unblocked_mutex,unblocked_count,1u);
-
- bool const try_succeeded=rw_mutex.try_lock_shared();
- BOOST_CHECK(!try_succeeded);
- if(try_succeeded)
- {
- rw_mutex.unlock_shared();
- }
-
- finish_lock.unlock();
- writer.join();
-}
-
-void test_if_no_thread_has_lock_try_lock_shared_returns_true()
-{
- boost::shared_mutex rw_mutex;
- bool const try_succeeded=rw_mutex.try_lock_shared();
- BOOST_CHECK(try_succeeded);
- if(try_succeeded)
- {
- rw_mutex.unlock_shared();
- }
-}
-
-namespace
-{
- class simple_reading_thread
- {
- boost::shared_mutex& rwm;
- boost::mutex& finish_mutex;
- boost::mutex& unblocked_mutex;
- unsigned& unblocked_count;
-
- public:
- simple_reading_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::shared_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_shared_lock_try_lock_shared_returns_true()
-{
-
- 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_reading_thread(rw_mutex,finish_mutex,unblocked_mutex,unblocked_count));
- boost::thread::sleep(delay(1));
- CHECK_LOCKED_VALUE_EQUAL(unblocked_mutex,unblocked_count,1u);
-
- bool const try_succeeded=rw_mutex.try_lock_shared();
- BOOST_CHECK(try_succeeded);
- if(try_succeeded)
- {
- rw_mutex.unlock_shared();
- }
-
- finish_lock.unlock();
- 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);
- bool const timed_lock_succeeded=rw_mutex.timed_lock_shared(timeout);
- BOOST_CHECK(in_range(boost::get_xtime(timeout),1));
- 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 =
@@ -582,12 +280,6 @@
     test->add(BOOST_TEST_CASE(&test_reader_blocks_writer));
     test->add(BOOST_TEST_CASE(&test_unlocking_writer_unblocks_all_readers));
     test->add(BOOST_TEST_CASE(&test_unlocking_last_reader_only_unblocks_one_writer));
- test->add(BOOST_TEST_CASE(&test_only_one_upgrade_lock_permitted));
- test->add(BOOST_TEST_CASE(&test_can_lock_upgrade_if_currently_locked_shared));
- 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_part_2.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_shared_mutex_part_2.cpp 2007-12-04 05:04:30 EST (Tue, 04 Dec 2007)
@@ -0,0 +1,270 @@
+// (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_only_one_upgrade_lock_permitted()
+{
+ unsigned const number_of_threads=100;
+
+ boost::thread_group pool;
+
+ boost::shared_mutex rw_mutex;
+ unsigned unblocked_count=0;
+ unsigned simultaneous_running_count=0;
+ unsigned max_simultaneous_running=0;
+ boost::mutex unblocked_count_mutex;
+ boost::condition_variable unblocked_condition;
+ boost::mutex finish_mutex;
+ boost::mutex::scoped_lock finish_lock(finish_mutex);
+
+ try
+ {
+ for(unsigned i=0;i<number_of_threads;++i)
+ {
+ pool.create_thread(locking_thread<boost::upgrade_lock<boost::shared_mutex> >(rw_mutex,unblocked_count,unblocked_count_mutex,unblocked_condition,
+ finish_mutex,simultaneous_running_count,max_simultaneous_running));
+ }
+
+ boost::thread::sleep(delay(1));
+
+ CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,1U);
+
+ finish_lock.unlock();
+
+ pool.join_all();
+ }
+ catch(...)
+ {
+ pool.interrupt_all();
+ pool.join_all();
+ throw;
+ }
+
+ CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,number_of_threads);
+ CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,max_simultaneous_running,1u);
+}
+
+void test_can_lock_upgrade_if_currently_locked_shared()
+{
+ boost::thread_group pool;
+
+ boost::shared_mutex rw_mutex;
+ unsigned unblocked_count=0;
+ unsigned simultaneous_running_count=0;
+ unsigned max_simultaneous_running=0;
+ boost::mutex unblocked_count_mutex;
+ boost::condition_variable unblocked_condition;
+ boost::mutex finish_mutex;
+ boost::mutex::scoped_lock finish_lock(finish_mutex);
+
+ unsigned const reader_count=100;
+
+ try
+ {
+ for(unsigned i=0;i<reader_count;++i)
+ {
+ pool.create_thread(locking_thread<boost::shared_lock<boost::shared_mutex> >(rw_mutex,unblocked_count,unblocked_count_mutex,unblocked_condition,
+ finish_mutex,simultaneous_running_count,max_simultaneous_running));
+ }
+ boost::thread::sleep(delay(1));
+ pool.create_thread(locking_thread<boost::upgrade_lock<boost::shared_mutex> >(rw_mutex,unblocked_count,unblocked_count_mutex,unblocked_condition,
+ finish_mutex,simultaneous_running_count,max_simultaneous_running));
+ {
+ boost::mutex::scoped_lock lk(unblocked_count_mutex);
+ while(unblocked_count<(reader_count+1))
+ {
+ unblocked_condition.wait(lk);
+ }
+ }
+ CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,reader_count+1);
+
+ finish_lock.unlock();
+ pool.join_all();
+ }
+ catch(...)
+ {
+ pool.interrupt_all();
+ pool.join_all();
+ throw;
+ }
+
+
+ CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,reader_count+1);
+ 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;
+
+ 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()
+{
+
+ 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::this_thread::sleep(boost::posix_time::seconds(1));
+ CHECK_LOCKED_VALUE_EQUAL(unblocked_mutex,unblocked_count,1u);
+
+ bool const try_succeeded=rw_mutex.try_lock_shared();
+ BOOST_CHECK(!try_succeeded);
+ if(try_succeeded)
+ {
+ rw_mutex.unlock_shared();
+ }
+
+ finish_lock.unlock();
+ writer.join();
+}
+
+void test_if_no_thread_has_lock_try_lock_shared_returns_true()
+{
+ boost::shared_mutex rw_mutex;
+ bool const try_succeeded=rw_mutex.try_lock_shared();
+ BOOST_CHECK(try_succeeded);
+ if(try_succeeded)
+ {
+ rw_mutex.unlock_shared();
+ }
+}
+
+namespace
+{
+ class simple_reading_thread
+ {
+ boost::shared_mutex& rwm;
+ boost::mutex& finish_mutex;
+ boost::mutex& unblocked_mutex;
+ unsigned& unblocked_count;
+
+ public:
+ simple_reading_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::shared_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_shared_lock_try_lock_shared_returns_true()
+{
+
+ 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_reading_thread(rw_mutex,finish_mutex,unblocked_mutex,unblocked_count));
+ boost::thread::sleep(delay(1));
+ CHECK_LOCKED_VALUE_EQUAL(unblocked_mutex,unblocked_count,1u);
+
+ bool const try_succeeded=rw_mutex.try_lock_shared();
+ BOOST_CHECK(try_succeeded);
+ if(try_succeeded)
+ {
+ rw_mutex.unlock_shared();
+ }
+
+ finish_lock.unlock();
+ 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);
+ bool const timed_lock_succeeded=rw_mutex.timed_lock_shared(timeout);
+ BOOST_CHECK(in_range(boost::get_xtime(timeout),1));
+ 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 =
+ BOOST_TEST_SUITE("Boost.Threads: shared_mutex test suite");
+
+ test->add(BOOST_TEST_CASE(&test_only_one_upgrade_lock_permitted));
+ test->add(BOOST_TEST_CASE(&test_can_lock_upgrade_if_currently_locked_shared));
+ 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;
+}


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