// (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 #include #include #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_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(500); boost::posix_time::milliseconds const timeout_resolution(50); bool timed_lock_succeeded=rw_mutex.timed_lock_shared(timeout); BOOST_CHECK((timeout-timeout_resolution)add(BOOST_TEST_CASE(&test_timed_lock_shared_times_out_if_write_lock_held)); test->add(BOOST_TEST_CASE(&test_timed_lock_shared_succeeds_if_no_lock_held)); test->add(BOOST_TEST_CASE(&test_timed_lock_shared_succeeds_if_read_lock_held)); test->add(BOOST_TEST_CASE(&test_timed_lock_times_out_if_write_lock_held)); test->add(BOOST_TEST_CASE(&test_timed_lock_times_out_if_read_lock_held)); test->add(BOOST_TEST_CASE(&test_timed_lock_succeeds_if_no_lock_held)); test->add(BOOST_TEST_CASE(&test_timed_lock_times_out_but_read_lock_succeeds_if_read_lock_held)); return test; }