Code cleanup - change naked 'throw' expressions to call helpre function '__throw_future_error'. The behavior change is that if you build libc++ with exceptions disabled, and then use that in a program that sets the value of the future twice (for example), it will now abort instead of behaving unpredictably.

llvm-svn: 338332
This commit is contained in:
Marshall Clow 2018-07-30 23:33:48 +00:00
parent a5ed43c1c9
commit 934864bfa7
1 changed files with 11 additions and 33 deletions

View File

@ -92,10 +92,8 @@ void
__assoc_sub_state::set_value() __assoc_sub_state::set_value()
{ {
unique_lock<mutex> __lk(__mut_); unique_lock<mutex> __lk(__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__has_value()) if (__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error(future_errc::promise_already_satisfied);
#endif
__state_ |= __constructed | ready; __state_ |= __constructed | ready;
__cv_.notify_all(); __cv_.notify_all();
} }
@ -104,10 +102,8 @@ void
__assoc_sub_state::set_value_at_thread_exit() __assoc_sub_state::set_value_at_thread_exit()
{ {
unique_lock<mutex> __lk(__mut_); unique_lock<mutex> __lk(__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__has_value()) if (__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error(future_errc::promise_already_satisfied);
#endif
__state_ |= __constructed; __state_ |= __constructed;
__thread_local_data()->__make_ready_at_thread_exit(this); __thread_local_data()->__make_ready_at_thread_exit(this);
} }
@ -116,10 +112,8 @@ void
__assoc_sub_state::set_exception(exception_ptr __p) __assoc_sub_state::set_exception(exception_ptr __p)
{ {
unique_lock<mutex> __lk(__mut_); unique_lock<mutex> __lk(__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__has_value()) if (__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error(future_errc::promise_already_satisfied);
#endif
__exception_ = __p; __exception_ = __p;
__state_ |= ready; __state_ |= ready;
__cv_.notify_all(); __cv_.notify_all();
@ -129,10 +123,8 @@ void
__assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p) __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p)
{ {
unique_lock<mutex> __lk(__mut_); unique_lock<mutex> __lk(__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__has_value()) if (__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error(future_errc::promise_already_satisfied);
#endif
__exception_ = __p; __exception_ = __p;
__thread_local_data()->__make_ready_at_thread_exit(this); __thread_local_data()->__make_ready_at_thread_exit(this);
} }
@ -181,18 +173,14 @@ __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk)
void void
__assoc_sub_state::__execute() __assoc_sub_state::__execute()
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS __throw_future_error(future_errc::no_state);
throw future_error(make_error_code(future_errc::no_state));
#endif
} }
future<void>::future(__assoc_sub_state* __state) future<void>::future(__assoc_sub_state* __state)
: __state_(__state) : __state_(__state)
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__state_->__has_future_attached()) if (__state_->__has_future_attached())
throw future_error(make_error_code(future_errc::future_already_retrieved)); __throw_future_error(future_errc::future_already_retrieved);
#endif
__state_->__add_shared(); __state_->__add_shared();
__state_->__set_future_attached(); __state_->__set_future_attached();
} }
@ -234,50 +222,40 @@ promise<void>::~promise()
future<void> future<void>
promise<void>::get_future() promise<void>::get_future()
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__state_ == nullptr) if (__state_ == nullptr)
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error(future_errc::no_state);
#endif
return future<void>(__state_); return future<void>(__state_);
} }
void void
promise<void>::set_value() promise<void>::set_value()
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__state_ == nullptr) if (__state_ == nullptr)
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error(future_errc::no_state);
#endif
__state_->set_value(); __state_->set_value();
} }
void void
promise<void>::set_exception(exception_ptr __p) promise<void>::set_exception(exception_ptr __p)
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__state_ == nullptr) if (__state_ == nullptr)
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error(future_errc::no_state);
#endif
__state_->set_exception(__p); __state_->set_exception(__p);
} }
void void
promise<void>::set_value_at_thread_exit() promise<void>::set_value_at_thread_exit()
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__state_ == nullptr) if (__state_ == nullptr)
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error(future_errc::no_state);
#endif
__state_->set_value_at_thread_exit(); __state_->set_value_at_thread_exit();
} }
void void
promise<void>::set_exception_at_thread_exit(exception_ptr __p) promise<void>::set_exception_at_thread_exit(exception_ptr __p)
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__state_ == nullptr) if (__state_ == nullptr)
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error(future_errc::no_state);
#endif
__state_->set_exception_at_thread_exit(__p); __state_->set_exception_at_thread_exit(__p);
} }