Пример #1
0
void test_only_one_upgrade_lock_permitted()
{
    typedef hpx::lcos::local::shared_mutex shared_mutex_type;
    typedef hpx::lcos::local::mutex mutex_type;

    unsigned const number_of_threads = 2;

    test::thread_group pool;

    shared_mutex_type rw_mutex;
    unsigned unblocked_count = 0;
    unsigned simultaneous_running_count = 0;
    unsigned max_simultaneous_running = 0;
    mutex_type unblocked_count_mutex;
    hpx::lcos::local::condition_variable unblocked_condition;
    mutex_type finish_mutex;
    boost::unique_lock<mutex_type> finish_lock(finish_mutex);

    try
    {
        for( unsigned i = 0; i != number_of_threads; ++i)
        {
            pool.create_thread(
                test::locking_thread<boost::upgrade_lock<shared_mutex_type> >(
                    rw_mutex, unblocked_count, unblocked_count_mutex,
                    unblocked_condition, finish_mutex,
                    simultaneous_running_count, max_simultaneous_running
                )
            );
        }

        hpx::this_thread::sleep_for(boost::chrono::seconds(1));

        CHECK_LOCKED_VALUE_EQUAL(unblocked_count_mutex,
            unblocked_count, 1u);

        finish_lock.unlock();
        pool.join_all();
    }
    catch(...)
    {
        pool.interrupt_all();
        pool.join_all();
        HPX_TEST(false);
    }

    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_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=10;

    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);
}
Пример #3
0
void test_timed_lock_shared_succeeds_if_read_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 reader(simple_reading_thread(rw_mutex,finish_mutex,unblocked_mutex,unblocked_count));
    boost::this_thread::sleep_for(boost::chrono::seconds(1));
    CHECK_LOCKED_VALUE_EQUAL(unblocked_mutex,unblocked_count,1u);

    boost::chrono::steady_clock::time_point const start=boost::chrono::steady_clock::now();
    boost::chrono::steady_clock::time_point const timeout=start+boost::chrono::milliseconds(500);
    boost::chrono::milliseconds const timeout_resolution(50);
    bool timed_lock_succeeded=rw_mutex.try_lock_shared_until(timeout);
    BOOST_CHECK(boost::chrono::steady_clock::now()<timeout);
    BOOST_CHECK(timed_lock_succeeded);
    if(timed_lock_succeeded)
    {
        rw_mutex.unlock_shared();
    }

    boost::chrono::milliseconds const wait_duration(500);
    boost::chrono::steady_clock::time_point const timeout2=boost::chrono::steady_clock::now()+wait_duration;
    timed_lock_succeeded=rw_mutex.try_lock_shared_for(wait_duration);
    BOOST_CHECK(boost::chrono::steady_clock::now()<timeout2);
    BOOST_CHECK(timed_lock_succeeded);
    if(timed_lock_succeeded)
    {
        rw_mutex.unlock_shared();
    }

    finish_lock.unlock();
    reader.join();
}
void test_timed_lock_shared_succeeds_if_read_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 reader(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);

    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(boost::get_system_time()<timeout);
    BOOST_CHECK(timed_lock_succeeded);
    if(timed_lock_succeeded)
    {
        rw_mutex.unlock_shared();
    }

    boost::posix_time::milliseconds const wait_duration(500);
    boost::system_time const timeout2=boost::get_system_time()+wait_duration;
    timed_lock_succeeded=rw_mutex.timed_lock_shared(wait_duration);
    BOOST_CHECK(boost::get_system_time()<timeout2);
    BOOST_CHECK(timed_lock_succeeded);
    if(timed_lock_succeeded)
    {
        rw_mutex.unlock_shared();
    }

    finish_lock.unlock();
    reader.join();
}
Пример #5
0
void test_if_other_thread_has_upgrade_lock_try_lock_upgrade_returns_false()
{
    typedef hpx::lcos::local::shared_mutex shared_mutex_type;
    typedef hpx::lcos::local::mutex mutex_type;

    shared_mutex_type rw_mutex;
    mutex_type finish_mutex;
    mutex_type unblocked_mutex;
    unsigned unblocked_count = 0;
    boost::unique_lock<mutex_type> finish_lock(finish_mutex);
    hpx::thread writer(test::simple_upgrade_thread(
        rw_mutex, finish_mutex, unblocked_mutex, unblocked_count));

    hpx::this_thread::sleep_for(boost::chrono::seconds(1));

    CHECK_LOCKED_VALUE_EQUAL(unblocked_mutex,
        unblocked_count, 1u);

    bool const try_succeeded = rw_mutex.try_lock_upgrade();
    HPX_TEST(!try_succeeded);
    if (try_succeeded)
    {
        rw_mutex.unlock_upgrade();
    }

    finish_lock.unlock();
    writer.join();
}
void test_timed_lock_times_out_if_write_lock_held()
{
    boost::shared_mutex rw_mutex;
    boost::mutex finish_mutex;
    boost::mutex unblocked_mutex;
    unsigned unblocked_count=0;
    boost::unique_lock<boost::mutex> 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(timeout);
    BOOST_CHECK((timeout-timeout_resolution)<boost::get_system_time());
    BOOST_CHECK(!timed_lock_succeeded);
    if(timed_lock_succeeded)
    {
        rw_mutex.unlock();
    }

    boost::posix_time::milliseconds const wait_duration(500);
    boost::system_time const timeout2=boost::get_system_time()+wait_duration;
    timed_lock_succeeded=rw_mutex.timed_lock(wait_duration);
    BOOST_CHECK((timeout2-timeout_resolution)<boost::get_system_time());
    BOOST_CHECK(!timed_lock_succeeded);
    if(timed_lock_succeeded)
    {
        rw_mutex.unlock();
    }

    finish_lock.unlock();
    writer.join();
}
void test_multiple_readers()
{
    unsigned const number_of_threads=10;
    
    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_upgrade_lock_permitted()
{
    unsigned const number_of_threads=10;
    
    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_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();
}
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();
}
Пример #11
0
void test_can_lock_upgrade_if_currently_locked_shared()
{
    typedef hpx::lcos::local::shared_mutex shared_mutex_type;
    typedef hpx::lcos::local::mutex mutex_type;

    test::thread_group pool;

    shared_mutex_type rw_mutex;
    unsigned unblocked_count = 0;
    unsigned simultaneous_running_count = 0;
    unsigned max_simultaneous_running = 0;
    mutex_type unblocked_count_mutex;
    hpx::lcos::local::condition_variable unblocked_condition;
    mutex_type finish_mutex;
    boost::unique_lock<mutex_type> finish_lock(finish_mutex);

    unsigned const reader_count = 10;

    try
    {
        for(unsigned i = 0; i != reader_count; ++i)
        {
            pool.create_thread(
                test::locking_thread<boost::shared_lock<shared_mutex_type> >(
                    rw_mutex, unblocked_count,  unblocked_count_mutex,
                    unblocked_condition, finish_mutex,
                    simultaneous_running_count, max_simultaneous_running
                )
            );
        }

        hpx::this_thread::sleep_for(boost::chrono::seconds(1));

        pool.create_thread(
            test::locking_thread<boost::upgrade_lock<shared_mutex_type> >(
                rw_mutex, unblocked_count, unblocked_count_mutex,
                unblocked_condition, finish_mutex,
                simultaneous_running_count, max_simultaneous_running
            )
        );

        {
            boost::unique_lock<mutex_type> 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();
        HPX_TEST(false);
    }

    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);
}