From 7700826614a5d6c5ccbbf92e643fbf2d21599fdd Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Sun, 5 Oct 2008 01:47:33 +0000 Subject: [PATCH] merged from trunk [SVN r49133] --- include/boost/thread/detail/move.hpp | 4 +- include/boost/thread/detail/thread.hpp | 4 +- .../boost/thread/pthread/recursive_mutex.hpp | 2 +- include/boost/thread/pthread/shared_mutex.hpp | 46 +++++++++--------- test/test_thread_move.cpp | 47 ++++++++++++++++--- 5 files changed, 69 insertions(+), 34 deletions(-) diff --git a/include/boost/thread/detail/move.hpp b/include/boost/thread/detail/move.hpp index 91b2eda1..044ecda6 100644 --- a/include/boost/thread/detail/move.hpp +++ b/include/boost/thread/detail/move.hpp @@ -41,9 +41,9 @@ namespace boost #ifndef BOOST_NO_SFINAE template - typename enable_if >, detail::thread_move_t >::type move(T& t) + typename enable_if >, T >::type move(T& t) { - return t; + return T(detail::thread_move_t(t)); } #endif diff --git a/include/boost/thread/detail/thread.hpp b/include/boost/thread/detail/thread.hpp index 3d39d1df..fbb895d1 100644 --- a/include/boost/thread/detail/thread.hpp +++ b/include/boost/thread/detail/thread.hpp @@ -339,9 +339,9 @@ namespace boost return t; } #else - inline detail::thread_move_t move(detail::thread_move_t t) + inline thread move(detail::thread_move_t t) { - return t; + return thread(t); } #endif diff --git a/include/boost/thread/pthread/recursive_mutex.hpp b/include/boost/thread/pthread/recursive_mutex.hpp index 30689460..f3f7bf1d 100644 --- a/include/boost/thread/pthread/recursive_mutex.hpp +++ b/include/boost/thread/pthread/recursive_mutex.hpp @@ -177,7 +177,7 @@ namespace boost { struct timespec const timeout=detail::get_timespec(abs_time); int const res=pthread_mutex_timedlock(&m,&timeout); - BOOST_ASSERT(!res || res==EBUSY); + BOOST_ASSERT(!res || res==ETIMEDOUT); return !res; } diff --git a/include/boost/thread/pthread/shared_mutex.hpp b/include/boost/thread/pthread/shared_mutex.hpp index 74345d88..3ce4e233 100644 --- a/include/boost/thread/pthread/shared_mutex.hpp +++ b/include/boost/thread/pthread/shared_mutex.hpp @@ -57,18 +57,18 @@ namespace boost void lock_shared() { boost::this_thread::disable_interruption do_not_disturb; - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); while(state.exclusive || state.exclusive_waiting_blocked) { - shared_cond.wait(lock); + shared_cond.wait(lk); } ++state.shared_count; } bool try_lock_shared() { - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); if(state.exclusive || state.exclusive_waiting_blocked) { @@ -84,11 +84,11 @@ namespace boost bool timed_lock_shared(system_time const& timeout) { boost::this_thread::disable_interruption do_not_disturb; - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); while(state.exclusive || state.exclusive_waiting_blocked) { - if(!shared_cond.timed_wait(lock,timeout)) + if(!shared_cond.timed_wait(lk,timeout)) { return false; } @@ -105,7 +105,7 @@ namespace boost void unlock_shared() { - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); bool const last_reader=!--state.shared_count; if(last_reader) @@ -127,12 +127,12 @@ namespace boost void lock() { boost::this_thread::disable_interruption do_not_disturb; - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); while(state.shared_count || state.exclusive) { state.exclusive_waiting_blocked=true; - exclusive_cond.wait(lock); + exclusive_cond.wait(lk); } state.exclusive=true; } @@ -140,12 +140,12 @@ namespace boost bool timed_lock(system_time const& timeout) { boost::this_thread::disable_interruption do_not_disturb; - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); while(state.shared_count || state.exclusive) { state.exclusive_waiting_blocked=true; - if(!exclusive_cond.timed_wait(lock,timeout)) + if(!exclusive_cond.timed_wait(lk,timeout)) { if(state.shared_count || state.exclusive) { @@ -168,7 +168,7 @@ namespace boost bool try_lock() { - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); if(state.shared_count || state.exclusive) { @@ -184,7 +184,7 @@ namespace boost void unlock() { - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); state.exclusive=false; state.exclusive_waiting_blocked=false; release_waiters(); @@ -193,10 +193,10 @@ namespace boost void lock_upgrade() { boost::this_thread::disable_interruption do_not_disturb; - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); while(state.exclusive || state.exclusive_waiting_blocked || state.upgrade) { - shared_cond.wait(lock); + shared_cond.wait(lk); } ++state.shared_count; state.upgrade=true; @@ -205,10 +205,10 @@ namespace boost bool timed_lock_upgrade(system_time const& timeout) { boost::this_thread::disable_interruption do_not_disturb; - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); while(state.exclusive || state.exclusive_waiting_blocked || state.upgrade) { - if(!shared_cond.timed_wait(lock,timeout)) + if(!shared_cond.timed_wait(lk,timeout)) { if(state.exclusive || state.exclusive_waiting_blocked || state.upgrade) { @@ -230,7 +230,7 @@ namespace boost bool try_lock_upgrade() { - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); if(state.exclusive || state.exclusive_waiting_blocked || state.upgrade) { return false; @@ -245,7 +245,7 @@ namespace boost void unlock_upgrade() { - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); state.upgrade=false; bool const last_reader=!--state.shared_count; @@ -259,11 +259,11 @@ namespace boost void unlock_upgrade_and_lock() { boost::this_thread::disable_interruption do_not_disturb; - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); --state.shared_count; while(state.shared_count) { - upgrade_cond.wait(lock); + upgrade_cond.wait(lk); } state.upgrade=false; state.exclusive=true; @@ -271,7 +271,7 @@ namespace boost void unlock_and_lock_upgrade() { - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); state.exclusive=false; state.upgrade=true; ++state.shared_count; @@ -281,7 +281,7 @@ namespace boost void unlock_and_lock_shared() { - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); state.exclusive=false; ++state.shared_count; state.exclusive_waiting_blocked=false; @@ -290,7 +290,7 @@ namespace boost void unlock_upgrade_and_lock_shared() { - boost::mutex::scoped_lock lock(state_change); + boost::mutex::scoped_lock lk(state_change); state.upgrade=false; state.exclusive_waiting_blocked=false; release_waiters(); diff --git a/test/test_thread_move.cpp b/test/test_thread_move.cpp index dc8f964f..550b62dd 100644 --- a/test/test_thread_move.cpp +++ b/test/test_thread_move.cpp @@ -5,26 +5,59 @@ #include #include -void do_nothing() -{} +void do_nothing(boost::thread::id* my_id) +{ + *my_id=boost::this_thread::get_id(); +} void test_move_on_construction() { - boost::thread x=boost::thread(do_nothing); + boost::thread::id the_id; + boost::thread x=boost::thread(do_nothing,&the_id); + boost::thread::id x_id=x.get_id(); x.join(); + BOOST_CHECK_EQUAL(the_id,x_id); } -boost::thread make_thread() +boost::thread make_thread(boost::thread::id* the_id) { - return boost::thread(do_nothing); + return boost::thread(do_nothing,the_id); } void test_move_from_function_return() { - boost::thread x=make_thread(); + boost::thread::id the_id; + boost::thread x=make_thread(&the_id); + boost::thread::id x_id=x.get_id(); x.join(); + BOOST_CHECK_EQUAL(the_id,x_id); } +boost::thread make_thread_return_lvalue(boost::thread::id* the_id) +{ + boost::thread t(do_nothing,the_id); + return boost::move(t); +} + +void test_move_from_function_return_lvalue() +{ + boost::thread::id the_id; + boost::thread x=make_thread_return_lvalue(&the_id); + boost::thread::id x_id=x.get_id(); + x.join(); + BOOST_CHECK_EQUAL(the_id,x_id); +} + +void test_move_assign() +{ + boost::thread::id the_id; + boost::thread x(do_nothing,&the_id); + boost::thread y; + y=boost::move(x); + boost::thread::id y_id=y.get_id(); + y.join(); + BOOST_CHECK_EQUAL(the_id,y_id); +} boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) { @@ -33,5 +66,7 @@ boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) test->add(BOOST_TEST_CASE(test_move_on_construction)); test->add(BOOST_TEST_CASE(test_move_from_function_return)); + test->add(BOOST_TEST_CASE(test_move_from_function_return_lvalue)); + test->add(BOOST_TEST_CASE(test_move_assign)); return test; }