mirror of
https://github.com/boostorg/thread.git
synced 2026-02-18 02:22:10 +00:00
merged from trunk
[SVN r47983]
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2007 Anthony Williams
|
||||
// Copyright (C) 2007-8 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)
|
||||
@@ -16,7 +16,7 @@ bool fake_predicate()
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned const timeout_seconds=5;
|
||||
unsigned const timeout_seconds=2;
|
||||
unsigned const timeout_grace=1;
|
||||
boost::posix_time::milliseconds const timeout_resolution(100);
|
||||
|
||||
@@ -70,12 +70,96 @@ void do_test_relative_timed_wait_with_predicate_times_out()
|
||||
BOOST_CHECK((delay-timeout_resolution)<=(end-start));
|
||||
}
|
||||
|
||||
void do_test_timed_wait_relative_times_out()
|
||||
{
|
||||
boost::condition_variable cond;
|
||||
boost::mutex m;
|
||||
|
||||
boost::posix_time::seconds const delay(timeout_seconds);
|
||||
boost::mutex::scoped_lock lock(m);
|
||||
boost::system_time const start=boost::get_system_time();
|
||||
|
||||
while(cond.timed_wait(lock,delay));
|
||||
|
||||
boost::system_time const end=boost::get_system_time();
|
||||
BOOST_CHECK((delay-timeout_resolution)<=(end-start));
|
||||
}
|
||||
|
||||
void do_test_cv_any_timed_wait_times_out()
|
||||
{
|
||||
boost::condition_variable_any cond;
|
||||
boost::mutex m;
|
||||
|
||||
boost::posix_time::seconds const delay(timeout_seconds);
|
||||
boost::mutex::scoped_lock lock(m);
|
||||
boost::system_time const start=boost::get_system_time();
|
||||
boost::system_time const timeout=start+delay;
|
||||
|
||||
while(cond.timed_wait(lock,timeout));
|
||||
|
||||
boost::system_time const end=boost::get_system_time();
|
||||
BOOST_CHECK((delay-timeout_resolution)<=(end-start));
|
||||
}
|
||||
|
||||
void do_test_cv_any_timed_wait_with_predicate_times_out()
|
||||
{
|
||||
boost::condition_variable_any cond;
|
||||
boost::mutex m;
|
||||
|
||||
boost::posix_time::seconds const delay(timeout_seconds);
|
||||
boost::mutex::scoped_lock lock(m);
|
||||
boost::system_time const start=boost::get_system_time();
|
||||
boost::system_time const timeout=start+delay;
|
||||
|
||||
bool const res=cond.timed_wait(lock,timeout,fake_predicate);
|
||||
|
||||
boost::system_time const end=boost::get_system_time();
|
||||
BOOST_CHECK(!res);
|
||||
BOOST_CHECK((delay-timeout_resolution)<=(end-start));
|
||||
}
|
||||
|
||||
void do_test_cv_any_relative_timed_wait_with_predicate_times_out()
|
||||
{
|
||||
boost::condition_variable_any cond;
|
||||
boost::mutex m;
|
||||
|
||||
boost::posix_time::seconds const delay(timeout_seconds);
|
||||
boost::mutex::scoped_lock lock(m);
|
||||
boost::system_time const start=boost::get_system_time();
|
||||
|
||||
bool const res=cond.timed_wait(lock,delay,fake_predicate);
|
||||
|
||||
boost::system_time const end=boost::get_system_time();
|
||||
BOOST_CHECK(!res);
|
||||
BOOST_CHECK((delay-timeout_resolution)<=(end-start));
|
||||
}
|
||||
|
||||
void do_test_cv_any_timed_wait_relative_times_out()
|
||||
{
|
||||
boost::condition_variable_any cond;
|
||||
boost::mutex m;
|
||||
|
||||
boost::posix_time::seconds const delay(timeout_seconds);
|
||||
boost::mutex::scoped_lock lock(m);
|
||||
boost::system_time const start=boost::get_system_time();
|
||||
|
||||
while(cond.timed_wait(lock,delay));
|
||||
|
||||
boost::system_time const end=boost::get_system_time();
|
||||
BOOST_CHECK((delay-timeout_resolution)<=(end-start));
|
||||
}
|
||||
|
||||
|
||||
void test_timed_wait_times_out()
|
||||
{
|
||||
timed_test(&do_test_timed_wait_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
|
||||
timed_test(&do_test_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
|
||||
timed_test(&do_test_relative_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
|
||||
timed_test(&do_test_timed_wait_relative_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
|
||||
timed_test(&do_test_cv_any_timed_wait_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
|
||||
timed_test(&do_test_cv_any_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
|
||||
timed_test(&do_test_cv_any_relative_timed_wait_with_predicate_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
|
||||
timed_test(&do_test_cv_any_timed_wait_relative_times_out, timeout_seconds+timeout_grace, execution_monitor::use_mutex);
|
||||
}
|
||||
|
||||
boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
|
||||
|
||||
@@ -272,6 +272,16 @@ struct dummy_mutex
|
||||
}
|
||||
};
|
||||
|
||||
namespace boost
|
||||
{
|
||||
template<>
|
||||
struct is_mutex_type<dummy_mutex>
|
||||
{
|
||||
BOOST_STATIC_CONSTANT(bool, value = true);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
void test_lock_five_in_range()
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <boost/test/test_case_template.hpp>
|
||||
#include <boost/mpl/vector.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
@@ -82,6 +83,122 @@ struct test_initially_unlocked_if_other_thread_has_lock
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Mutex,typename Lock>
|
||||
struct test_initially_unlocked_with_try_lock_if_other_thread_has_unique_lock
|
||||
{
|
||||
Mutex m;
|
||||
boost::mutex done_mutex;
|
||||
bool done;
|
||||
bool locked;
|
||||
boost::condition_variable done_cond;
|
||||
|
||||
test_initially_unlocked_with_try_lock_if_other_thread_has_unique_lock():
|
||||
done(false),locked(false)
|
||||
{}
|
||||
|
||||
void locking_thread()
|
||||
{
|
||||
Lock lock(m,boost::try_to_lock);
|
||||
|
||||
boost::lock_guard<boost::mutex> lk(done_mutex);
|
||||
locked=lock.owns_lock();
|
||||
done=true;
|
||||
done_cond.notify_one();
|
||||
}
|
||||
|
||||
bool is_done() const
|
||||
{
|
||||
return done;
|
||||
}
|
||||
|
||||
|
||||
void operator()()
|
||||
{
|
||||
boost::unique_lock<Mutex> lock(m);
|
||||
|
||||
typedef test_initially_unlocked_with_try_lock_if_other_thread_has_unique_lock<Mutex,Lock> this_type;
|
||||
|
||||
boost::thread t(&this_type::locking_thread,this);
|
||||
|
||||
try
|
||||
{
|
||||
{
|
||||
boost::mutex::scoped_lock lk(done_mutex);
|
||||
BOOST_CHECK(done_cond.timed_wait(lk,boost::posix_time::seconds(2),
|
||||
boost::bind(&this_type::is_done,this)));
|
||||
BOOST_CHECK(!locked);
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
t.join();
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
lock.unlock();
|
||||
t.join();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Mutex,typename Lock>
|
||||
struct test_initially_locked_if_other_thread_has_shared_lock
|
||||
{
|
||||
Mutex m;
|
||||
boost::mutex done_mutex;
|
||||
bool done;
|
||||
bool locked;
|
||||
boost::condition_variable done_cond;
|
||||
|
||||
test_initially_locked_if_other_thread_has_shared_lock():
|
||||
done(false),locked(false)
|
||||
{}
|
||||
|
||||
void locking_thread()
|
||||
{
|
||||
Lock lock(m);
|
||||
|
||||
boost::lock_guard<boost::mutex> lk(done_mutex);
|
||||
locked=lock.owns_lock();
|
||||
done=true;
|
||||
done_cond.notify_one();
|
||||
}
|
||||
|
||||
bool is_done() const
|
||||
{
|
||||
return done;
|
||||
}
|
||||
|
||||
|
||||
void operator()()
|
||||
{
|
||||
boost::shared_lock<Mutex> lock(m);
|
||||
|
||||
typedef test_initially_locked_if_other_thread_has_shared_lock<Mutex,Lock> this_type;
|
||||
|
||||
boost::thread t(&this_type::locking_thread,this);
|
||||
|
||||
try
|
||||
{
|
||||
{
|
||||
boost::mutex::scoped_lock lk(done_mutex);
|
||||
BOOST_CHECK(done_cond.timed_wait(lk,boost::posix_time::seconds(2),
|
||||
boost::bind(&this_type::is_done,this)));
|
||||
BOOST_CHECK(locked);
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
t.join();
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
lock.unlock();
|
||||
t.join();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Mutex,typename Lock>
|
||||
struct test_initially_unlocked_with_defer_lock_parameter
|
||||
{
|
||||
@@ -149,6 +266,64 @@ struct test_locked_after_try_lock_called
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Mutex,typename Lock>
|
||||
struct test_unlocked_after_try_lock_if_other_thread_has_lock
|
||||
{
|
||||
Mutex m;
|
||||
boost::mutex done_mutex;
|
||||
bool done;
|
||||
bool locked;
|
||||
boost::condition_variable done_cond;
|
||||
|
||||
test_unlocked_after_try_lock_if_other_thread_has_lock():
|
||||
done(false),locked(false)
|
||||
{}
|
||||
|
||||
void locking_thread()
|
||||
{
|
||||
Lock lock(m,boost::defer_lock);
|
||||
|
||||
boost::lock_guard<boost::mutex> lk(done_mutex);
|
||||
locked=lock.owns_lock();
|
||||
done=true;
|
||||
done_cond.notify_one();
|
||||
}
|
||||
|
||||
bool is_done() const
|
||||
{
|
||||
return done;
|
||||
}
|
||||
|
||||
|
||||
void operator()()
|
||||
{
|
||||
Lock lock(m);
|
||||
|
||||
typedef test_unlocked_after_try_lock_if_other_thread_has_lock<Mutex,Lock> this_type;
|
||||
|
||||
boost::thread t(&this_type::locking_thread,this);
|
||||
|
||||
try
|
||||
{
|
||||
{
|
||||
boost::mutex::scoped_lock lk(done_mutex);
|
||||
BOOST_CHECK(done_cond.timed_wait(lk,boost::posix_time::seconds(2),
|
||||
boost::bind(&this_type::is_done,this)));
|
||||
BOOST_CHECK(!locked);
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
t.join();
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
lock.unlock();
|
||||
t.join();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Mutex,typename Lock>
|
||||
struct test_throws_if_lock_called_when_already_locked
|
||||
{
|
||||
@@ -204,6 +379,8 @@ struct test_locks_can_be_swapped
|
||||
{
|
||||
Mutex m1;
|
||||
Mutex m2;
|
||||
Mutex m3;
|
||||
|
||||
Lock l1(m1);
|
||||
Lock l2(m2);
|
||||
|
||||
@@ -219,16 +396,16 @@ struct test_locks_can_be_swapped
|
||||
|
||||
BOOST_CHECK_EQUAL(l1.mutex(),&m1);
|
||||
BOOST_CHECK_EQUAL(l2.mutex(),&m2);
|
||||
|
||||
|
||||
l1.swap(Lock(m3));
|
||||
|
||||
BOOST_CHECK_EQUAL(l1.mutex(),&m3);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
BOOST_TEST_CASE_TEMPLATE_FUNCTION(test_scoped_lock_concept,Mutex)
|
||||
template<typename Mutex,typename Lock>
|
||||
void test_lock_is_scoped_lock_concept_for_mutex()
|
||||
{
|
||||
typedef typename Mutex::scoped_lock Lock;
|
||||
|
||||
test_default_constructed_has_no_mutex_and_unlocked<Lock>()();
|
||||
test_initially_locked<Mutex,Lock>()();
|
||||
test_initially_unlocked_with_defer_lock_parameter<Mutex,Lock>()();
|
||||
@@ -238,6 +415,24 @@ BOOST_TEST_CASE_TEMPLATE_FUNCTION(test_scoped_lock_concept,Mutex)
|
||||
test_throws_if_lock_called_when_already_locked<Mutex,Lock>()();
|
||||
test_throws_if_unlock_called_when_already_unlocked<Mutex,Lock>()();
|
||||
test_locks_can_be_swapped<Mutex,Lock>()();
|
||||
test_locked_after_try_lock_called<Mutex,Lock>()();
|
||||
test_throws_if_try_lock_called_when_already_locked<Mutex,Lock>()();
|
||||
test_unlocked_after_try_lock_if_other_thread_has_lock<Mutex,Lock>()();
|
||||
}
|
||||
|
||||
|
||||
BOOST_TEST_CASE_TEMPLATE_FUNCTION(test_scoped_lock_concept,Mutex)
|
||||
{
|
||||
typedef typename Mutex::scoped_lock Lock;
|
||||
|
||||
test_lock_is_scoped_lock_concept_for_mutex<Mutex,Lock>();
|
||||
}
|
||||
|
||||
BOOST_TEST_CASE_TEMPLATE_FUNCTION(test_unique_lock_is_scoped_lock,Mutex)
|
||||
{
|
||||
typedef boost::unique_lock<Mutex> Lock;
|
||||
|
||||
test_lock_is_scoped_lock_concept_for_mutex<Mutex,Lock>();
|
||||
}
|
||||
|
||||
BOOST_TEST_CASE_TEMPLATE_FUNCTION(test_scoped_try_lock_concept,Mutex)
|
||||
@@ -252,26 +447,125 @@ BOOST_TEST_CASE_TEMPLATE_FUNCTION(test_scoped_try_lock_concept,Mutex)
|
||||
test_unlocked_after_unlock_called<Mutex,Lock>()();
|
||||
test_locked_after_lock_called<Mutex,Lock>()();
|
||||
test_locked_after_try_lock_called<Mutex,Lock>()();
|
||||
test_unlocked_after_try_lock_if_other_thread_has_lock<Mutex,Lock>()();
|
||||
test_throws_if_lock_called_when_already_locked<Mutex,Lock>()();
|
||||
test_throws_if_try_lock_called_when_already_locked<Mutex,Lock>()();
|
||||
test_throws_if_unlock_called_when_already_unlocked<Mutex,Lock>()();
|
||||
test_locks_can_be_swapped<Mutex,Lock>()();
|
||||
}
|
||||
|
||||
struct dummy_shared_mutex
|
||||
{
|
||||
bool locked;
|
||||
bool shared_locked;
|
||||
bool shared_unlocked;
|
||||
bool shared_timed_locked_relative;
|
||||
bool shared_timed_locked_absolute;
|
||||
bool timed_locked_relative;
|
||||
bool timed_locked_absolute;
|
||||
|
||||
dummy_shared_mutex():
|
||||
locked(false),shared_locked(false),shared_unlocked(false),
|
||||
shared_timed_locked_relative(false),
|
||||
shared_timed_locked_absolute(false),
|
||||
timed_locked_relative(false),
|
||||
timed_locked_absolute(false)
|
||||
{}
|
||||
|
||||
void lock()
|
||||
{
|
||||
locked=true;
|
||||
}
|
||||
|
||||
void lock_shared()
|
||||
{
|
||||
shared_locked=true;
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{}
|
||||
|
||||
void unlock_shared()
|
||||
{
|
||||
shared_unlocked=true;
|
||||
}
|
||||
|
||||
bool timed_lock_shared(boost::system_time)
|
||||
{
|
||||
shared_timed_locked_absolute=true;
|
||||
return false;
|
||||
}
|
||||
template<typename Duration>
|
||||
bool timed_lock_shared(Duration)
|
||||
{
|
||||
shared_timed_locked_relative=true;
|
||||
return false;
|
||||
}
|
||||
bool timed_lock(boost::system_time)
|
||||
{
|
||||
timed_locked_absolute=true;
|
||||
return false;
|
||||
}
|
||||
template<typename Duration>
|
||||
bool timed_lock(Duration)
|
||||
{
|
||||
timed_locked_relative=true;
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
void test_shared_lock()
|
||||
{
|
||||
typedef boost::shared_mutex Mutex;
|
||||
typedef boost::shared_lock<Mutex> Lock;
|
||||
|
||||
test_default_constructed_has_no_mutex_and_unlocked<Lock>()();
|
||||
test_initially_locked<Mutex,Lock>()();
|
||||
test_initially_unlocked_with_try_lock_if_other_thread_has_unique_lock<Mutex,Lock>()();
|
||||
test_initially_locked_if_other_thread_has_shared_lock<Mutex,Lock>()();
|
||||
test_initially_unlocked_with_defer_lock_parameter<Mutex,Lock>()();
|
||||
test_initially_locked_with_adopt_lock_parameter<Mutex,Lock>()();
|
||||
test_unlocked_after_unlock_called<Mutex,Lock>()();
|
||||
test_locked_after_lock_called<Mutex,Lock>()();
|
||||
test_locked_after_try_lock_called<Mutex,Lock>()();
|
||||
test_throws_if_lock_called_when_already_locked<Mutex,Lock>()();
|
||||
test_throws_if_try_lock_called_when_already_locked<Mutex,Lock>()();
|
||||
test_throws_if_unlock_called_when_already_unlocked<Mutex,Lock>()();
|
||||
test_locks_can_be_swapped<Mutex,Lock>()();
|
||||
|
||||
dummy_shared_mutex dummy;
|
||||
boost::shared_lock<dummy_shared_mutex> lk(dummy);
|
||||
BOOST_CHECK(dummy.shared_locked);
|
||||
lk.unlock();
|
||||
BOOST_CHECK(dummy.shared_unlocked);
|
||||
lk.timed_lock(boost::posix_time::milliseconds(5));
|
||||
BOOST_CHECK(dummy.shared_timed_locked_relative);
|
||||
lk.timed_lock(boost::get_system_time());
|
||||
BOOST_CHECK(dummy.shared_timed_locked_absolute);
|
||||
}
|
||||
|
||||
boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
|
||||
{
|
||||
boost::unit_test_framework::test_suite* test =
|
||||
BOOST_TEST_SUITE("Boost.Threads: lock concept test suite");
|
||||
|
||||
typedef boost::mpl::vector<boost::mutex,boost::try_mutex,boost::timed_mutex,
|
||||
boost::recursive_mutex,boost::recursive_try_mutex,boost::recursive_timed_mutex> mutex_types;
|
||||
boost::recursive_mutex,boost::recursive_try_mutex,boost::recursive_timed_mutex> mutex_types_with_scoped_lock;
|
||||
|
||||
test->add(BOOST_TEST_CASE_TEMPLATE(test_scoped_lock_concept,mutex_types));
|
||||
test->add(BOOST_TEST_CASE_TEMPLATE(test_scoped_lock_concept,mutex_types_with_scoped_lock));
|
||||
|
||||
typedef boost::mpl::vector<boost::try_mutex,boost::timed_mutex,
|
||||
boost::recursive_try_mutex,boost::recursive_timed_mutex> try_mutex_types;
|
||||
boost::recursive_try_mutex,boost::recursive_timed_mutex> mutex_types_with_scoped_try_lock;
|
||||
|
||||
test->add(BOOST_TEST_CASE_TEMPLATE(test_scoped_try_lock_concept,try_mutex_types));
|
||||
test->add(BOOST_TEST_CASE_TEMPLATE(test_scoped_try_lock_concept,mutex_types_with_scoped_try_lock));
|
||||
|
||||
typedef boost::mpl::vector<boost::mutex,boost::try_mutex,boost::timed_mutex,
|
||||
boost::recursive_mutex,boost::recursive_try_mutex,boost::recursive_timed_mutex,boost::shared_mutex> all_mutex_types;
|
||||
|
||||
test->add(BOOST_TEST_CASE_TEMPLATE(test_unique_lock_is_scoped_lock,all_mutex_types));
|
||||
test->add(BOOST_TEST_CASE(&test_shared_lock));
|
||||
|
||||
return test;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
void test_multiple_readers()
|
||||
{
|
||||
unsigned const number_of_threads=100;
|
||||
unsigned const number_of_threads=10;
|
||||
|
||||
boost::thread_group pool;
|
||||
|
||||
@@ -64,7 +64,7 @@ void test_multiple_readers()
|
||||
|
||||
void test_only_one_writer_permitted()
|
||||
{
|
||||
unsigned const number_of_threads=100;
|
||||
unsigned const number_of_threads=10;
|
||||
|
||||
boost::thread_group pool;
|
||||
|
||||
@@ -164,7 +164,7 @@ void test_unlocking_writer_unblocks_all_readers()
|
||||
boost::mutex finish_mutex;
|
||||
boost::mutex::scoped_lock finish_lock(finish_mutex);
|
||||
|
||||
unsigned const reader_count=100;
|
||||
unsigned const reader_count=10;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -218,8 +218,8 @@ void test_unlocking_last_reader_only_unblocks_one_writer()
|
||||
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;
|
||||
unsigned const reader_count=10;
|
||||
unsigned const writer_count=10;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -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 @@ void test_can_lock_upgrade_if_currently_locked_shared()
|
||||
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 @@ void test_if_other_thread_has_write_lock_try_lock_shared_returns_false()
|
||||
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_shared_returns_true()
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ void test_if_other_thread_has_shared_lock_try_lock_shared_returns_true()
|
||||
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 =
|
||||
|
||||
@@ -45,92 +45,90 @@ struct X
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace boost;
|
||||
|
||||
X x;
|
||||
|
||||
// 0
|
||||
|
||||
thread( &X::f0, &x ).join();
|
||||
thread( &X::f0, ref(x) ).join();
|
||||
boost::thread( &X::f0, &x ).join();
|
||||
boost::thread( &X::f0, boost::ref(x) ).join();
|
||||
|
||||
thread( &X::g0, &x ).join();
|
||||
thread( &X::g0, x ).join();
|
||||
thread( &X::g0, ref(x) ).join();
|
||||
boost::thread( &X::g0, &x ).join();
|
||||
boost::thread( &X::g0, x ).join();
|
||||
boost::thread( &X::g0, boost::ref(x) ).join();
|
||||
|
||||
// 1
|
||||
|
||||
thread( &X::f1, &x, 1 ).join();
|
||||
thread( &X::f1, ref(x), 1 ).join();
|
||||
boost::thread( &X::f1, &x, 1 ).join();
|
||||
boost::thread( &X::f1, boost::ref(x), 1 ).join();
|
||||
|
||||
thread( &X::g1, &x, 1 ).join();
|
||||
thread( &X::g1, x, 1 ).join();
|
||||
thread( &X::g1, ref(x), 1 ).join();
|
||||
boost::thread( &X::g1, &x, 1 ).join();
|
||||
boost::thread( &X::g1, x, 1 ).join();
|
||||
boost::thread( &X::g1, boost::ref(x), 1 ).join();
|
||||
|
||||
// 2
|
||||
|
||||
thread( &X::f2, &x, 1, 2 ).join();
|
||||
thread( &X::f2, ref(x), 1, 2 ).join();
|
||||
boost::thread( &X::f2, &x, 1, 2 ).join();
|
||||
boost::thread( &X::f2, boost::ref(x), 1, 2 ).join();
|
||||
|
||||
thread( &X::g2, &x, 1, 2 ).join();
|
||||
thread( &X::g2, x, 1, 2 ).join();
|
||||
thread( &X::g2, ref(x), 1, 2 ).join();
|
||||
boost::thread( &X::g2, &x, 1, 2 ).join();
|
||||
boost::thread( &X::g2, x, 1, 2 ).join();
|
||||
boost::thread( &X::g2, boost::ref(x), 1, 2 ).join();
|
||||
|
||||
// 3
|
||||
|
||||
thread( &X::f3, &x, 1, 2, 3 ).join();
|
||||
thread( &X::f3, ref(x), 1, 2, 3 ).join();
|
||||
boost::thread( &X::f3, &x, 1, 2, 3 ).join();
|
||||
boost::thread( &X::f3, boost::ref(x), 1, 2, 3 ).join();
|
||||
|
||||
thread( &X::g3, &x, 1, 2, 3 ).join();
|
||||
thread( &X::g3, x, 1, 2, 3 ).join();
|
||||
thread( &X::g3, ref(x), 1, 2, 3 ).join();
|
||||
boost::thread( &X::g3, &x, 1, 2, 3 ).join();
|
||||
boost::thread( &X::g3, x, 1, 2, 3 ).join();
|
||||
boost::thread( &X::g3, boost::ref(x), 1, 2, 3 ).join();
|
||||
|
||||
// 4
|
||||
|
||||
thread( &X::f4, &x, 1, 2, 3, 4 ).join();
|
||||
thread( &X::f4, ref(x), 1, 2, 3, 4 ).join();
|
||||
boost::thread( &X::f4, &x, 1, 2, 3, 4 ).join();
|
||||
boost::thread( &X::f4, boost::ref(x), 1, 2, 3, 4 ).join();
|
||||
|
||||
thread( &X::g4, &x, 1, 2, 3, 4 ).join();
|
||||
thread( &X::g4, x, 1, 2, 3, 4 ).join();
|
||||
thread( &X::g4, ref(x), 1, 2, 3, 4 ).join();
|
||||
boost::thread( &X::g4, &x, 1, 2, 3, 4 ).join();
|
||||
boost::thread( &X::g4, x, 1, 2, 3, 4 ).join();
|
||||
boost::thread( &X::g4, boost::ref(x), 1, 2, 3, 4 ).join();
|
||||
|
||||
// 5
|
||||
|
||||
thread( &X::f5, &x, 1, 2, 3, 4, 5 ).join();
|
||||
thread( &X::f5, ref(x), 1, 2, 3, 4, 5 ).join();
|
||||
boost::thread( &X::f5, &x, 1, 2, 3, 4, 5 ).join();
|
||||
boost::thread( &X::f5, boost::ref(x), 1, 2, 3, 4, 5 ).join();
|
||||
|
||||
thread( &X::g5, &x, 1, 2, 3, 4, 5 ).join();
|
||||
thread( &X::g5, x, 1, 2, 3, 4, 5 ).join();
|
||||
thread( &X::g5, ref(x), 1, 2, 3, 4, 5 ).join();
|
||||
boost::thread( &X::g5, &x, 1, 2, 3, 4, 5 ).join();
|
||||
boost::thread( &X::g5, x, 1, 2, 3, 4, 5 ).join();
|
||||
boost::thread( &X::g5, boost::ref(x), 1, 2, 3, 4, 5 ).join();
|
||||
|
||||
// 6
|
||||
|
||||
thread( &X::f6, &x, 1, 2, 3, 4, 5, 6 ).join();
|
||||
thread( &X::f6, ref(x), 1, 2, 3, 4, 5, 6 ).join();
|
||||
boost::thread( &X::f6, &x, 1, 2, 3, 4, 5, 6 ).join();
|
||||
boost::thread( &X::f6, boost::ref(x), 1, 2, 3, 4, 5, 6 ).join();
|
||||
|
||||
thread( &X::g6, &x, 1, 2, 3, 4, 5, 6 ).join();
|
||||
thread( &X::g6, x, 1, 2, 3, 4, 5, 6 ).join();
|
||||
thread( &X::g6, ref(x), 1, 2, 3, 4, 5, 6 ).join();
|
||||
boost::thread( &X::g6, &x, 1, 2, 3, 4, 5, 6 ).join();
|
||||
boost::thread( &X::g6, x, 1, 2, 3, 4, 5, 6 ).join();
|
||||
boost::thread( &X::g6, boost::ref(x), 1, 2, 3, 4, 5, 6 ).join();
|
||||
|
||||
// 7
|
||||
|
||||
thread( &X::f7, &x, 1, 2, 3, 4, 5, 6, 7).join();
|
||||
thread( &X::f7, ref(x), 1, 2, 3, 4, 5, 6, 7).join();
|
||||
boost::thread( &X::f7, &x, 1, 2, 3, 4, 5, 6, 7).join();
|
||||
boost::thread( &X::f7, boost::ref(x), 1, 2, 3, 4, 5, 6, 7).join();
|
||||
|
||||
thread( &X::g7, &x, 1, 2, 3, 4, 5, 6, 7).join();
|
||||
thread( &X::g7, x, 1, 2, 3, 4, 5, 6, 7).join();
|
||||
thread( &X::g7, ref(x), 1, 2, 3, 4, 5, 6, 7).join();
|
||||
boost::thread( &X::g7, &x, 1, 2, 3, 4, 5, 6, 7).join();
|
||||
boost::thread( &X::g7, x, 1, 2, 3, 4, 5, 6, 7).join();
|
||||
boost::thread( &X::g7, boost::ref(x), 1, 2, 3, 4, 5, 6, 7).join();
|
||||
|
||||
// 8
|
||||
|
||||
thread( &X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
thread( &X::f8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
boost::thread( &X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
boost::thread( &X::f8, boost::ref(x), 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
|
||||
thread( &X::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
thread( &X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
thread( &X::g8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
boost::thread( &X::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
boost::thread( &X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
boost::thread( &X::g8, boost::ref(x), 1, 2, 3, 4, 5, 6, 7, 8 ).join();
|
||||
|
||||
BOOST_TEST( x.hash == 23558 );
|
||||
|
||||
return report_errors();
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
// Copyright (C) 2008 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)
|
||||
@@ -9,6 +10,8 @@
|
||||
#include <boost/thread/xtime.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/condition.hpp>
|
||||
|
||||
void test_xtime_cmp()
|
||||
{
|
||||
@@ -53,6 +56,45 @@ void test_xtime_get()
|
||||
}
|
||||
}
|
||||
|
||||
void test_xtime_mutex_backwards_compatibility()
|
||||
{
|
||||
boost::timed_mutex m;
|
||||
BOOST_CHECK(m.timed_lock(boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10))));
|
||||
m.unlock();
|
||||
boost::timed_mutex::scoped_timed_lock lk(m,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)));
|
||||
BOOST_CHECK(lk.owns_lock());
|
||||
if(lk.owns_lock())
|
||||
{
|
||||
lk.unlock();
|
||||
}
|
||||
BOOST_CHECK(lk.timed_lock(boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10))));
|
||||
if(lk.owns_lock())
|
||||
{
|
||||
lk.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
bool predicate()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void test_xtime_condvar_backwards_compatibility()
|
||||
{
|
||||
boost::condition_variable cond;
|
||||
boost::condition_variable_any cond_any;
|
||||
boost::mutex m;
|
||||
|
||||
boost::mutex::scoped_lock lk(m);
|
||||
cond.timed_wait(lk,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)));
|
||||
cond.timed_wait(lk,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)),predicate);
|
||||
cond_any.timed_wait(lk,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)));
|
||||
cond_any.timed_wait(lk,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)),predicate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
|
||||
{
|
||||
boost::unit_test_framework::test_suite* test =
|
||||
@@ -60,6 +102,8 @@ boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
|
||||
|
||||
test->add(BOOST_TEST_CASE(&test_xtime_cmp));
|
||||
test->add(BOOST_TEST_CASE(&test_xtime_get));
|
||||
test->add(BOOST_TEST_CASE(&test_xtime_mutex_backwards_compatibility));
|
||||
test->add(BOOST_TEST_CASE(&test_xtime_condvar_backwards_compatibility));
|
||||
|
||||
return test;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user