diff --git a/include/future b/include/future index abfb77d0..b75014ac 100644 --- a/include/future +++ b/include/future @@ -398,11 +398,6 @@ public: // construction and destruction packaged_task(); - template - explicit packaged_task(F f); - template - explicit packaged_task(allocator_arg_t, const Allocator& a, F f); - explicit packaged_task(R(*f)(ArgTypes...)); template explicit packaged_task(F&& f); template @@ -538,7 +533,7 @@ protected: unsigned __state_; virtual void __on_zero_shared(); - + void __sub_wait(unique_lock& __lk); public: enum { @@ -556,6 +551,8 @@ public: void __set_future_attached() {__state_ |= __future_attached;} bool __has_future_attached() const {return __state_ & __future_attached;} + void __set_deferred() {__state_ |= deferred;} + void __make_ready(); bool __is_ready() const {return __state_ & ready;} @@ -567,13 +564,15 @@ public: void copy(); - void wait() const; + void wait(); template future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const; template future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const; + + virtual void __execute(); }; template @@ -581,12 +580,12 @@ future_status __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const { unique_lock __lk(__mut_); - while (!(__state_ & (ready | deferred)) && _Clock::now() < __abs_time) + if (__state_ & deferred) + return future_status::deferred; + while (!(__state_ & ready) && _Clock::now() < __abs_time) __cv_.wait_until(__lk, __abs_time); if (__state_ & ready) return future_status::ready; - if (__state_ & deferred) - return future_status::deferred; return future_status::timeout; } @@ -678,8 +677,7 @@ _R __assoc_state<_R>::move() { unique_lock __lk(this->__mut_); - while (!this->__is_ready()) - this->__cv_.wait(__lk); + this->__sub_wait(__lk); if (this->__exception_ != nullptr) rethrow_exception(this->__exception_); return _STD::move(*reinterpret_cast<_R*>(&__value_)); @@ -690,8 +688,7 @@ typename add_lvalue_reference<_R>::type __assoc_state<_R>::copy() { unique_lock __lk(this->__mut_); - while (!this->__is_ready()) - this->__cv_.wait(__lk); + this->__sub_wait(__lk); if (this->__exception_ != nullptr) rethrow_exception(this->__exception_); return *reinterpret_cast<_R*>(&__value_); @@ -753,8 +750,7 @@ _R& __assoc_state<_R&>::copy() { unique_lock __lk(this->__mut_); - while (!this->__is_ready()) - this->__cv_.wait(__lk); + this->__sub_wait(__lk); if (this->__exception_ != nullptr) rethrow_exception(this->__exception_); return *__value_; @@ -829,10 +825,113 @@ __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() __a.deallocate(this, 1); } +template +class __deferred_assoc_state + : public __assoc_state<_R> +{ + typedef __assoc_state<_R> base; + + _F __func_; + +public: +#ifdef _LIBCPP_MOVE + explicit __deferred_assoc_state(_F&& __f); +#endif + + virtual void __execute(); +}; + +#ifdef _LIBCPP_MOVE + +template +inline _LIBCPP_INLINE_VISIBILITY +__deferred_assoc_state<_R, _F>::__deferred_assoc_state(_F&& __f) + : __func_(_STD::forward<_F>(__f)) +{ + this->__set_deferred(); +} + +#endif // _LIBCPP_MOVE + +template +void +__deferred_assoc_state<_R, _F>::__execute() +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + this->set_value(__func_()); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + this->set_exception(current_exception()); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} + +template +class __deferred_assoc_state + : public __assoc_sub_state +{ + typedef __assoc_sub_state base; + + _F __func_; + +public: +#ifdef _LIBCPP_MOVE + explicit __deferred_assoc_state(_F&& __f); +#endif + + virtual void __execute(); +}; + +#ifdef _LIBCPP_MOVE + +template +inline _LIBCPP_INLINE_VISIBILITY +__deferred_assoc_state::__deferred_assoc_state(_F&& __f) + : __func_(_STD::forward<_F>(__f)) +{ + this->__set_deferred(); +} + +#endif // _LIBCPP_MOVE + +template +void +__deferred_assoc_state::__execute() +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + __func_(); + this->set_value(); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + this->set_exception(current_exception()); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} + template class promise; // future +template class future; + +template +future<_R> +#ifdef _LIBCPP_MOVE +__make_deferred_assoc_state(_F&& __f); +#else +__make_deferred_assoc_state(_F __f); +#endif + template class future { @@ -841,6 +940,14 @@ class future explicit future(__assoc_state<_R>* __state); template friend class promise; + + template +#ifdef _LIBCPP_MOVE + friend future<_R1> __make_deferred_assoc_state(_F&& __f); +#else + friend future<_R1> __make_deferred_assoc_state(_F __f); +#endif + public: future() : __state_(nullptr) {} #ifdef _LIBCPP_MOVE @@ -887,8 +994,14 @@ future<_R>::future(__assoc_state<_R>* __state) if (__state_->__has_future_attached()) throw future_error(make_error_code(future_errc::future_already_retrieved)); __state_->__add_shared(); + __state_->__set_future_attached(); } +struct __release_shared_count +{ + void operator()(__shared_count* p) {p->__release_shared();} +}; + template future<_R>::~future() { @@ -900,6 +1013,7 @@ template _R future<_R>::get() { + unique_ptr<__shared_count, __release_shared_count> __(__state_); __assoc_state<_R>* __s = __state_; __state_ = nullptr; return __s->move(); @@ -913,6 +1027,14 @@ class future<_R&> explicit future(__assoc_state<_R&>* __state); template friend class promise; + + template +#ifdef _LIBCPP_MOVE + friend future<_R1> __make_deferred_assoc_state(_F&& __f); +#else + friend future<_R1> __make_deferred_assoc_state(_F __f); +#endif + public: future() : __state_(nullptr) {} #ifdef _LIBCPP_MOVE @@ -959,6 +1081,7 @@ future<_R&>::future(__assoc_state<_R&>* __state) if (__state_->__has_future_attached()) throw future_error(make_error_code(future_errc::future_already_retrieved)); __state_->__add_shared(); + __state_->__set_future_attached(); } template @@ -972,6 +1095,7 @@ template _R& future<_R&>::get() { + unique_ptr<__shared_count, __release_shared_count> __(__state_); __assoc_state<_R&>* __s = __state_; __state_ = nullptr; return __s->copy(); @@ -985,6 +1109,14 @@ class future explicit future(__assoc_sub_state* __state); template friend class promise; + + template +#ifdef _LIBCPP_MOVE + friend future<_R1> __make_deferred_assoc_state(_F&& __f); +#else + friend future<_R1> __make_deferred_assoc_state(_F __f); +#endif + public: future() : __state_(nullptr) {} #ifdef _LIBCPP_MOVE @@ -1026,10 +1158,16 @@ public: // promise +template class packaged_task; + template class promise { __assoc_state<_R>* __state_; + + explicit promise(nullptr_t) : __state_(nullptr) {} + + template friend class packaged_task; public: promise(); template @@ -1186,6 +1324,11 @@ template class promise<_R&> { __assoc_state<_R&>* __state_; + + explicit promise(nullptr_t) : __state_(nullptr) {} + + template friend class packaged_task; + public: promise(); template @@ -1310,6 +1453,11 @@ template <> class promise { __assoc_sub_state* __state_; + + explicit promise(nullptr_t) : __state_(nullptr) {} + + template friend class packaged_task; + public: promise(); template @@ -1374,6 +1522,534 @@ swap(promise<_R>& __x, promise<_R>& __y) template struct uses_allocator, _Alloc> : public true_type {}; +#ifndef _LIBCPP_HAS_NO_VARIADICS + +// packaged_task + +template class __packaged_task_base; + +template +class __packaged_task_base<_R(_ArgTypes...)> +{ + __packaged_task_base(const __packaged_task_base&); + __packaged_task_base& operator=(const __packaged_task_base&); +public: + __packaged_task_base() {} + virtual ~__packaged_task_base() {} + virtual void __move_to(__packaged_task_base*) = 0; + virtual void destroy() = 0; + virtual void destroy_deallocate() = 0; + virtual _R operator()(_ArgTypes&& ...) = 0; +}; + +template class __packaged_task_func; + +template +class __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)> + : public __packaged_task_base<_R(_ArgTypes...)> +{ + __compressed_pair<_F, _Alloc> __f_; +public: + explicit __packaged_task_func(const _F& __f) : __f_(__f) {} + explicit __packaged_task_func(_F&& __f) : __f_(_STD::move(__f)) {} + __packaged_task_func(const _F& __f, const _Alloc& __a) + : __f_(__f, __a) {} + __packaged_task_func(_F&& __f, const _Alloc& __a) + : __f_(_STD::move(__f), __a) {} + virtual void __move_to(__packaged_task_base<_R(_ArgTypes...)>*); + virtual void destroy(); + virtual void destroy_deallocate(); + virtual _R operator()(_ArgTypes&& ... __args); +}; + +template +void +__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::__move_to( + __packaged_task_base<_R(_ArgTypes...)>* __p) +{ + ::new (__p) __packaged_task_func(_STD::move(__f_.first()), _STD::move(__f_.second())); +} + +template +void +__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy() +{ + __f_.~__compressed_pair<_F, _Alloc>(); +} + +template +void +__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate() +{ + typedef typename _Alloc::template rebind<__packaged_task_func>::other _A; + _A __a(__f_.second()); + __f_.~__compressed_pair<_F, _Alloc>(); + __a.deallocate(this, 1); +} + +template +_R +__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg) +{ + return __invoke(__f_.first(), _STD::forward<_ArgTypes>(__arg)...); +} + +template class __packaged_task_function; + +template +class __packaged_task_function<_R(_ArgTypes...)> +{ + typedef __packaged_task_base<_R(_ArgTypes...)> __base; + aligned_storage<3*sizeof(void*)>::type __buf_; + __base* __f_; + +public: + typedef _R result_type; + + // construct/copy/destroy: + __packaged_task_function() : __f_(nullptr) {} + template + __packaged_task_function(_F&& __f); + template + __packaged_task_function(allocator_arg_t, const _Alloc& __a, _F&& __f); + + __packaged_task_function(__packaged_task_function&&); + __packaged_task_function& operator=(__packaged_task_function&&); + + __packaged_task_function(const __packaged_task_function&) = delete; + __packaged_task_function& operator=(const __packaged_task_function&) = delete; + + ~__packaged_task_function(); + + void swap(__packaged_task_function&); + + _R operator()(_ArgTypes...) const; +}; + +template +__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) +{ + if (__f.__f_ == nullptr) + __f_ = nullptr; + else if (__f.__f_ == (__base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__move_to(__f_); + } + else + { + __f_ = __f.__f_; + __f.__f_ = nullptr; + } +} + +template +template +__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(_F&& __f) + : __f_(nullptr) +{ + typedef typename remove_reference<_F>::type _FR; + typedef __packaged_task_func<_FR, allocator<_FR>, _R(_ArgTypes...)> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(_STD::forward<_F>(__f)); + } + else + { + typedef allocator<_FF> _A; + _A __a; + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(_STD::forward<_F>(__f), allocator<_FR>(__a)); + __f_ = __hold.release(); + } +} + +template +template +__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function( + allocator_arg_t, const _Alloc& __a0, _F&& __f) + : __f_(nullptr) +{ + typedef allocator_traits<_Alloc> __alloc_traits; + typedef typename remove_reference<_F>::type _FR; + typedef __packaged_task_func<_FR, _Alloc, _R(_ArgTypes...)> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(_STD::forward<_F>(__f)); + } + else + { + typedef typename __alloc_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind_alloc<_FF> +#else + rebind_alloc<_FF>::other +#endif + _A; + _A __a(__a0); + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(_STD::forward<_F>(__f), _Alloc(__a)); + __f_ = __hold.release(); + } +} + +template +__packaged_task_function<_R(_ArgTypes...)>& +__packaged_task_function<_R(_ArgTypes...)>::operator=(__packaged_task_function&& __f) +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); + __f_ = nullptr; + if (__f.__f_ == nullptr) + __f_ = nullptr; + else if (__f.__f_ == (__base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__move_to(__f_); + } + else + { + __f_ = __f.__f_; + __f.__f_ = nullptr; + } +} + +template +__packaged_task_function<_R(_ArgTypes...)>::~__packaged_task_function() +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); +} + +template +void +__packaged_task_function<_R(_ArgTypes...)>::swap(__packaged_task_function& __f) +{ + if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) + { + typename aligned_storage::type __tempbuf; + __base* __t = (__base*)&__tempbuf; + __f_->__move_to(__t); + __f_->destroy(); + __f_ = nullptr; + __f.__f_->__move_to((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = nullptr; + __f_ = (__base*)&__buf_; + __t->__move_to((__base*)&__f.__buf_); + __t->destroy(); + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f_ == (__base*)&__buf_) + { + __f_->__move_to((__base*)&__f.__buf_); + __f_->destroy(); + __f_ = __f.__f_; + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f.__f_ == (__base*)&__f.__buf_) + { + __f.__f_->__move_to((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = __f_; + __f_ = (__base*)&__buf_; + } + else + _STD::swap(__f_, __f.__f_); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +_R +__packaged_task_function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const +{ + return (*__f_)(_STD::forward<_ArgTypes>(__arg)...); +} + +template +class packaged_task<_R(_ArgTypes...)> +{ +public: + typedef _R result_type; + +private: + __packaged_task_function __f_; + promise __p_; + +public: + // construction and destruction + packaged_task() : __p_(nullptr) {} + template + explicit packaged_task(_F&& __f) : __f_(_STD::forward<_F>(__f)) {} + template + explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f) + : __f_(allocator_arg, __a, _STD::forward<_F>(__f)), + __p_(allocator_arg, __a) {} + // ~packaged_task() = default; + + // no copy + packaged_task(packaged_task&) = delete; + packaged_task& operator=(packaged_task&) = delete; + + // move support + packaged_task(packaged_task&& __other) + : __f_(_STD::move(__other.__f_)), __p_(_STD::move(__other.__p_)) {} + packaged_task& operator=(packaged_task&& __other) + { + __f_ = _STD::move(__other.__f_); + __p_ = _STD::move(__other.__p_); + return *this; + } + void swap(packaged_task& __other) + { + __f_.swap(__other.__f_); + __p_.swap(__other.__p_); + } + + //explicit + operator bool() const {return __p_.__state_ != nullptr;} + + // result retrieval + future get_future() {return __p_.get_future();} + + // execution + void operator()(_ArgTypes... __args); + void make_ready_at_thread_exit(_ArgTypes... __args); + + void reset(); +}; + +template +void +packaged_task<_R(_ArgTypes...)>::operator()(_ArgTypes... __args) +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (__p_.__state_ == nullptr) + throw future_error(make_error_code(future_errc::no_state)); + if (__p_.__state_->__has_value()) + throw future_error(make_error_code(future_errc::promise_already_satisfied)); + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + __p_.set_value(__f_(_STD::forward<_ArgTypes>(__args)...)); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + __p_.set_exception(current_exception()); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} + +template +void +packaged_task<_R(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (__p_.__state_ == nullptr) + throw future_error(make_error_code(future_errc::no_state)); + if (__p_.__state_->__has_value()) + throw future_error(make_error_code(future_errc::promise_already_satisfied)); + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + __p_.set_value_at_thread_exit(__f_(_STD::forward<_ArgTypes>(__args)...)); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + __p_.set_exception_at_thread_exit(current_exception()); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} + +template +void +packaged_task<_R(_ArgTypes...)>::reset() +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (!(*this)) + throw future_error(make_error_code(future_errc::no_state)); +#endif // _LIBCPP_NO_EXCEPTIONS + __p_ = promise(); +} + +template +class packaged_task +{ +public: + typedef void result_type; + +private: + __packaged_task_function __f_; + promise __p_; + +public: + // construction and destruction + packaged_task() : __p_(nullptr) {} + template + explicit packaged_task(_F&& __f) : __f_(_STD::forward<_F>(__f)) {} + template + explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f) + : __f_(allocator_arg, __a, _STD::forward<_F>(__f)), + __p_(allocator_arg, __a) {} + // ~packaged_task() = default; + + // no copy + packaged_task(packaged_task&) = delete; + packaged_task& operator=(packaged_task&) = delete; + + // move support + packaged_task(packaged_task&& __other) + : __f_(_STD::move(__other.__f_)), __p_(_STD::move(__other.__p_)) {} + packaged_task& operator=(packaged_task&& __other) + { + __f_ = _STD::move(__other.__f_); + __p_ = _STD::move(__other.__p_); + return *this; + } + void swap(packaged_task& __other) + { + __f_.swap(__other.__f_); + __p_.swap(__other.__p_); + } + + //explicit + operator bool() const {return __p_.__state_ != nullptr;} + + // result retrieval + future get_future() {return __p_.get_future();} + + // execution + void operator()(_ArgTypes... __args); + void make_ready_at_thread_exit(_ArgTypes... __args); + + void reset(); +}; + +template +void +packaged_task::operator()(_ArgTypes... __args) +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (__p_.__state_ == nullptr) + throw future_error(make_error_code(future_errc::no_state)); + if (__p_.__state_->__has_value()) + throw future_error(make_error_code(future_errc::promise_already_satisfied)); + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + __f_(_STD::forward<_ArgTypes>(__args)...); + __p_.set_value(); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + __p_.set_exception(current_exception()); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} + +template +void +packaged_task::make_ready_at_thread_exit(_ArgTypes... __args) +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (__p_.__state_ == nullptr) + throw future_error(make_error_code(future_errc::no_state)); + if (__p_.__state_->__has_value()) + throw future_error(make_error_code(future_errc::promise_already_satisfied)); + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + __f_(_STD::forward<_ArgTypes>(__args)...); + __p_.set_value_at_thread_exit(); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + __p_.set_exception_at_thread_exit(current_exception()); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} + +template +void +packaged_task::reset() +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (!(*this)) + throw future_error(make_error_code(future_errc::no_state)); +#endif // _LIBCPP_NO_EXCEPTIONS + __p_ = promise(); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +void +swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) +{ + __x.swap(__y); +} + +template +struct uses_allocator, _Alloc> : public true_type {}; + +template +future<_R> +#ifdef _LIBCPP_MOVE +__make_deferred_assoc_state(_F&& __f) +#else +__make_deferred_assoc_state(_F __f) +#endif +{ + unique_ptr<__deferred_assoc_state<_R, _F>, __release_shared_count> + __h(new __deferred_assoc_state<_R, _F>(_STD::forward<_F>(__f))); + return future<_R>(__h.get()); +} + +template +future::type> +async(launch __policy, _F&& __f, _Args&&... __args) +{ + typedef typename result_of<_F(_Args...)>::type _R; + future<_R> __r; + if (__policy == launch::sync) + __r = _STD::__make_deferred_assoc_state<_R>(bind(_STD::forward<_F>(__f), + _STD::forward<_Args>(__args)...)); + else + { + packaged_task<_R()> __pk(bind(_STD::forward<_F>(__f), + _STD::forward<_Args>(__args)...)); + __r = __pk.get_future(); + thread(_STD::move(__pk)).detach(); + } + return __r; +} + +template +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_same::type, launch>::value, + future::type> +>::type +async(_F&& __f, _Args&&... __args) +{ + return async(launch::any, _STD::forward<_F>(__f), + _STD::forward<_Args>(__args)...); +} + +#endif // _LIBCPP_HAS_NO_VARIADICS + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_FUTURE diff --git a/src/future.cpp b/src/future.cpp index 82b9b60e..a0082767 100644 --- a/src/future.cpp +++ b/src/future.cpp @@ -124,18 +124,39 @@ void __assoc_sub_state::copy() { unique_lock __lk(__mut_); - while (!__is_ready()) - __cv_.wait(__lk); + __sub_wait(__lk); if (__exception_ != nullptr) rethrow_exception(__exception_); } void -__assoc_sub_state::wait() const +__assoc_sub_state::wait() { unique_lock __lk(__mut_); - while (!__is_ready()) - __cv_.wait(__lk); + __sub_wait(__lk); +} + +void +__assoc_sub_state::__sub_wait(unique_lock& __lk) +{ + if (!__is_ready()) + { + if (__state_ & deferred) + { + __state_ &= ~deferred; + __lk.unlock(); + __execute(); + } + else + while (!__is_ready()) + __cv_.wait(__lk); + } +} + +void +__assoc_sub_state::__execute() +{ + throw future_error(make_error_code(future_errc::no_state)); } future::future(__assoc_sub_state* __state) @@ -144,6 +165,7 @@ future::future(__assoc_sub_state* __state) if (__state_->__has_future_attached()) throw future_error(make_error_code(future_errc::future_already_retrieved)); __state_->__add_shared(); + __state_->__set_future_attached(); } future::~future() @@ -155,9 +177,10 @@ future::~future() void future::get() { + unique_ptr<__shared_count, __release_shared_count> __(__state_); __assoc_sub_state* __s = __state_; __state_ = nullptr; - return __s->copy(); + __s->copy(); } promise::promise() diff --git a/test/thread/futures/futures.async/async.pass.cpp b/test/thread/futures/futures.async/async.pass.cpp new file mode 100644 index 00000000..f160ad35 --- /dev/null +++ b/test/thread/futures/futures.async/async.pass.cpp @@ -0,0 +1,177 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// future::type> +// async(F&& f, Args&&... args); + +// template +// future::type> +// async(launch policy, F&& f, Args&&... args); + +#include +#include +#include + +typedef std::chrono::high_resolution_clock Clock; +typedef std::chrono::milliseconds ms; + +int f0() +{ + std::this_thread::sleep_for(ms(200)); + return 3; +} + +int i = 0; + +int& f1() +{ + std::this_thread::sleep_for(ms(200)); + return i; +} + +void f2() +{ + std::this_thread::sleep_for(ms(200)); +} + +std::unique_ptr f3(int i) +{ + std::this_thread::sleep_for(ms(200)); + return std::unique_ptr(new int(i)); +} + +std::unique_ptr f4(std::unique_ptr&& p) +{ + std::this_thread::sleep_for(ms(200)); + return std::move(p); +} + +int main() +{ + { + std::future f = std::async(f0); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(f.get() == 3); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + { + std::future f = std::async(std::launch::async, f0); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(f.get() == 3); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + { + std::future f = std::async(std::launch::any, f0); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(f.get() == 3); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + { + std::future f = std::async(std::launch::sync, f0); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(f.get() == 3); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 > ms(100)); + } + + { + std::future f = std::async(f1); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(&f.get() == &i); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + { + std::future f = std::async(std::launch::async, f1); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(&f.get() == &i); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + { + std::future f = std::async(std::launch::any, f1); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(&f.get() == &i); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + { + std::future f = std::async(std::launch::sync, f1); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(&f.get() == &i); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 > ms(100)); + } + + { + std::future f = std::async(f2); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + f.get(); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + { + std::future f = std::async(std::launch::async, f2); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + f.get(); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + { + std::future f = std::async(std::launch::any, f2); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + f.get(); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + { + std::future f = std::async(std::launch::sync, f2); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + f.get(); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 > ms(100)); + } + + { + std::future> f = std::async(f3, 3); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(*f.get() == 3); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } + + { + std::future> f = + std::async(f4, std::unique_ptr(new int(3))); + std::this_thread::sleep_for(ms(300)); + Clock::time_point t0 = Clock::now(); + assert(*f.get() == 3); + Clock::time_point t1 = Clock::now(); + assert(t1-t0 < ms(100)); + } +} diff --git a/test/thread/futures/futures.promise/get_future.pass.cpp b/test/thread/futures/futures.promise/get_future.pass.cpp new file mode 100644 index 00000000..daa2c061 --- /dev/null +++ b/test/thread/futures/futures.promise/get_future.pass.cpp @@ -0,0 +1,53 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class promise + +// future get_future(); + +#include +#include + +int main() +{ + { + std::promise p; + std::future f = p.get_future(); + p.set_value(105.5); + assert(f.get() == 105.5); + } + { + std::promise p; + std::future f = p.get_future(); + try + { + f = p.get_future(); + assert(false); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::future_already_retrieved)); + } + } + { + std::promise p; + std::promise p0 = std::move(p); + try + { + std::future f = p.get_future(); + assert(false); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::no_state)); + } + } +} diff --git a/test/thread/futures/futures.promise/set_lvalue.pass.cpp b/test/thread/futures/futures.promise/set_lvalue.pass.cpp index 4ebd8dd2..8144fa68 100644 --- a/test/thread/futures/futures.promise/set_lvalue.pass.cpp +++ b/test/thread/futures/futures.promise/set_lvalue.pass.cpp @@ -24,10 +24,10 @@ int main() std::promise p; std::future f = p.get_future(); p.set_value(i); - assert(f.get() == 3); + int& j = f.get(); + assert(j == 3); ++i; - f = p.get_future(); - assert(f.get() == 4); + assert(j == 4); try { p.set_value(i); diff --git a/test/thread/futures/futures.tas/futures.task.members/assign_copy.fail.cpp b/test/thread/futures/futures.tas/futures.task.members/assign_copy.fail.cpp new file mode 100644 index 00000000..ea6c4fad --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/assign_copy.fail.cpp @@ -0,0 +1,48 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// packaged_task& operator=(packaged_task&) = delete; + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +int main() +{ + { + std::packaged_task p0(A(5)); + std::packaged_task p; + p = p0; + assert(!p0); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + } + { + std::packaged_task p0; + std::packaged_task p; + p = p0; + assert(!p0); + assert(!p); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/assign_move.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/assign_move.pass.cpp new file mode 100644 index 00000000..c33349d3 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/assign_move.pass.cpp @@ -0,0 +1,48 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// packaged_task& operator=(packaged_task&& other); + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +int main() +{ + { + std::packaged_task p0(A(5)); + std::packaged_task p; + p = std::move(p0); + assert(!p0); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + } + { + std::packaged_task p0; + std::packaged_task p; + p = std::move(p0); + assert(!p0); + assert(!p); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/ctor_copy.fail.cpp b/test/thread/futures/futures.tas/futures.task.members/ctor_copy.fail.cpp new file mode 100644 index 00000000..b2bcce53 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/ctor_copy.fail.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// packaged_task(packaged_task&) = delete; + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +int main() +{ + { + std::packaged_task p0(A(5)); + std::packaged_task p(p0); + assert(!p0); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + } + { + std::packaged_task p0; + std::packaged_task p(p0); + assert(!p0); + assert(!p); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/ctor_default.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/ctor_default.pass.cpp new file mode 100644 index 00000000..4e5fece7 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/ctor_default.pass.cpp @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// packaged_task(); + +#include +#include + +struct A {}; + +int main() +{ + std::packaged_task p; + assert(!p); +} diff --git a/test/thread/futures/futures.tas/futures.task.members/ctor_func.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/ctor_func.pass.cpp new file mode 100644 index 00000000..de603215 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/ctor_func.pass.cpp @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// template +// explicit packaged_task(F&& f); + +#include +#include + +class A +{ + long data_; + +public: + static int n_moves; + static int n_copies; + + explicit A(long i) : data_(i) {} + A(A&& a) : data_(a.data_) {++n_moves; a.data_ = -1;} + A(const A& a) : data_(a.data_) {++n_copies;} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +int A::n_moves = 0; +int A::n_copies = 0; + +int main() +{ + { + std::packaged_task p(A(5)); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + assert(A::n_copies == 0); + assert(A::n_moves > 0); + } + A::n_copies == 0; + A::n_copies = 0; + { + A a(5); + std::packaged_task p(a); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + assert(A::n_copies > 0); + assert(A::n_moves > 0); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/ctor_func_alloc.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/ctor_func_alloc.pass.cpp new file mode 100644 index 00000000..e38446f1 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/ctor_func_alloc.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// template +// explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f); + +#include +#include + +#include "../../test_allocator.h" + +class A +{ + long data_; + +public: + static int n_moves; + static int n_copies; + + explicit A(long i) : data_(i) {} + A(A&& a) : data_(a.data_) {++n_moves; a.data_ = -1;} + A(const A& a) : data_(a.data_) {++n_copies;} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +int A::n_moves = 0; +int A::n_copies = 0; + +int main() +{ + { + std::packaged_task p(std::allocator_arg, + test_allocator(), A(5)); + assert(test_alloc_base::count > 0); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + assert(A::n_copies == 0); + assert(A::n_moves > 0); + } + assert(test_alloc_base::count == 0); + A::n_copies == 0; + A::n_copies = 0; + { + A a(5); + std::packaged_task p(std::allocator_arg, + test_allocator(), a); + assert(test_alloc_base::count > 0); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + assert(A::n_copies > 0); + assert(A::n_moves > 0); + } + assert(test_alloc_base::count == 0); +} diff --git a/test/thread/futures/futures.tas/futures.task.members/ctor_move.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/ctor_move.pass.cpp new file mode 100644 index 00000000..2bc5061d --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/ctor_move.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// packaged_task(packaged_task&& other); + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +int main() +{ + { + std::packaged_task p0(A(5)); + std::packaged_task p = std::move(p0); + assert(!p0); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + } + { + std::packaged_task p0; + std::packaged_task p = std::move(p0); + assert(!p0); + assert(!p); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/dtor.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/dtor.pass.cpp new file mode 100644 index 00000000..0f719173 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/dtor.pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// ~packaged_task(); + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +void func(std::packaged_task& p) +{ +} + +void func2(std::packaged_task& p) +{ + p(3, 'a'); +} + +int main() +{ + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + std::thread(func, std::move(p)).detach(); + try + { + double i = f.get(); + assert(false); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::broken_promise)); + } + } + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + std::thread(func2, std::move(p)).detach(); + assert(f.get() == 105.0); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/get_future.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/get_future.pass.cpp new file mode 100644 index 00000000..369028e9 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/get_future.pass.cpp @@ -0,0 +1,62 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// future get_future(); + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +int main() +{ + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + } + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + try + { + f = p.get_future(); + assert(false); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::future_already_retrieved)); + } + } + { + std::packaged_task p; + try + { + std::future f = p.get_future(); + assert(false); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::no_state)); + } + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/make_ready_at_thread_exit.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/make_ready_at_thread_exit.pass.cpp new file mode 100644 index 00000000..11c0f91b --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/make_ready_at_thread_exit.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// void make_ready_at_thread_exit(ArgTypes... args); + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const + { + if (j == 'z') + throw A(6); + return data_ + i + j; + } +}; + +void func0(std::packaged_task& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p.make_ready_at_thread_exit(3, 'a'); +} + +void func1(std::packaged_task& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p.make_ready_at_thread_exit(3, 'z'); +} + +void func2(std::packaged_task& p) +{ + p.make_ready_at_thread_exit(3, 'a'); + try + { + p.make_ready_at_thread_exit(3, 'c'); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::promise_already_satisfied)); + } +} + +void func3(std::packaged_task& p) +{ + try + { + p.make_ready_at_thread_exit(3, 'a'); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::no_state)); + } +} + +int main() +{ + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + std::thread(func0, std::move(p)).detach(); + assert(f.get() == 105.0); + } + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + std::thread(func1, std::move(p)).detach(); + try + { + f.get(); + assert(false); + } + catch (const A& e) + { + assert(e(3, 'a') == 106); + } + } + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + std::thread(func2, std::move(p)).detach(); + assert(f.get() == 105.0); + } + { + std::packaged_task p; + std::thread t(func3, std::move(p)); + t.join(); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/operator.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/operator.pass.cpp new file mode 100644 index 00000000..fff8467d --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/operator.pass.cpp @@ -0,0 +1,105 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// void operator()(ArgTypes... args); + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const + { + if (j == 'z') + throw A(6); + return data_ + i + j; + } +}; + +void func0(std::packaged_task& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p(3, 'a'); +} + +void func1(std::packaged_task& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p(3, 'z'); +} + +void func2(std::packaged_task& p) +{ + p(3, 'a'); + try + { + p(3, 'c'); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::promise_already_satisfied)); + } +} + +void func3(std::packaged_task& p) +{ + try + { + p(3, 'a'); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::no_state)); + } +} + +int main() +{ + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + std::thread(func0, std::move(p)).detach(); + assert(f.get() == 105.0); + } + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + std::thread(func1, std::move(p)).detach(); + try + { + f.get(); + assert(false); + } + catch (const A& e) + { + assert(e(3, 'a') == 106); + } + } + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + std::thread t(func2, std::move(p)); + assert(f.get() == 105.0); + t.join(); + } + { + std::packaged_task p; + std::thread t(func3, std::move(p)); + t.join(); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/reset.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/reset.pass.cpp new file mode 100644 index 00000000..db9cfbf2 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/reset.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// void reset(); + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const + { + if (j == 'z') + throw A(6); + return data_ + i + j; + } +}; + +int main() +{ + { + std::packaged_task p(A(5)); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + p.reset(); + p(4, 'a'); + f = p.get_future(); + assert(f.get() == 106.0); + } + { + std::packaged_task p; + try + { + p.reset(); + assert(false); + } + catch (const std::future_error& e) + { + assert(e.code() == make_error_code(std::future_errc::no_state)); + } + } +} diff --git a/test/thread/futures/futures.tas/futures.task.members/swap.pass.cpp b/test/thread/futures/futures.tas/futures.task.members/swap.pass.cpp new file mode 100644 index 00000000..c5bd7f59 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.members/swap.pass.cpp @@ -0,0 +1,48 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// void swap(packaged_task& other); + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +int main() +{ + { + std::packaged_task p0(A(5)); + std::packaged_task p; + p.swap(p0); + assert(!p0); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + } + { + std::packaged_task p0; + std::packaged_task p; + p.swap(p0); + assert(!p0); + assert(!p); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.nonmembers/swap.pass.cpp b/test/thread/futures/futures.tas/futures.task.nonmembers/swap.pass.cpp new file mode 100644 index 00000000..f6916a5a --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.nonmembers/swap.pass.cpp @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// template +// void +// swap(packaged_task& x, packaged_task& y); + +#include +#include + +class A +{ + long data_; + +public: + explicit A(long i) : data_(i) {} + + long operator()(long i, long j) const {return data_ + i + j;} +}; + +int main() +{ + { + std::packaged_task p0(A(5)); + std::packaged_task p; + swap(p, p0); + assert(!p0); + assert(p); + std::future f = p.get_future(); + p(3, 'a'); + assert(f.get() == 105.0); + } + { + std::packaged_task p0; + std::packaged_task p; + swap(p, p0); + assert(!p0); + assert(!p); + } +} diff --git a/test/thread/futures/futures.tas/futures.task.nonmembers/uses_allocator.pass.cpp b/test/thread/futures/futures.tas/futures.task.nonmembers/uses_allocator.pass.cpp new file mode 100644 index 00000000..3203bf27 --- /dev/null +++ b/test/thread/futures/futures.tas/futures.task.nonmembers/uses_allocator.pass.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class packaged_task + +// template +// struct uses_allocator, Alloc> +// : true_type { }; + +#include +#include "../../test_allocator.h" + +int main() +{ + static_assert((std::uses_allocator, test_allocator >::value), ""); +} diff --git a/test/thread/futures/futures.tas/types.pass.cpp b/test/thread/futures/futures.tas/types.pass.cpp new file mode 100644 index 00000000..a8569dcd --- /dev/null +++ b/test/thread/futures/futures.tas/types.pass.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class packaged_task +// { +// public: +// typedef R result_type; + +#include +#include + +struct A {}; + +int main() +{ + static_assert((std::is_same::result_type, A>::value), ""); +}