From 5c7858279481d39301ade86971476c57daec36b4 Mon Sep 17 00:00:00 2001 From: "Vicente J. Botet Escriba" Date: Tue, 17 Sep 2013 20:57:46 +0000 Subject: [PATCH] Thread: refactor make_ready_at_thread_exit + inhibit at_thread_exit functions from external threads. [SVN r85732] --- include/boost/thread/detail/thread.hpp | 13 +++++++++ include/boost/thread/future.hpp | 13 ++++----- src/pthread/thread.cpp | 37 ++++++++++++++++++++------ src/win32/thread.cpp | 11 ++++++++ 4 files changed, 60 insertions(+), 14 deletions(-) diff --git a/include/boost/thread/detail/thread.hpp b/include/boost/thread/detail/thread.hpp index e65d76b0..a6e2da30 100644 --- a/include/boost/thread/detail/thread.hpp +++ b/include/boost/thread/detail/thread.hpp @@ -826,6 +826,19 @@ namespace boost }; void BOOST_THREAD_DECL add_thread_exit_function(thread_exit_function_base*); + struct shared_state_base; +#if defined(BOOST_THREAD_PLATFORM_WIN32) + inline void make_ready_at_thread_exit(shared_ptr as) + { + detail::thread_data_base* const current_thread_data(detail::get_current_thread_data()); + if(current_thread_data) + { + current_thread_data->make_ready_at_thread_exit(as); + } + } +#else + void BOOST_THREAD_DECL make_ready_at_thread_exit(shared_ptr as); +#endif } namespace this_thread diff --git a/include/boost/thread/future.hpp b/include/boost/thread/future.hpp index 635d2022..2ae68aec 100644 --- a/include/boost/thread/future.hpp +++ b/include/boost/thread/future.hpp @@ -418,7 +418,7 @@ namespace boost { throw_exception(promise_already_satisfied()); } - get_current_thread_data()->make_ready_at_thread_exit(shared_from_this()); + detail::make_ready_at_thread_exit(shared_from_this()); } #endif @@ -431,7 +431,8 @@ namespace boost } exception=e; this->is_constructed = true; - get_current_thread_data()->make_ready_at_thread_exit(shared_from_this()); + detail::make_ready_at_thread_exit(shared_from_this()); + } bool has_value() const @@ -688,7 +689,7 @@ namespace boost result.reset(new T(result_)); this->is_constructed = true; - get_current_thread_data()->make_ready_at_thread_exit(shared_from_this()); + detail::make_ready_at_thread_exit(shared_from_this()); } //void set_value_at_thread_exit(BOOST_THREAD_RV_REF(T) result_) void set_value_at_thread_exit(rvalue_source_type result_) @@ -699,7 +700,7 @@ namespace boost result.reset(new T(boost::move(result_))); //future_traits::init(result,static_cast(result_)); this->is_constructed = true; - get_current_thread_data()->make_ready_at_thread_exit(shared_from_this()); + detail::make_ready_at_thread_exit(shared_from_this()); } @@ -760,7 +761,7 @@ namespace boost //future_traits::init(result,result_); result= &result_; this->is_constructed = true; - get_current_thread_data()->make_ready_at_thread_exit(shared_from_this()); + detail::make_ready_at_thread_exit(shared_from_this()); } private: @@ -806,7 +807,7 @@ namespace boost throw_exception(promise_already_satisfied()); } this->is_constructed = true; - get_current_thread_data()->make_ready_at_thread_exit(shared_from_this()); + detail::make_ready_at_thread_exit(shared_from_this()); } private: shared_state(shared_state const&); diff --git a/src/pthread/thread.cpp b/src/pthread/thread.cpp index 96ef117a..bd67bae2 100644 --- a/src/pthread/thread.cpp +++ b/src/pthread/thread.cpp @@ -187,7 +187,9 @@ namespace boost return 0; } } - + } + namespace detail + { struct externally_launched_thread: detail::thread_data_base { @@ -197,7 +199,12 @@ namespace boost interrupt_enabled=false; #endif } - + ~externally_launched_thread() { + BOOST_ASSERT(notify.empty()); + notify.clear(); + BOOST_ASSERT(async_states_.empty()); + async_states_.clear(); + } void run() {} void notify_all_at_thread_exit(condition_variable*, mutex*) @@ -208,18 +215,18 @@ namespace boost void operator=(externally_launched_thread&); }; - detail::thread_data_base* make_external_thread_data() + thread_data_base* make_external_thread_data() { - detail::thread_data_base* const me(new externally_launched_thread()); + thread_data_base* const me(new externally_launched_thread()); me->self.reset(me); set_current_thread_data(me); return me; } - detail::thread_data_base* get_or_make_current_thread_data() + thread_data_base* get_or_make_current_thread_data() { - detail::thread_data_base* current_thread_data(detail::get_current_thread_data()); + thread_data_base* current_thread_data(get_current_thread_data()); if(!current_thread_data) { current_thread_data=make_external_thread_data(); @@ -701,8 +708,11 @@ namespace boost void erase_tss_node(void const* key) { - detail::thread_data_base* const current_thread_data(get_or_make_current_thread_data()); - current_thread_data->tss_data.erase(key); + detail::thread_data_base* const current_thread_data(get_current_thread_data()); + if(current_thread_data) + { + current_thread_data->tss_data.erase(key); + } } void set_tss_data(void const* key, @@ -740,6 +750,17 @@ namespace boost current_thread_data->notify_all_at_thread_exit(&cond, lk.release()); } } +namespace detail { + + void BOOST_THREAD_DECL make_ready_at_thread_exit(shared_ptr as) + { + detail::thread_data_base* const current_thread_data(detail::get_current_thread_data()); + if(current_thread_data) + { + current_thread_data->make_ready_at_thread_exit(as); + } + } +} diff --git a/src/win32/thread.cpp b/src/win32/thread.cpp index a90e8a53..8d8e3e2b 100644 --- a/src/win32/thread.cpp +++ b/src/win32/thread.cpp @@ -754,5 +754,16 @@ namespace boost current_thread_data->notify_all_at_thread_exit(&cond, lk.release()); } } +//namespace detail { +// +// void BOOST_THREAD_DECL make_ready_at_thread_exit(shared_ptr as) +// { +// detail::thread_data_base* const current_thread_data(detail::get_current_thread_data()); +// if(current_thread_data) +// { +// current_thread_data->make_ready_at_thread_exit(as); +// } +// } +//} }