mirror of
https://github.com/boostorg/thread.git
synced 2026-01-24 18:32:32 +00:00
https://svn.boost.org/svn/boost/trunk ........ r41077 | troyer | 2007-11-13 23:00:02 -0800 (Tue, 13 Nov 2007) | 1 line Added support for std::complex to serialization ........ r41078 | joaquin | 2007-11-13 23:09:29 -0800 (Tue, 13 Nov 2007) | 1 line added missing include ........ r41079 | djowel | 2007-11-14 02:13:51 -0800 (Wed, 14 Nov 2007) | 1 line adjust to compensate for lack of <small> tag. ........ r41080 | djowel | 2007-11-14 02:17:09 -0800 (Wed, 14 Nov 2007) | 1 line refresh docs ........ r41081 | djowel | 2007-11-14 02:23:23 -0800 (Wed, 14 Nov 2007) | 1 line refresh docs ........ r41082 | djowel | 2007-11-14 02:24:21 -0800 (Wed, 14 Nov 2007) | 1 line refresh docs ........ r41083 | anthonyw | 2007-11-14 03:08:09 -0800 (Wed, 14 Nov 2007) | 1 line ignore and join all threads in group on exception ........ r41084 | anthonyw | 2007-11-14 03:56:53 -0800 (Wed, 14 Nov 2007) | 1 line interrupt and join all threads in a group if an exception is thrown during a test ........ r41086 | johnmaddock | 2007-11-14 04:14:19 -0800 (Wed, 14 Nov 2007) | 1 line Merged changes from the Sandbox to Trunk. ........ r41087 | anthonyw | 2007-11-14 04:17:41 -0800 (Wed, 14 Nov 2007) | 1 line interrupt and join all threads in a group if an exception is thrown during a test ........ r41088 | johnmaddock | 2007-11-14 04:41:32 -0800 (Wed, 14 Nov 2007) | 1 line Added WinCE workaround. ........ r41089 | johnmaddock | 2007-11-14 05:03:15 -0800 (Wed, 14 Nov 2007) | 1 line Catch exceptions thrown from thread creation. ........ r41090 | anthonyw | 2007-11-14 06:49:58 -0800 (Wed, 14 Nov 2007) | 1 line fixes for pthread implementation ........ r41091 | bemandawes | 2007-11-14 07:50:43 -0800 (Wed, 14 Nov 2007) | 1 line Replace old docs with excerpts from http://article.gmane.org/gmane.comp.lib.boost.testing/5020 ........ r41093 | eric_niebler | 2007-11-14 08:36:15 -0800 (Wed, 14 Nov 2007) | 1 line fix support for segmented iteration ........ r41094 | dave | 2007-11-14 09:37:55 -0800 (Wed, 14 Nov 2007) | 3 lines Update command-prompt instructions per http://news.gmane.org/find-root.php?message_id=%3cfhcrem%24c4%241%40ger.gmane.org%3e ........ r41095 | bemandawes | 2007-11-14 12:05:30 -0800 (Wed, 14 Nov 2007) | 1 line Bring docs more in line with reality:-) ........ r41097 | hljin | 2007-11-14 18:07:53 -0800 (Wed, 14 Nov 2007) | 1 line GIL: removed the example directory from the documentation as it is a duplication and re-directed the Doxygen documentation to a local copy (to be submitted next) ........ r41098 | hljin | 2007-11-14 18:30:13 -0800 (Wed, 14 Nov 2007) | 1 line GIL: added a local copy of the Doxygen documentation ........ r41099 | hljin | 2007-11-14 18:32:04 -0800 (Wed, 14 Nov 2007) | 1 line GIL: added a local copy of the Doxygen documentation (second batch) ........ r41100 | chris_kohlhoff | 2007-11-14 22:19:04 -0800 (Wed, 14 Nov 2007) | 6 lines Try to fix an order-of-initialisation problem with error_category references. The symptom, which only occurs in some applications, is a crash due to a dereference of a null pointer. The exact conditions under which the problem occurs are not fully understood, so this fix is probably more paranoid than necessary. ........ r41102 | jhunold | 2007-11-14 23:04:24 -0800 (Wed, 14 Nov 2007) | 1 line Suppress msvc "deprecated" warnings. ........ r41103 | johnmaddock | 2007-11-15 01:52:26 -0800 (Thu, 15 Nov 2007) | 1 line Fix some compiler warnings when building with the Intel and aCC compilers. ........ r41104 | garcia | 2007-11-15 04:50:48 -0800 (Thu, 15 Nov 2007) | 2 lines John Torjo is reviewing the X-files. ........ r41105 | hkaiser | 2007-11-15 05:27:24 -0800 (Thu, 15 Nov 2007) | 1 line Suppressed more warnings during build of Wave. ........ r41106 | bemandawes | 2007-11-15 06:36:40 -0800 (Thu, 15 Nov 2007) | 1 line GetUserDefaultUILanguage isn't present for Cygwin, so disable use ........ r41107 | bemandawes | 2007-11-15 07:20:27 -0800 (Thu, 15 Nov 2007) | 1 line Get rid of .cvsignore files ........ r41108 | eric_niebler | 2007-11-15 08:06:22 -0800 (Thu, 15 Nov 2007) | 1 line fix typo ........ r41110 | grafik | 2007-11-15 08:58:21 -0800 (Thu, 15 Nov 2007) | 1 line Fix building of multiple regression tools at once. ........ r41111 | gmelquio | 2007-11-15 09:52:31 -0800 (Thu, 15 Nov 2007) | 1 line Fixed documentation typo. ........ r41113 | grafik | 2007-11-15 10:56:43 -0800 (Thu, 15 Nov 2007) | 1 line Switch intel-win32 to use static multi thread runtime since the single thread static runtime is no longer available. (fixes #1287) ........ r41114 | andreas_huber69 | 2007-11-15 11:22:12 -0800 (Thu, 15 Nov 2007) | 1 line Added new FAQ item suggested by Chris Paulse. ........ r41116 | guwi17 | 2007-11-15 12:52:37 -0800 (Thu, 15 Nov 2007) | 4 lines This patch now lets the Sun compiler work for most of uBLAS. 'test4' is now failing. Too avoid other compilers complaining about missing return values in some functions where exceptions are thrown, code was added so an arbitatry were chosen. ........ r41117 | danmarsden | 2007-11-15 13:15:47 -0800 (Thu, 15 Nov 2007) | 1 line removing failing test on EDG compilers ........ r41118 | grafik | 2007-11-15 14:05:15 -0800 (Thu, 15 Nov 2007) | 1 line Remove redundant reference to system library. It causes double linking when used indirectly through filesystem. Which can lead to double linking incompatible variants when a specific filesystem variant is requested. ........ r41119 | grafik | 2007-11-15 14:05:22 -0800 (Thu, 15 Nov 2007) | 1 line Oops, forgot we need to use the root boost project settings when building. ........ [SVN r41141]
594 lines
20 KiB
C++
594 lines
20 KiB
C++
// (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/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>
|
|
|
|
#define CHECK_LOCKED_VALUE_EQUAL(mutex_name,value,expected_value) \
|
|
{ \
|
|
boost::mutex::scoped_lock lock(mutex_name); \
|
|
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;
|
|
|
|
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::shared_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<number_of_threads)
|
|
{
|
|
unblocked_condition.wait(lk);
|
|
}
|
|
}
|
|
|
|
CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,number_of_threads);
|
|
|
|
finish_lock.unlock();
|
|
|
|
pool.join_all();
|
|
}
|
|
catch(...)
|
|
{
|
|
pool.interrupt_all();
|
|
pool.join_all();
|
|
throw;
|
|
}
|
|
|
|
CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,max_simultaneous_running,number_of_threads);
|
|
}
|
|
|
|
void test_only_one_writer_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::unique_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(2));
|
|
|
|
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_reader_blocks_writer()
|
|
{
|
|
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
|
|
{
|
|
|
|
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::mutex::scoped_lock lk(unblocked_count_mutex);
|
|
while(unblocked_count<1)
|
|
{
|
|
unblocked_condition.wait(lk);
|
|
}
|
|
}
|
|
CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,1U);
|
|
pool.create_thread(locking_thread<boost::unique_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,2U);
|
|
CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,max_simultaneous_running,1u);
|
|
}
|
|
|
|
void test_unlocking_writer_unblocks_all_readers()
|
|
{
|
|
boost::thread_group pool;
|
|
|
|
boost::shared_mutex rw_mutex;
|
|
boost::unique_lock<boost::shared_mutex> write_lock(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));
|
|
CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,0U);
|
|
|
|
write_lock.unlock();
|
|
|
|
{
|
|
boost::mutex::scoped_lock lk(unblocked_count_mutex);
|
|
while(unblocked_count<reader_count)
|
|
{
|
|
unblocked_condition.wait(lk);
|
|
}
|
|
}
|
|
|
|
CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,reader_count);
|
|
|
|
finish_lock.unlock();
|
|
pool.join_all();
|
|
}
|
|
catch(...)
|
|
{
|
|
pool.interrupt_all();
|
|
pool.join_all();
|
|
throw;
|
|
}
|
|
|
|
CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,max_simultaneous_running,reader_count);
|
|
}
|
|
|
|
void test_unlocking_last_reader_only_unblocks_one_writer()
|
|
{
|
|
boost::thread_group pool;
|
|
|
|
boost::shared_mutex rw_mutex;
|
|
unsigned unblocked_count=0;
|
|
unsigned simultaneous_running_readers=0;
|
|
unsigned max_simultaneous_readers=0;
|
|
unsigned simultaneous_running_writers=0;
|
|
unsigned max_simultaneous_writers=0;
|
|
boost::mutex unblocked_count_mutex;
|
|
boost::condition_variable unblocked_condition;
|
|
boost::mutex finish_reading_mutex;
|
|
boost::mutex::scoped_lock finish_reading_lock(finish_reading_mutex);
|
|
boost::mutex finish_writing_mutex;
|
|
boost::mutex::scoped_lock finish_writing_lock(finish_writing_mutex);
|
|
|
|
unsigned const reader_count=100;
|
|
unsigned const writer_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_reading_mutex,simultaneous_running_readers,max_simultaneous_readers));
|
|
}
|
|
boost::thread::sleep(delay(1));
|
|
for(unsigned i=0;i<writer_count;++i)
|
|
{
|
|
pool.create_thread(locking_thread<boost::unique_lock<boost::shared_mutex> >(rw_mutex,unblocked_count,unblocked_count_mutex,unblocked_condition,
|
|
finish_writing_mutex,simultaneous_running_writers,max_simultaneous_writers));
|
|
}
|
|
{
|
|
boost::mutex::scoped_lock lk(unblocked_count_mutex);
|
|
while(unblocked_count<reader_count)
|
|
{
|
|
unblocked_condition.wait(lk);
|
|
}
|
|
}
|
|
boost::thread::sleep(delay(1));
|
|
CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,unblocked_count,reader_count);
|
|
|
|
finish_reading_lock.unlock();
|
|
|
|
{
|
|
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_writing_lock.unlock();
|
|
pool.join_all();
|
|
}
|
|
catch(...)
|
|
{
|
|
pool.interrupt_all();
|
|
pool.join_all();
|
|
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::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_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_multiple_readers));
|
|
test->add(BOOST_TEST_CASE(&test_only_one_writer_permitted));
|
|
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;
|
|
}
|