|
Boost-Commit : |
From: anthony_at_[hidden]
Date: 2008-07-01 12:28:08
Author: anthonyw
Date: 2008-07-01 12:28:06 EDT (Tue, 01 Jul 2008)
New Revision: 46961
URL: http://svn.boost.org/trac/boost/changeset/46961
Log:
Added try_lock_upgrade to shared_mutex: second half of #1867 fix
Text files modified:
trunk/libs/thread/test/test_shared_mutex_part_2.cpp | 115 +++++++++++++++++++++++++++++++++++++++
1 files changed, 113 insertions(+), 2 deletions(-)
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-07-01 12:28:06 EDT (Tue, 01 Jul 2008)
@@ -15,10 +15,41 @@
BOOST_CHECK_EQUAL(value,expected_value); \
}
+class simple_upgrade_thread
+{
+ boost::shared_mutex& rwm;
+ boost::mutex& finish_mutex;
+ boost::mutex& unblocked_mutex;
+ unsigned& unblocked_count;
+
+ void operator=(simple_upgrade_thread&);
+
+public:
+ simple_upgrade_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::upgrade_lock<boost::shared_mutex> lk(rwm);
+
+ {
+ boost::mutex::scoped_lock ulk(unblocked_mutex);
+ ++unblocked_count;
+ }
+
+ boost::mutex::scoped_lock flk(finish_mutex);
+ }
+};
+
void test_only_one_upgrade_lock_permitted()
{
- unsigned const number_of_threads=100;
+ unsigned const number_of_threads=10;
boost::thread_group pool;
@@ -71,7 +102,7 @@
boost::mutex finish_mutex;
boost::mutex::scoped_lock finish_lock(finish_mutex);
- unsigned const reader_count=100;
+ unsigned const reader_count=10;
try
{
@@ -130,6 +161,29 @@
writer.join();
}
+void test_if_other_thread_has_write_lock_try_lock_upgrade_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_upgrade();
+ BOOST_CHECK(!try_succeeded);
+ if(try_succeeded)
+ {
+ rw_mutex.unlock_upgrade();
+ }
+
+ finish_lock.unlock();
+ writer.join();
+}
+
void test_if_no_thread_has_lock_try_lock_shared_returns_true()
{
boost::shared_mutex rw_mutex;
@@ -141,6 +195,17 @@
}
}
+void test_if_no_thread_has_lock_try_lock_upgrade_returns_true()
+{
+ boost::shared_mutex rw_mutex;
+ bool const try_succeeded=rw_mutex.try_lock_upgrade();
+ BOOST_CHECK(try_succeeded);
+ if(try_succeeded)
+ {
+ rw_mutex.unlock_upgrade();
+ }
+}
+
void test_if_other_thread_has_shared_lock_try_lock_shared_returns_true()
{
@@ -164,6 +229,52 @@
writer.join();
}
+void test_if_other_thread_has_shared_lock_try_lock_upgrade_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_upgrade();
+ BOOST_CHECK(try_succeeded);
+ if(try_succeeded)
+ {
+ rw_mutex.unlock_upgrade();
+ }
+
+ finish_lock.unlock();
+ writer.join();
+}
+
+void test_if_other_thread_has_upgrade_lock_try_lock_upgrade_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_upgrade_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_upgrade();
+ BOOST_CHECK(!try_succeeded);
+ if(try_succeeded)
+ {
+ rw_mutex.unlock_upgrade();
+ }
+
+ 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-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