Make a helper routine __throw_future_error, and encapsulate the #ifdef _LIBCPP_NO_EXCEPTIONS there, instead of duplicating it throughout the code. No functionality change

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@246772 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2015-09-03 15:11:32 +00:00
parent 6d9da5891f
commit a189974948

View File

@@ -512,6 +512,17 @@ public:
virtual ~future_error() _NOEXCEPT; virtual ~future_error() _NOEXCEPT;
}; };
template <future_errc _Ev>
_LIBCPP_ALWAYS_INLINE
void __throw_future_error()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw future_error(make_error_code(_Ev));
#else
assert(!"future_error");
#endif
}
class _LIBCPP_TYPE_VIS __assoc_sub_state class _LIBCPP_TYPE_VIS __assoc_sub_state
: public __shared_count : public __shared_count
{ {
@@ -645,10 +656,8 @@ __assoc_state<_Rp>::set_value(_Arg& __arg)
#endif #endif
{ {
unique_lock<mutex> __lk(this->__mut_); unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
if (this->__has_value()) if (this->__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error<future_errc::promise_already_satisfied>();
#endif
::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg)); ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
this->__state_ |= base::__constructed | base::ready; this->__state_ |= base::__constructed | base::ready;
__cv_.notify_all(); __cv_.notify_all();
@@ -664,10 +673,8 @@ __assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
#endif #endif
{ {
unique_lock<mutex> __lk(this->__mut_); unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
if (this->__has_value()) if (this->__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error<future_errc::promise_already_satisfied>();
#endif
::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg)); ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
this->__state_ |= base::__constructed; this->__state_ |= base::__constructed;
__thread_local_data()->__make_ready_at_thread_exit(this); __thread_local_data()->__make_ready_at_thread_exit(this);
@@ -725,10 +732,8 @@ void
__assoc_state<_Rp&>::set_value(_Rp& __arg) __assoc_state<_Rp&>::set_value(_Rp& __arg)
{ {
unique_lock<mutex> __lk(this->__mut_); unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
if (this->__has_value()) if (this->__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error<future_errc::promise_already_satisfied>();
#endif
__value_ = _VSTD::addressof(__arg); __value_ = _VSTD::addressof(__arg);
this->__state_ |= base::__constructed | base::ready; this->__state_ |= base::__constructed | base::ready;
__cv_.notify_all(); __cv_.notify_all();
@@ -739,10 +744,8 @@ void
__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
{ {
unique_lock<mutex> __lk(this->__mut_); unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
if (this->__has_value()) if (this->__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error<future_errc::promise_already_satisfied>();
#endif
__value_ = _VSTD::addressof(__arg); __value_ = _VSTD::addressof(__arg);
this->__state_ |= base::__constructed; this->__state_ |= base::__constructed;
__thread_local_data()->__make_ready_at_thread_exit(this); __thread_local_data()->__make_ready_at_thread_exit(this);
@@ -1138,10 +1141,8 @@ template <class _Rp>
future<_Rp>::future(__assoc_state<_Rp>* __state) future<_Rp>::future(__assoc_state<_Rp>* __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();
} }
@@ -1242,10 +1243,8 @@ template <class _Rp>
future<_Rp&>::future(__assoc_state<_Rp&>* __state) future<_Rp&>::future(__assoc_state<_Rp&>* __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();
} }
@@ -1445,10 +1444,8 @@ template <class _Rp>
future<_Rp> future<_Rp>
promise<_Rp>::get_future() promise<_Rp>::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<_Rp>(__state_); return future<_Rp>(__state_);
} }
@@ -1456,10 +1453,8 @@ template <class _Rp>
void void
promise<_Rp>::set_value(const _Rp& __r) promise<_Rp>::set_value(const _Rp& __r)
{ {
#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(__r); __state_->set_value(__r);
} }
@@ -1469,10 +1464,8 @@ template <class _Rp>
void void
promise<_Rp>::set_value(_Rp&& __r) promise<_Rp>::set_value(_Rp&& __r)
{ {
#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(_VSTD::move(__r)); __state_->set_value(_VSTD::move(__r));
} }
@@ -1482,10 +1475,8 @@ template <class _Rp>
void void
promise<_Rp>::set_exception(exception_ptr __p) promise<_Rp>::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);
} }
@@ -1493,10 +1484,8 @@ template <class _Rp>
void void
promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
{ {
#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(__r); __state_->set_value_at_thread_exit(__r);
} }
@@ -1506,10 +1495,8 @@ template <class _Rp>
void void
promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
{ {
#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(_VSTD::move(__r)); __state_->set_value_at_thread_exit(_VSTD::move(__r));
} }
@@ -1519,10 +1506,8 @@ template <class _Rp>
void void
promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) promise<_Rp>::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);
} }
@@ -1619,10 +1604,8 @@ template <class _Rp>
future<_Rp&> future<_Rp&>
promise<_Rp&>::get_future() promise<_Rp&>::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<_Rp&>(__state_); return future<_Rp&>(__state_);
} }
@@ -1630,10 +1613,8 @@ template <class _Rp>
void void
promise<_Rp&>::set_value(_Rp& __r) promise<_Rp&>::set_value(_Rp& __r)
{ {
#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(__r); __state_->set_value(__r);
} }
@@ -1641,10 +1622,8 @@ template <class _Rp>
void void
promise<_Rp&>::set_exception(exception_ptr __p) promise<_Rp&>::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);
} }
@@ -1652,10 +1631,8 @@ template <class _Rp>
void void
promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
{ {
#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(__r); __state_->set_value_at_thread_exit(__r);
} }
@@ -1663,10 +1640,8 @@ template <class _Rp>
void void
promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) promise<_Rp&>::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);
} }
@@ -2087,11 +2062,11 @@ template<class _Rp, class ..._ArgTypes>
void void
packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__p_.__state_ == nullptr) if (__p_.__state_ == nullptr)
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error<future_errc::no_state>();
if (__p_.__state_->__has_value()) if (__p_.__state_->__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error<future_errc::promise_already_satisfied>();
#ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
#endif // _LIBCPP_NO_EXCEPTIONS #endif // _LIBCPP_NO_EXCEPTIONS
@@ -2109,11 +2084,11 @@ template<class _Rp, class ..._ArgTypes>
void void
packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__p_.__state_ == nullptr) if (__p_.__state_ == nullptr)
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error<future_errc::no_state>();
if (__p_.__state_->__has_value()) if (__p_.__state_->__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error<future_errc::promise_already_satisfied>();
#ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
#endif // _LIBCPP_NO_EXCEPTIONS #endif // _LIBCPP_NO_EXCEPTIONS
@@ -2131,10 +2106,8 @@ template<class _Rp, class ..._ArgTypes>
void void
packaged_task<_Rp(_ArgTypes...)>::reset() packaged_task<_Rp(_ArgTypes...)>::reset()
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (!valid()) if (!valid())
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error<future_errc::no_state>();
#endif // _LIBCPP_NO_EXCEPTIONS
__p_ = promise<result_type>(); __p_ = promise<result_type>();
} }
@@ -2218,11 +2191,11 @@ template<class ..._ArgTypes>
void void
packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__p_.__state_ == nullptr) if (__p_.__state_ == nullptr)
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error<future_errc::no_state>();
if (__p_.__state_->__has_value()) if (__p_.__state_->__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error<future_errc::promise_already_satisfied>();
#ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
#endif // _LIBCPP_NO_EXCEPTIONS #endif // _LIBCPP_NO_EXCEPTIONS
@@ -2241,11 +2214,11 @@ template<class ..._ArgTypes>
void void
packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__p_.__state_ == nullptr) if (__p_.__state_ == nullptr)
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error<future_errc::no_state>();
if (__p_.__state_->__has_value()) if (__p_.__state_->__has_value())
throw future_error(make_error_code(future_errc::promise_already_satisfied)); __throw_future_error<future_errc::promise_already_satisfied>();
#ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
#endif // _LIBCPP_NO_EXCEPTIONS #endif // _LIBCPP_NO_EXCEPTIONS
@@ -2264,10 +2237,8 @@ template<class ..._ArgTypes>
void void
packaged_task<void(_ArgTypes...)>::reset() packaged_task<void(_ArgTypes...)>::reset()
{ {
#ifndef _LIBCPP_NO_EXCEPTIONS
if (!valid()) if (!valid())
throw future_error(make_error_code(future_errc::no_state)); __throw_future_error<future_errc::no_state>();
#endif // _LIBCPP_NO_EXCEPTIONS
__p_ = promise<result_type>(); __p_ = promise<result_type>();
} }