mirror of
https://github.com/boostorg/thread.git
synced 2026-01-24 06:22:12 +00:00
https://svn.boost.org/svn/boost/trunk ........ r41489 | djowel | 2007-11-30 02:07:41 -0800 (Fri, 30 Nov 2007) | 1 line fixed link ........ r41493 | grafik | 2007-11-30 07:56:42 -0800 (Fri, 30 Nov 2007) | 1 line Mode proposal.pdf to new web site. (fixes #1364) ........ r41494 | grafik | 2007-11-30 07:58:59 -0800 (Fri, 30 Nov 2007) | 1 line Obsolete. (fixes #1362) ........ r41497 | grafik | 2007-11-30 08:21:49 -0800 (Fri, 30 Nov 2007) | 1 line Move the content of old more/links.html doc to new web site. (fixes #1360). ........ r41500 | bemandawes | 2007-11-30 08:44:23 -0800 (Fri, 30 Nov 2007) | 1 line Refresh examples, add example build script, reflect that in docs. Apply suggestions from Darren Cook. ........ r41501 | grafik | 2007-11-30 09:13:02 -0800 (Fri, 30 Nov 2007) | 1 line Obsolete. (fixes #1365) ........ r41502 | grafik | 2007-11-30 09:16:00 -0800 (Fri, 30 Nov 2007) | 1 line Obsolete. (fixes #1348) ........ r41504 | grafik | 2007-11-30 09:45:24 -0800 (Fri, 30 Nov 2007) | 1 line Obsolete. (fixes #1345) ........ r41505 | anthonyw | 2007-11-30 10:38:21 -0800 (Fri, 30 Nov 2007) | 1 line interruptible_wait (and hence condition timed_wait) now uses a WaitableTimer where possible, to be robust in the face of clock changes ........ r41506 | danieljames | 2007-11-30 11:28:46 -0800 (Fri, 30 Nov 2007) | 2 lines Frank Mori Hess's patch to get the documentation building. ........ r41511 | danieljames | 2007-11-30 12:06:44 -0800 (Fri, 30 Nov 2007) | 3 lines Remove the formal review schedule, as it has been moved to the new site. Fixes #1351 ........ r41519 | djowel | 2007-11-30 17:11:44 -0800 (Fri, 30 Nov 2007) | 1 line remove old unused file ........ r41521 | dave | 2007-11-30 18:15:17 -0800 (Fri, 30 Nov 2007) | 7 lines Boost.Python: * Workarounds for many SunCC 5.9 bugs * Suppression of many SunCC 5.9 warnings * Improve the style of some test invocations in Jamfile ........ r41526 | dave | 2007-11-30 21:07:13 -0800 (Fri, 30 Nov 2007) | 2 lines Fixed grammar in error message ........ r41529 | niels_dekker | 2007-12-01 04:14:37 -0800 (Sat, 01 Dec 2007) | 1 line Added value_init tests, based upon GCC bug report by Jonathan Wakely. Added URL to Borland bug report. ........ r41530 | niels_dekker | 2007-12-01 05:57:06 -0800 (Sat, 01 Dec 2007) | 1 line Marked value_init_test failures on GCC as "expected failures", referring to ticket #1491 ........ r41532 | vladimir_prus | 2007-12-01 06:56:23 -0800 (Sat, 01 Dec 2007) | 2 lines Change the detection of 64-bit windows to handle EM64T processors. ........ r41534 | danieljames | 2007-12-01 08:44:20 -0800 (Sat, 01 Dec 2007) | 4 lines Transfer the rest of the version history to the new website. Fixes #1374. ........ r41535 | grafik | 2007-12-01 08:44:54 -0800 (Sat, 01 Dec 2007) | 1 line Plug memory leak when closing out actions. Thanks to Martin Kortmann for finding this. ........ r41538 | vladimir_prus | 2007-12-01 09:56:47 -0800 (Sat, 01 Dec 2007) | 2 lines Document 64-bit compilation. ........ r41539 | danieljames | 2007-12-01 09:58:44 -0800 (Sat, 01 Dec 2007) | 2 lines Mark hash_long_double_test as failing on all PA-RISC compilers. ........ r41540 | igaztanaga | 2007-12-01 10:01:15 -0800 (Sat, 01 Dec 2007) | 1 line Simplified mutexes for systems with no _POSIX_TIMEOUTS. ........ r41542 | vladimir_prus | 2007-12-01 11:07:22 -0800 (Sat, 01 Dec 2007) | 4 lines Add <testing.arg> functionality to unit-test rule. Patch from Mark Desnoyer. ........ r41544 | jhunold | 2007-12-01 11:27:06 -0800 (Sat, 01 Dec 2007) | 2 lines Silence compiler by adding cosmetic virtual destructors. ........ r41547 | vladimir_prus | 2007-12-01 12:06:43 -0800 (Sat, 01 Dec 2007) | 1 line Document runtime-link ........ r41548 | grafik | 2007-12-01 12:17:52 -0800 (Sat, 01 Dec 2007) | 1 line Add support for detection and building with vc9. (fixes #1490) ........ r41549 | jhunold | 2007-12-01 12:24:51 -0800 (Sat, 01 Dec 2007) | 2 lines Silence compiler by adding cosmetic virtual destructors. ........ r41550 | jhunold | 2007-12-01 12:26:37 -0800 (Sat, 01 Dec 2007) | 3 lines Remove unused paramters. Add -Wextra to gcc flags to enable more warnings. ........ r41552 | grafik | 2007-12-01 12:40:56 -0800 (Sat, 01 Dec 2007) | 1 line Remove borders from simple list tables. ........ r41553 | grafik | 2007-12-01 12:42:27 -0800 (Sat, 01 Dec 2007) | 1 line Remove borders from simple list tables. ........ r41554 | t_schwinger | 2007-12-01 12:52:00 -0800 (Sat, 01 Dec 2007) | 3 lines Using central stylesheet now. ........ r41555 | vladimir_prus | 2007-12-01 13:26:09 -0800 (Sat, 01 Dec 2007) | 1 line Document STLport ........ r41556 | grafik | 2007-12-01 13:53:47 -0800 (Sat, 01 Dec 2007) | 1 line Document various "new" features, and add in the history till now for 3.1.16. (fixes #1445 #1447 #1448) ........ r41557 | vladimir_prus | 2007-12-01 14:31:04 -0800 (Sat, 01 Dec 2007) | 1 line Remove unused method ........ r41562 | grafik | 2007-12-01 17:59:02 -0800 (Sat, 01 Dec 2007) | 1 line No-op no-empty action. ........ r41563 | grafik | 2007-12-01 18:03:48 -0800 (Sat, 01 Dec 2007) | 1 line ';' can't be a no-op on nix. ........ r41565 | grafik | 2007-12-01 20:06:28 -0800 (Sat, 01 Dec 2007) | 1 line Remove outdated release procedures, and move content to wiki. Content moved to <http://svn.boost.org/trac/boost/wiki/ReleasePractices/Procedures> and <http://svn.boost.org/trac/boost/wiki/ReleasePractices/ManagerCheckList>. (fixes #1366 #1367) ........ r41566 | grafik | 2007-12-01 20:22:34 -0800 (Sat, 01 Dec 2007) | 1 line Remove obsolete images. (fixes #1257) ........ r41568 | grafik | 2007-12-01 22:56:38 -0800 (Sat, 01 Dec 2007) | 1 line "Who's Using Boost?" content moved to new web site. ........ r41569 | grafik | 2007-12-01 23:02:12 -0800 (Sat, 01 Dec 2007) | 1 line "Who's Using Boost?" content moved to new web site. ........ r41571 | igaztanaga | 2007-12-02 01:25:53 -0800 (Sun, 02 Dec 2007) | 1 line Added missing #include <typeinfo> overwritten by previous commit ........ r41572 | danieljames | 2007-12-02 01:59:15 -0800 (Sun, 02 Dec 2007) | 1 line Add some parameters to the standalone hash build. ........ r41574 | danieljames | 2007-12-02 02:23:58 -0800 (Sun, 02 Dec 2007) | 2 lines Initialise svnmerge. ........ r41575 | danieljames | 2007-12-02 02:25:22 -0800 (Sun, 02 Dec 2007) | 1 line Fix the navbar links. ........ r41577 | jhunold | 2007-12-02 03:51:08 -0800 (Sun, 02 Dec 2007) | 3 lines Revert revisions 41544 and 41549. See http://lists.boost.org/Archives/boost/2007/12/131116.php for details. ........ r41580 | danieljames | 2007-12-02 05:47:31 -0800 (Sun, 02 Dec 2007) | 2 lines Revert a change I mean to make on the fix-links branch. ........ r41582 | danieljames | 2007-12-02 06:15:25 -0800 (Sun, 02 Dec 2007) | 5 lines Remove formal_review_process, it looks like I forgot to when I updated the version in the new site. Refs #1350. ........ r41587 | grafik | 2007-12-02 09:03:43 -0800 (Sun, 02 Dec 2007) | 1 line Obsolete. ........ r41588 | hkaiser | 2007-12-02 09:18:54 -0800 (Sun, 02 Dec 2007) | 1 line Applied patch supplied by Jens Seidel. Fixed #1410. ........ r41590 | hkaiser | 2007-12-02 11:08:13 -0800 (Sun, 02 Dec 2007) | 1 line Wave: More fixes to allow error free compilation of every header on its own. ........ r41592 | grafik | 2007-12-02 12:15:25 -0800 (Sun, 02 Dec 2007) | 1 line Support building of universal binaries using architecture options. Thanks to Mat Marcus. (fixes #552 #1342 #989) ........ r41593 | niels_dekker | 2007-12-02 14:10:45 -0800 (Sun, 02 Dec 2007) | 1 line Added missing GCC version (4.0) to expected GCC failures of value_init_test ........ [SVN r41596]
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::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_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;
|
|
}
|