Commit b3eed6fe by Jonathan Wakely Committed by Jonathan Wakely

re PR libstdc++/42201 ([C++0x] std::vector<std::unique_future<T>>::push_back fails)

2010-01-21  Jonathan Wakely  <jwakely.gcc@gmail.com>

        PR libstdc++/42201
	* include/std/future: Update to latest WP.
	* src/functexcept.cc (__throw_future_error): Use make_error_code.
	* testsuite/30_threads/async/any.cc: New.
	* testsuite/30_threads/async/async.cc: New.
	* testsuite/30_threads/async/sync.cc: New.
	* testsuite/30_threads/packaged_task/cons/alloc.cc: New.
	* testsuite/30_threads/packaged_task/cons/assign_neg.cc: Adjust.
	* testsuite/30_threads/packaged_task/cons/copy_neg.cc: Adjust.
	* testsuite/30_threads/packaged_task/members/get_future.cc: Adjust.
	* testsuite/30_threads/packaged_task/members/get_future2.cc: Likewise.
	* testsuite/30_threads/packaged_task/members/invoke.cc: Adjust.
	* testsuite/30_threads/packaged_task/members/invoke2.cc: Adjust.
	* testsuite/30_threads/packaged_task/members/invoke3.cc: Adjust.
	* testsuite/30_threads/packaged_task/members/invoke4.cc: Adjust.
	* testsuite/30_threads/packaged_task/members/reset.cc: Adjust.
	* testsuite/30_threads/packaged_task/members/reset2.cc: Adjust.
	* testsuite/30_threads/shared_future/cons/assign_neg.cc: Remove.
	* testsuite/30_threads/shared_future/cons/default_neg.cc: Remove.
	* testsuite/30_threads/shared_future/cons/default.cc: New.
	* testsuite/30_threads/shared_future/cons/assign.cc: New.
	* testsuite/30_threads/shared_future/cons/copy.cc: Adjust.
	* testsuite/30_threads/shared_future/cons/move.cc: Adjust.
	* testsuite/30_threads/shared_future/cons/move_assign.cc: New.
	* testsuite/30_threads/shared_future/members/is_ready.cc: Remove.
	* testsuite/30_threads/shared_future/members/has_value.cc: Remove.
	* testsuite/30_threads/shared_future/members/has_exception.cc: Remove.
	* testsuite/30_threads/shared_future/members/valid.cc: New.
	* testsuite/30_threads/unique_future/cons/default_neg.cc: Remove.
	* testsuite/30_threads/unique_future/cons/default.cc: New.
	* testsuite/30_threads/unique_future/cons/move_assign.cc: New.
	* testsuite/30_threads/unique_future/cons/assign_neg.cc: Adjust.
	* testsuite/30_threads/unique_future/cons/copy_neg.cc: Adjust.
	* testsuite/30_threads/unique_future/cons/move.cc: Adjust.
	* testsuite/30_threads/unique_future/requirements/
        explicit_instantiation.cc: Adjust.
	* testsuite/30_threads/unique_future/members/is_ready.cc: Remove.
	* testsuite/30_threads/unique_future/members/has_value.cc: Remove.
	* testsuite/30_threads/unique_future/members/has_exception.cc: Remove.
	* testsuite/30_threads/unique_future/members/valid.cc: New.
	* testsuite/30_threads/unique_future/members/get.cc: Adjust.
	* testsuite/30_threads/unique_future/members/get2.cc: Adjust.
	* testsuite/30_threads/unique_future/members/wait.cc: Adjust.
	* testsuite/30_threads/unique_future/members/wait_for.cc: Adjust.
	* testsuite/30_threads/unique_future/members/wait_until.cc: Adjust.
	* testsuite/30_threads/headers/future/types_std_c++0x.cc: Adjust.
	* testsuite/30_threads/promise/cons/alloc.cc: New.
	* testsuite/30_threads/promise/cons/assign_neg.cc: Adjust.
	* testsuite/30_threads/promise/cons/copy_neg.cc: Adjust.
	* testsuite/30_threads/promise/cons/move.cc: Adjust.
	* testsuite/30_threads/promise/cons/move_assign.cc: Adjust.
	* testsuite/30_threads/promise/members/get_future.cc: Adjust.
	* testsuite/30_threads/promise/members/set_value.cc: Adjust.
	* testsuite/30_threads/promise/members/set_exception.cc: Adjust.
	* testsuite/30_threads/promise/members/set_exception2.cc: Adjust.
	* testsuite/30_threads/promise/members/set_value2.cc: Adjust.
	* testsuite/30_threads/promise/members/set_value3.cc: Adjust.
	* testsuite/30_threads/promise/members/swap.cc: Adjust.

From-SVN: r156097
parent 4227d4a1
2010-01-21 Jonathan Wakely <jwakely.gcc@gmail.com>
PR libstdc++/42201
* include/std/future: Update to latest WP.
* src/functexcept.cc (__throw_future_error): Use make_error_code.
* testsuite/30_threads/async/any.cc: New.
* testsuite/30_threads/async/async.cc: New.
* testsuite/30_threads/async/sync.cc: New.
* testsuite/30_threads/packaged_task/cons/alloc.cc: New.
* testsuite/30_threads/packaged_task/cons/assign_neg.cc: Adjust.
* testsuite/30_threads/packaged_task/cons/copy_neg.cc: Adjust.
* testsuite/30_threads/packaged_task/members/get_future.cc: Adjust.
* testsuite/30_threads/packaged_task/members/get_future2.cc: Likewise.
* testsuite/30_threads/packaged_task/members/invoke.cc: Adjust.
* testsuite/30_threads/packaged_task/members/invoke2.cc: Adjust.
* testsuite/30_threads/packaged_task/members/invoke3.cc: Adjust.
* testsuite/30_threads/packaged_task/members/invoke4.cc: Adjust.
* testsuite/30_threads/packaged_task/members/reset.cc: Adjust.
* testsuite/30_threads/packaged_task/members/reset2.cc: Adjust.
* testsuite/30_threads/shared_future/cons/assign_neg.cc: Remove.
* testsuite/30_threads/shared_future/cons/default_neg.cc: Remove.
* testsuite/30_threads/shared_future/cons/default.cc: New.
* testsuite/30_threads/shared_future/cons/assign.cc: New.
* testsuite/30_threads/shared_future/cons/copy.cc: Adjust.
* testsuite/30_threads/shared_future/cons/move.cc: Adjust.
* testsuite/30_threads/shared_future/cons/move_assign.cc: New.
* testsuite/30_threads/shared_future/members/is_ready.cc: Remove.
* testsuite/30_threads/shared_future/members/has_value.cc: Remove.
* testsuite/30_threads/shared_future/members/has_exception.cc: Remove.
* testsuite/30_threads/shared_future/members/valid.cc: New.
* testsuite/30_threads/unique_future/cons/default_neg.cc: Remove.
* testsuite/30_threads/unique_future/cons/default.cc: New.
* testsuite/30_threads/unique_future/cons/move_assign.cc: New.
* testsuite/30_threads/unique_future/cons/assign_neg.cc: Adjust.
* testsuite/30_threads/unique_future/cons/copy_neg.cc: Adjust.
* testsuite/30_threads/unique_future/cons/move.cc: Adjust.
* testsuite/30_threads/unique_future/requirements/
explicit_instantiation.cc: Adjust.
* testsuite/30_threads/unique_future/members/is_ready.cc: Remove.
* testsuite/30_threads/unique_future/members/has_value.cc: Remove.
* testsuite/30_threads/unique_future/members/has_exception.cc: Remove.
* testsuite/30_threads/unique_future/members/valid.cc: New.
* testsuite/30_threads/unique_future/members/get.cc: Adjust.
* testsuite/30_threads/unique_future/members/get2.cc: Adjust.
* testsuite/30_threads/unique_future/members/wait.cc: Adjust.
* testsuite/30_threads/unique_future/members/wait_for.cc: Adjust.
* testsuite/30_threads/unique_future/members/wait_until.cc: Adjust.
* testsuite/30_threads/headers/future/types_std_c++0x.cc: Adjust.
* testsuite/30_threads/promise/cons/alloc.cc: New.
* testsuite/30_threads/promise/cons/assign_neg.cc: Adjust.
* testsuite/30_threads/promise/cons/copy_neg.cc: Adjust.
* testsuite/30_threads/promise/cons/move.cc: Adjust.
* testsuite/30_threads/promise/cons/move_assign.cc: Adjust.
* testsuite/30_threads/promise/members/get_future.cc: Adjust.
* testsuite/30_threads/promise/members/set_value.cc: Adjust.
* testsuite/30_threads/promise/members/set_exception.cc: Adjust.
* testsuite/30_threads/promise/members/set_exception2.cc: Adjust.
* testsuite/30_threads/promise/members/set_value2.cc: Adjust.
* testsuite/30_threads/promise/members/set_value3.cc: Adjust.
* testsuite/30_threads/promise/members/swap.cc: Adjust.
2010-01-20 Janis Johnson <janis187@us.ibm.com> 2010-01-20 Janis Johnson <janis187@us.ibm.com>
Paolo Carlini <paolo.carlini@oracle.com> Paolo Carlini <paolo.carlini@oracle.com>
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <thread>
#include <condition_variable> #include <condition_variable>
#include <system_error> #include <system_error>
#include <exception> #include <exception>
...@@ -56,10 +57,13 @@ namespace std ...@@ -56,10 +57,13 @@ namespace std
/// Error code for futures /// Error code for futures
enum class future_errc enum class future_errc
{ broken_promise, future_already_retrieved, promise_already_satisfied }; {
broken_promise,
future_already_retrieved,
promise_already_satisfied,
no_state
};
// TODO: requires concepts
// concept_map ErrorCodeEnum<future_errc> { }
template<> template<>
struct is_error_code_enum<future_errc> : public true_type { }; struct is_error_code_enum<future_errc> : public true_type { };
...@@ -83,8 +87,8 @@ namespace std ...@@ -83,8 +87,8 @@ namespace std
error_code _M_code; error_code _M_code;
public: public:
explicit future_error(future_errc __ec) explicit future_error(error_code __ec)
: logic_error("std::future_error"), _M_code(make_error_code(__ec)) : logic_error("std::future_error"), _M_code(__ec)
{ } { }
virtual ~future_error() throw(); virtual ~future_error() throw();
...@@ -98,17 +102,30 @@ namespace std ...@@ -98,17 +102,30 @@ namespace std
// Forward declarations. // Forward declarations.
template<typename _Res> template<typename _Res>
class unique_future; class future;
template<typename _Res> template<typename _Res>
class shared_future; class shared_future;
template<typename> template<typename _Res>
class atomic_future;
template<typename _Signature>
class packaged_task; class packaged_task;
template<typename _Res> template<typename _Res>
class promise; class promise;
enum class launch { any, async, sync };
template<typename _Fn, typename... _Args>
future<typename _Fn::result_type>
async(launch __policy, _Fn&& __fn, _Args&&... __args);
template<typename _Fn, typename... _Args>
future<typename _Fn::result_type>
async(_Fn&& __fn, _Args&&... __args);
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \ #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
&& defined(_GLIBCXX_ATOMIC_BUILTINS_4) && defined(_GLIBCXX_ATOMIC_BUILTINS_4)
...@@ -124,9 +141,7 @@ namespace std ...@@ -124,9 +141,7 @@ namespace std
_Result_base(const _Result_base&) = delete; _Result_base(const _Result_base&) = delete;
_Result_base& operator=(const _Result_base&) = delete; _Result_base& operator=(const _Result_base&) = delete;
// _M_destroy() allows derived classes to control deallocation, // _M_destroy() allows derived classes to control deallocation
// which will be needed when allocator support is added to promise.
// See http://gcc.gnu.org/ml/libstdc++/2009-06/msg00032.html
virtual void _M_destroy() = 0; virtual void _M_destroy() = 0;
struct _Deleter struct _Deleter
...@@ -183,7 +198,6 @@ namespace std ...@@ -183,7 +198,6 @@ namespace std
void* _M_addr() { return static_cast<void*>(&_M_storage); } void* _M_addr() { return static_cast<void*>(&_M_storage); }
}; };
// TODO: use template alias when available // TODO: use template alias when available
/* /*
template<typename _Res> template<typename _Res>
...@@ -196,6 +210,50 @@ namespace std ...@@ -196,6 +210,50 @@ namespace std
typedef unique_ptr<_Res, _Result_base::_Deleter> type; typedef unique_ptr<_Res, _Result_base::_Deleter> type;
}; };
// TODO: use when allocator_arg_t available
/*
/// Result_alloc.
template<typename _Res, typename _Alloc>
struct _Result_alloc : _Result<_Res>
{
typedef typename _Alloc::template rebind<_Result_alloc>::other
__allocator_type;
explicit
_Result_alloc(const _Alloc& __a) : _Result<_Res>(), _M_alloc(__a)
{ }
private:
void _M_destroy()
{
__allocator_type __a(_M_alloc);
__a.destroy(this);
__a.deallocate(this, 1);
}
__allocator_type _M_alloc;
};
template<typename _Res, typename _Allocator>
static typename _Ptr<_Result_alloc<_Res, _Allocator>>::type
_S_allocate_result(const _Allocator& __a)
{
typedef _Result_alloc<_Res, _Allocator> __result_type;
typename __result_type::__allocator_type __a2(__a);
__result_type* __p = __a2.allocate(1);
__try
{
__a2.construct(__p, __a);
}
__catch(...)
{
__a2.deallocate(__p, 1);
__throw_exception_again;
}
return typename _Ptr<__result_type>::type(__p);
}
*/
/// Shared state between a promise and one or more associated futures. /// Shared state between a promise and one or more associated futures.
class _State class _State
...@@ -206,6 +264,7 @@ namespace std ...@@ -206,6 +264,7 @@ namespace std
mutex _M_mutex; mutex _M_mutex;
condition_variable _M_cond; condition_variable _M_cond;
atomic_flag _M_retrieved; atomic_flag _M_retrieved;
once_flag _M_once;
public: public:
_State() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { } _State() : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
...@@ -213,30 +272,13 @@ namespace std ...@@ -213,30 +272,13 @@ namespace std
_State(const _State&) = delete; _State(const _State&) = delete;
_State& operator=(const _State&) = delete; _State& operator=(const _State&) = delete;
bool
is_ready()
{ return _M_get() != 0; }
bool
has_exception()
{
_Result_base* const __res = _M_get();
return __res && !(__res->_M_error == 0);
}
bool
has_value()
{
_Result_base* const __res = _M_get();
return __res && (__res->_M_error == 0);
}
_Result_base& _Result_base&
wait() wait()
{ {
_M_run_deferred();
unique_lock<mutex> __lock(_M_mutex); unique_lock<mutex> __lock(_M_mutex);
if (!_M_ready()) if (!_M_ready())
_M_cond.wait(__lock, std::bind(&_State::_M_ready, this)); _M_cond.wait(__lock, std::bind<bool>(&_State::_M_ready, this));
return *_M_result; return *_M_result;
} }
...@@ -245,7 +287,7 @@ namespace std ...@@ -245,7 +287,7 @@ namespace std
wait_for(const chrono::duration<_Rep, _Period>& __rel) wait_for(const chrono::duration<_Rep, _Period>& __rel)
{ {
unique_lock<mutex> __lock(_M_mutex); unique_lock<mutex> __lock(_M_mutex);
auto __bound = std::bind(&_State::_M_ready, this); auto __bound = std::bind<bool>(&_State::_M_ready, this);
return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound); return _M_ready() || _M_cond.wait_for(__lock, __rel, __bound);
} }
...@@ -254,20 +296,20 @@ namespace std ...@@ -254,20 +296,20 @@ namespace std
wait_until(const chrono::time_point<_Clock, _Duration>& __abs) wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
{ {
unique_lock<mutex> __lock(_M_mutex); unique_lock<mutex> __lock(_M_mutex);
auto __bound = std::bind(&_State::_M_ready, this); auto __bound = std::bind<bool>(&_State::_M_ready, this);
return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound); return _M_ready() || _M_cond.wait_until(__lock, __abs, __bound);
} }
void void
_M_set_result(_Ptr_type __res) _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
{ {
{ bool __set = __ignore_failure;
lock_guard<mutex> __lock(_M_mutex); // all calls to this function are serialized,
if (_M_ready()) // side-effects of invoking __res only happen once
call_once(_M_once, mem_fn(&_State::_M_do_set), this, ref(__res),
ref(__set));
if (!__set)
__throw_future_error(int(future_errc::promise_already_satisfied)); __throw_future_error(int(future_errc::promise_already_satisfied));
_M_result.swap(__res);
}
_M_cond.notify_all();
} }
void void
...@@ -275,7 +317,7 @@ namespace std ...@@ -275,7 +317,7 @@ namespace std
{ {
if (static_cast<bool>(__res)) if (static_cast<bool>(__res))
{ {
future_errc __ec(future_errc::broken_promise); // XXX error_code __ec(make_error_code(future_errc::broken_promise));
__res->_M_error = copy_exception(future_error(__ec)); __res->_M_error = copy_exception(future_error(__ec));
{ {
lock_guard<mutex> __lock(_M_mutex); lock_guard<mutex> __lock(_M_mutex);
...@@ -285,7 +327,7 @@ namespace std ...@@ -285,7 +327,7 @@ namespace std
} }
} }
// Called when this object is passed to a unique_future. // Called when this object is passed to a future.
void void
_M_set_retrieved_flag() _M_set_retrieved_flag()
{ {
...@@ -293,16 +335,114 @@ namespace std ...@@ -293,16 +335,114 @@ namespace std
__throw_future_error(int(future_errc::future_already_retrieved)); __throw_future_error(int(future_errc::future_already_retrieved));
} }
template<typename _Res, typename _Arg>
struct _Setter;
// set lvalues
template<typename _Res, typename _Arg>
struct _Setter<_Res, _Arg&>
{
// check this is only used by promise<R>::set_value(const R&)
// or promise<R>::set_value(R&)
static_assert(is_same<_Res, _Arg&>::value // promise<R&>
|| is_same<const _Res, _Arg>::value, // promise<R>
"Invalid specialisation");
typename promise<_Res>::_Ptr_type operator()()
{
_State::_S_check(_M_promise->_M_future);
_M_promise->_M_storage->_M_set(_M_arg);
return std::move(_M_promise->_M_storage);
}
promise<_Res>* _M_promise;
_Arg& _M_arg;
};
// set rvalues
template<typename _Res>
struct _Setter<_Res, _Res&&>
{
typename promise<_Res>::_Ptr_type operator()()
{
_State::_S_check(_M_promise->_M_future);
_M_promise->_M_storage->_M_set(std::move(_M_arg));
return std::move(_M_promise->_M_storage);
}
promise<_Res>* _M_promise;
_Res& _M_arg;
};
struct __exception_ptr_tag { };
// set exceptions
template<typename _Res>
struct _Setter<_Res, __exception_ptr_tag>
{
typename promise<_Res>::_Ptr_type operator()()
{
_State::_S_check(_M_promise->_M_future);
_M_promise->_M_storage->_M_error = _M_ex;
return std::move(_M_promise->_M_storage);
}
promise<_Res>* _M_promise;
exception_ptr& _M_ex;
};
template<typename _Res, typename _Arg>
static _Setter<_Res, _Arg&&>
__setter(promise<_Res>* __prom, _Arg&& __arg)
{
return _Setter<_Res, _Arg&&>{ __prom, __arg };
}
template<typename _Res>
static _Setter<_Res, __exception_ptr_tag>
__setter(exception_ptr& __ex, promise<_Res>* __prom)
{
return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
}
static _Setter<void, void>
__setter(promise<void>* __prom);
template<typename _Tp>
static bool
_S_check(const shared_ptr<_Tp>& __p)
{
if (!static_cast<bool>(__p))
__throw_future_error((int)future_errc::no_state);
}
private: private:
_Result_base* void
_M_get() _M_do_set(function<_Ptr_type()>& __f, bool& __set)
{
_Ptr_type __res = __f();
{ {
lock_guard<mutex> __lock(_M_mutex); lock_guard<mutex> __lock(_M_mutex);
return _M_result.get(); _M_result.swap(__res);
}
_M_cond.notify_all();
__set = true;
} }
bool _M_ready() const { return static_cast<bool>(_M_result); } bool _M_ready() const { return static_cast<bool>(_M_result); }
virtual void _M_run_deferred() { }
}; };
template<typename _Res>
class _Deferred_state;
template<typename _Res>
class _Async_state;
template<typename _Signature>
class _Task_state;
template<typename _StateT, typename _Res = typename _StateT::_Res_type>
struct _Task_setter;
}; };
inline __future_base::_Result_base::~_Result_base() = default; inline __future_base::_Result_base::~_Result_base() = default;
...@@ -313,9 +453,13 @@ namespace std ...@@ -313,9 +453,13 @@ namespace std
{ {
_Result() : _M_value_ptr() { } _Result() : _M_value_ptr() { }
_Res* _M_value_ptr; void _M_set(_Res& __res) { _M_value_ptr = &__res; }
_Res& _M_get() { return *_M_value_ptr; }
private: private:
_Res* _M_value_ptr;
void _M_destroy() { delete this; } void _M_destroy() { delete this; }
}; };
...@@ -328,7 +472,7 @@ namespace std ...@@ -328,7 +472,7 @@ namespace std
}; };
/// Common implementation for unique_future and shared_future. /// Common implementation for future and shared_future.
template<typename _Res> template<typename _Res>
class __basic_future : public __future_base class __basic_future : public __future_base
{ {
...@@ -344,134 +488,190 @@ namespace std ...@@ -344,134 +488,190 @@ namespace std
__basic_future(const __basic_future&) = delete; __basic_future(const __basic_future&) = delete;
__basic_future& operator=(const __basic_future&) = delete; __basic_future& operator=(const __basic_future&) = delete;
// Functions to check state and wait for ready.
bool
is_ready() const { return this->_M_state->is_ready(); }
bool bool
has_exception() const { return this->_M_state->has_exception(); } valid() const { return static_cast<bool>(_M_state); }
bool
has_value() const { return this->_M_state->has_value(); }
void void
wait() const { this->_M_state->wait(); } wait() const { _M_state->wait(); }
template<typename _Rep, typename _Period> template<typename _Rep, typename _Period>
bool bool
wait_for(const chrono::duration<_Rep, _Period>& __rel) const wait_for(const chrono::duration<_Rep, _Period>& __rel) const
{ return this->_M_state->wait_for(__rel); } { return _M_state->wait_for(__rel); }
template<typename _Clock, typename _Duration> template<typename _Clock, typename _Duration>
bool bool
wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
{ return this->_M_state->wait_until(__abs); } { return _M_state->wait_until(__abs); }
protected: protected:
/// Wait for the state to be ready and rethrow any stored exception /// Wait for the state to be ready and rethrow any stored exception
__result_type __result_type
_M_get_result() _M_get_result()
{ {
_Result_base& __res = this->_M_state->wait(); _Result_base& __res = _M_state->wait();
if (!(__res._M_error == 0)) if (!(__res._M_error == 0))
rethrow_exception(__res._M_error); rethrow_exception(__res._M_error);
return static_cast<__result_type>(__res); return static_cast<__result_type>(__res);
} }
// Construction of a unique_future by promise::get_future() void _M_swap(__basic_future& __that)
{
_M_state.swap(__that._M_state);
}
// Construction of a future by promise::get_future()
explicit explicit
__basic_future(const __state_type& __state) : _M_state(__state) __basic_future(const __state_type& __state) : _M_state(__state)
{ {
if (static_cast<bool>(this->_M_state)) _State::_S_check(_M_state);
this->_M_state->_M_set_retrieved_flag(); _M_state->_M_set_retrieved_flag();
else
__throw_future_error(int(future_errc::future_already_retrieved));
} }
// Copy construction from a shared_future // Copy construction from a shared_future
explicit explicit
__basic_future(const shared_future<_Res>&); __basic_future(const shared_future<_Res>&);
// Move construction from a unique_future // Move construction from a shared_future
explicit
__basic_future(shared_future<_Res>&&);
// Move construction from a future
explicit explicit
__basic_future(unique_future<_Res>&&); __basic_future(future<_Res>&&);
__basic_future() { }
struct _Reset
{
explicit _Reset(__basic_future& __fut) : _M_fut(__fut) { }
~_Reset() { _M_fut._M_state.reset(); }
__basic_future& _M_fut;
};
}; };
/// Primary template for unique_future. /// Primary template for future.
template<typename _Res> template<typename _Res>
class unique_future : public __basic_future<_Res> class future : public __basic_future<_Res>
{ {
friend class promise<_Res>; friend class promise<_Res>;
template<typename> friend class packaged_task;
template<typename _Fn, typename... _Args>
friend future<typename _Fn::result_type>
async(launch, _Fn&&, _Args&&...);
typedef __basic_future<_Res> _Base_type; typedef __basic_future<_Res> _Base_type;
typedef typename _Base_type::__state_type __state_type; typedef typename _Base_type::__state_type __state_type;
explicit explicit
unique_future(const __state_type& __state) : _Base_type(__state) { } future(const __state_type& __state) : _Base_type(__state) { }
public: public:
future() : _Base_type() { }
/// Move constructor /// Move constructor
unique_future(unique_future&& __uf) : _Base_type(std::move(__uf)) { } future(future&& __uf) : _Base_type(std::move(__uf)) { }
// Disable copying // Disable copying
unique_future(const unique_future&) = delete; future(const future&) = delete;
unique_future& operator=(const unique_future&) = delete; future& operator=(const future&) = delete;
future& operator=(future&& __fut)
{
future(std::move(__fut))._M_swap(*this);
return *this;
}
/// Retrieving the value /// Retrieving the value
_Res&& _Res
get() get()
{ return std::move(this->_M_get_result()._M_value()); } {
typename _Base_type::_Reset __reset(*this);
return std::move(this->_M_get_result()._M_value());
}
}; };
/// Partial specialization for unique_future<R&> /// Partial specialization for future<R&>
template<typename _Res> template<typename _Res>
class unique_future<_Res&> : public __basic_future<_Res&> class future<_Res&> : public __basic_future<_Res&>
{ {
friend class promise<_Res&>; friend class promise<_Res&>;
template<typename> friend class packaged_task;
template<typename _Fn, typename... _Args>
friend future<typename _Fn::result_type>
async(launch, _Fn&&, _Args&&...);
typedef __basic_future<_Res&> _Base_type; typedef __basic_future<_Res&> _Base_type;
typedef typename _Base_type::__state_type __state_type; typedef typename _Base_type::__state_type __state_type;
explicit explicit
unique_future(const __state_type& __state) : _Base_type(__state) { } future(const __state_type& __state) : _Base_type(__state) { }
public: public:
future() : _Base_type() { }
/// Move constructor /// Move constructor
unique_future(unique_future&& __uf) : _Base_type(std::move(__uf)) { } future(future&& __uf) : _Base_type(std::move(__uf)) { }
// Disable copying // Disable copying
unique_future(const unique_future&) = delete; future(const future&) = delete;
unique_future& operator=(const unique_future&) = delete; future& operator=(const future&) = delete;
future& operator=(future&& __fut)
{
future(std::move(__fut))._M_swap(*this);
return *this;
}
/// Retrieving the value /// Retrieving the value
_Res& _Res&
get() { return *this->_M_get_result()._M_value_ptr; } get()
{
typename _Base_type::_Reset __reset(*this);
return this->_M_get_result()._M_get();
}
}; };
/// Explicit specialization for unique_future<void> /// Explicit specialization for future<void>
template<> template<>
class unique_future<void> : public __basic_future<void> class future<void> : public __basic_future<void>
{ {
friend class promise<void>; friend class promise<void>;
template<typename> friend class packaged_task;
template<typename _Fn, typename... _Args>
friend future<typename _Fn::result_type>
async(launch, _Fn&&, _Args&&...);
typedef __basic_future<void> _Base_type; typedef __basic_future<void> _Base_type;
typedef typename _Base_type::__state_type __state_type; typedef typename _Base_type::__state_type __state_type;
explicit explicit
unique_future(const __state_type& __state) : _Base_type(__state) { } future(const __state_type& __state) : _Base_type(__state) { }
public: public:
future() : _Base_type() { }
/// Move constructor /// Move constructor
unique_future(unique_future&& __uf) : _Base_type(std::move(__uf)) { } future(future&& __uf) : _Base_type(std::move(__uf)) { }
// Disable copying // Disable copying
unique_future(const unique_future&) = delete; future(const future&) = delete;
unique_future& operator=(const unique_future&) = delete; future& operator=(const future&) = delete;
future& operator=(future&& __fut)
{
future(std::move(__fut))._M_swap(*this);
return *this;
}
/// Retrieving the value /// Retrieving the value
void void
get() { this->_M_get_result(); } get()
{
typename _Base_type::_Reset __reset(*this);
this->_M_get_result();
}
}; };
...@@ -482,15 +682,32 @@ namespace std ...@@ -482,15 +682,32 @@ namespace std
typedef __basic_future<_Res> _Base_type; typedef __basic_future<_Res> _Base_type;
public: public:
shared_future() : _Base_type() { }
/// Copy constructor /// Copy constructor
shared_future(const shared_future& __sf) : _Base_type(__sf) { } shared_future(const shared_future& __sf) : _Base_type(__sf) { }
/// Construct from a unique_future rvalue /// Construct from a future rvalue
shared_future(unique_future<_Res>&& __uf) shared_future(future<_Res>&& __uf)
: _Base_type(std::move(__uf)) : _Base_type(std::move(__uf))
{ } { }
shared_future& operator=(const shared_future&) = delete; /// Construct from a shared_future rvalue
shared_future(shared_future&& __sf)
: _Base_type(std::move(__sf))
{ }
shared_future& operator=(const shared_future& __sf)
{
shared_future(__sf)._M_swap(*this);
return *this;
}
shared_future& operator=(shared_future&& __sf)
{
shared_future(std::move(__sf))._M_swap(*this);
return *this;
}
/// Retrieving the value /// Retrieving the value
const _Res& const _Res&
...@@ -509,19 +726,36 @@ namespace std ...@@ -509,19 +726,36 @@ namespace std
typedef __basic_future<_Res&> _Base_type; typedef __basic_future<_Res&> _Base_type;
public: public:
shared_future() : _Base_type() { }
/// Copy constructor /// Copy constructor
shared_future(const shared_future& __sf) : _Base_type(__sf) { } shared_future(const shared_future& __sf) : _Base_type(__sf) { }
/// Construct from a unique_future rvalue /// Construct from a future rvalue
shared_future(unique_future<_Res&>&& __uf) shared_future(future<_Res&>&& __uf)
: _Base_type(std::move(__uf)) : _Base_type(std::move(__uf))
{ } { }
shared_future& operator=(const shared_future&) = delete; /// Construct from a shared_future rvalue
shared_future(shared_future&& __sf)
: _Base_type(std::move(__sf))
{ }
shared_future& operator=(const shared_future& __sf)
{
shared_future(__sf)._M_swap(*this);
return *this;
}
shared_future& operator=(shared_future&& __sf)
{
shared_future(std::move(__sf))._M_swap(*this);
return *this;
}
/// Retrieving the value /// Retrieving the value
_Res& _Res&
get() { return *this->_M_get_result()._M_value_ptr; } get() { return this->_M_get_result()._M_get(); }
}; };
/// Explicit specialization for shared_future<void> /// Explicit specialization for shared_future<void>
...@@ -531,15 +765,32 @@ namespace std ...@@ -531,15 +765,32 @@ namespace std
typedef __basic_future<void> _Base_type; typedef __basic_future<void> _Base_type;
public: public:
shared_future() : _Base_type() { }
/// Copy constructor /// Copy constructor
shared_future(const shared_future& __sf) : _Base_type(__sf) { } shared_future(const shared_future& __sf) : _Base_type(__sf) { }
/// Construct from a unique_future rvalue /// Construct from a future rvalue
shared_future(unique_future<void>&& __uf) shared_future(future<void>&& __uf)
: _Base_type(std::move(__uf)) : _Base_type(std::move(__uf))
{ } { }
shared_future& operator=(const shared_future&) = delete; /// Construct from a shared_future rvalue
shared_future(shared_future&& __sf)
: _Base_type(std::move(__sf))
{ }
shared_future& operator=(const shared_future& __sf)
{
shared_future(__sf)._M_swap(*this);
return *this;
}
shared_future& operator=(shared_future&& __sf)
{
shared_future(std::move(__sf))._M_swap(*this);
return *this;
}
// Retrieving the value // Retrieving the value
void void
...@@ -553,7 +804,12 @@ namespace std ...@@ -553,7 +804,12 @@ namespace std
{ } { }
template<typename _Res> template<typename _Res>
__basic_future<_Res>::__basic_future(unique_future<_Res>&& __uf) __basic_future<_Res>::__basic_future(shared_future<_Res>&& __sf)
: _M_state(std::move(__sf._M_state))
{ }
template<typename _Res>
__basic_future<_Res>::__basic_future(future<_Res>&& __uf)
: _M_state(std::move(__uf._M_state)) : _M_state(std::move(__uf._M_state))
{ } { }
...@@ -562,17 +818,17 @@ namespace std ...@@ -562,17 +818,17 @@ namespace std
template<typename _Res> template<typename _Res>
class promise class promise
{ {
template<typename> friend class packaged_task;
typedef __future_base::_State _State; typedef __future_base::_State _State;
typedef __future_base::_Result<_Res> result_type; typedef __future_base::_Result<_Res> _Res_type;
typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
template<typename, typename> friend class _State::_Setter;
shared_ptr<_State> _M_future; shared_ptr<_State> _M_future;
typename __future_base::_Ptr<result_type>::type _M_storage; _Ptr_type _M_storage;
public: public:
promise() promise()
: _M_future(std::make_shared<_State>()), _M_storage(new result_type()) : _M_future(std::make_shared<_State>()), _M_storage(new _Res_type())
{ } { }
promise(promise&& __rhs) promise(promise&& __rhs)
...@@ -580,13 +836,13 @@ namespace std ...@@ -580,13 +836,13 @@ namespace std
_M_storage(std::move(__rhs._M_storage)) _M_storage(std::move(__rhs._M_storage))
{ } { }
// TODO: requires allocator concepts // TODO: needs allocator_arg_t
/* /*
template<typename _Allocator> template<typename _Allocator>
promise(allocator_arg_t, const _Allocator& __a); promise(allocator_arg_t, const _Allocator& __a)
: _M_future(std::allocate_shared<_State>(__a)),
template<typename _Allocator> _M_storage(__future_base::_S_allocate_result<_Res>(__a))
promise(allocator_arg_t, const _Allocator&, promise&& __rhs); { }
*/ */
promise(const promise&) = delete; promise(const promise&) = delete;
...@@ -615,54 +871,48 @@ namespace std ...@@ -615,54 +871,48 @@ namespace std
} }
// Retrieving the result // Retrieving the result
unique_future<_Res> future<_Res>
get_future() get_future()
{ return unique_future<_Res>(_M_future); } { return future<_Res>(_M_future); }
// Setting the result // Setting the result
void void
set_value(const _Res& __r) set_value(const _Res& __r)
{ {
if (!_M_satisfied()) auto __setter = _State::__setter(this, __r);
_M_storage->_M_set(__r); _M_future->_M_set_result(std::move(__setter));
_M_future->_M_set_result(std::move(_M_storage));
} }
void void
set_value(_Res&& __r) set_value(_Res&& __r)
{ {
if (!_M_satisfied()) auto __setter = _State::__setter(this, std::move(__r));
_M_storage->_M_set(std::move(__r)); _M_future->_M_set_result(std::move(__setter));
_M_future->_M_set_result(std::move(_M_storage));
} }
void void
set_exception(exception_ptr __p) set_exception(exception_ptr __p)
{ {
if (!_M_satisfied()) auto __setter = _State::__setter(__p, this);
_M_storage->_M_error = __p; _M_future->_M_set_result(std::move(__setter));
_M_future->_M_set_result(std::move(_M_storage));
} }
private:
bool _M_satisfied() { return !static_cast<bool>(_M_storage); }
}; };
/// Partial specialization for promise<R&> /// Partial specialization for promise<R&>
template<typename _Res> template<typename _Res>
class promise<_Res&> class promise<_Res&>
{ {
template<typename> friend class packaged_task;
typedef __future_base::_State _State; typedef __future_base::_State _State;
typedef __future_base::_Result<_Res&> _Res_type;
typedef __future_base::_Result<_Res&> result_type; typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
template<typename, typename> friend class _State::_Setter;
shared_ptr<_State> _M_future; shared_ptr<_State> _M_future;
typename __future_base::_Ptr<result_type>::type _M_storage; _Ptr_type _M_storage;
public: public:
promise() promise()
: _M_future(std::make_shared<_State>()), _M_storage(new result_type()) : _M_future(std::make_shared<_State>()), _M_storage(new _Res_type())
{ } { }
promise(promise&& __rhs) promise(promise&& __rhs)
...@@ -670,13 +920,13 @@ namespace std ...@@ -670,13 +920,13 @@ namespace std
_M_storage(std::move(__rhs._M_storage)) _M_storage(std::move(__rhs._M_storage))
{ } { }
// TODO: requires allocator concepts // TODO: needs allocator_arg_t
/* /*
template<typename _Allocator> template<typename _Allocator>
promise(allocator_arg_t, const _Allocator& __a); promise(allocator_arg_t, const _Allocator& __a)
: _M_future(std::allocate_shared<_State>(__a)),
template<typename _Allocator> _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
promise(allocator_arg_t, const _Allocator&, promise&& __rhs); { }
*/ */
promise(const promise&) = delete; promise(const promise&) = delete;
...@@ -705,46 +955,42 @@ namespace std ...@@ -705,46 +955,42 @@ namespace std
} }
// Retrieving the result // Retrieving the result
unique_future<_Res&> future<_Res&>
get_future() get_future()
{ return unique_future<_Res&>(_M_future); } { return future<_Res&>(_M_future); }
// Setting the result // Setting the result
void void
set_value(_Res& __r) set_value(_Res& __r)
{ {
if (!_M_satisfied()) auto __setter = _State::__setter(this, __r);
_M_storage->_M_value_ptr = &__r; _M_future->_M_set_result(std::move(__setter));
_M_future->_M_set_result(std::move(_M_storage));
} }
void void
set_exception(exception_ptr __p) set_exception(exception_ptr __p)
{ {
if (!_M_satisfied()) auto __setter = _State::__setter(__p, this);
_M_storage->_M_error = __p; _M_future->_M_set_result(std::move(__setter));
_M_future->_M_set_result(std::move(_M_storage));
} }
private:
bool _M_satisfied() { return !static_cast<bool>(_M_storage); }
}; };
/// Explicit specialization for promise<void> /// Explicit specialization for promise<void>
template<> template<>
class promise<void> class promise<void>
{ {
template<typename> friend class packaged_task;
typedef __future_base::_State _State; typedef __future_base::_State _State;
typedef __future_base::_Result<void> result_type; typedef __future_base::_Result<void> _Res_type;
typedef typename __future_base::_Ptr<_Res_type>::type _Ptr_type;
template<typename, typename> friend class _State::_Setter;
shared_ptr<__future_base::_State> _M_future; shared_ptr<_State> _M_future;
typename __future_base::_Ptr<result_type>::type _M_storage; _Ptr_type _M_storage;
public: public:
promise() promise()
: _M_future(std::make_shared<_State>()), : _M_future(std::make_shared<_State>()),
_M_storage(new result_type()) _M_storage(new _Res_type())
{ } { }
promise(promise&& __rhs) promise(promise&& __rhs)
...@@ -752,13 +998,14 @@ namespace std ...@@ -752,13 +998,14 @@ namespace std
_M_storage(std::move(__rhs._M_storage)) _M_storage(std::move(__rhs._M_storage))
{ } { }
// TODO: requires allocator concepts
/*
template<typename _Allocator>
promise(allocator_arg_t, const _Allocator& __a);
// TODO: needs allocator_arg_t
/*
template<typename _Allocator> template<typename _Allocator>
promise(allocator_arg_t, const _Allocator&, promise&& __rhs); promise(allocator_arg_t, const _Allocator& __a)
: _M_future(std::allocate_shared<_State>(__a)),
_M_storage(__future_base::_S_allocate_result<void>(__a))
{ }
*/ */
promise(const promise&) = delete; promise(const promise&) = delete;
...@@ -787,69 +1034,137 @@ namespace std ...@@ -787,69 +1034,137 @@ namespace std
} }
// Retrieving the result // Retrieving the result
unique_future<void> future<void>
get_future() get_future()
{ return unique_future<void>(_M_future); } { return future<void>(_M_future); }
// Setting the result // Setting the result
void set_value();
void void
set_value() set_exception(exception_ptr __p)
{ {
_M_future->_M_set_result(std::move(_M_storage)); auto __setter = _State::__setter(__p, this);
_M_future->_M_set_result(std::move(__setter));
} }
};
void // set void
set_exception(exception_ptr __p) template<>
struct __future_base::_State::_Setter<void, void>
{
promise<void>::_Ptr_type operator()()
{ {
if (!_M_satisfied()) _State::_S_check(_M_promise->_M_future);
_M_storage->_M_error = __p; return std::move(_M_promise->_M_storage);
_M_future->_M_set_result(std::move(_M_storage));
} }
private: promise<void>* _M_promise;
bool _M_satisfied() { return !static_cast<bool>(_M_storage); }
}; };
// TODO: requires allocator concepts inline __future_base::_State::_Setter<void, void>
/* __future_base::_State::__setter(promise<void>* __prom)
template<typename _Res, class Alloc> {
concept_map UsesAllocator<promise<_Res>, Alloc> return _Setter<void, void>{ __prom };
}
inline void
promise<void>::set_value()
{ {
typedef Alloc allocator_type; auto __setter = _State::__setter(this);
_M_future->_M_set_result(std::move(__setter));
} }
// TODO: needs allocators
/*
template<typename _Res, class Alloc>
struct uses_allocator<promise<_Res>, Alloc> : true_type { };
*/ */
/// Primary template.
template<typename _Res, typename... _ArgTypes>
struct _Run_task template<typename _StateT, typename _Res>
struct __future_base::_Task_setter
{ {
static void typename _StateT::_Ptr_type operator()()
_S_run(promise<_Res>& __p, function<_Res(_ArgTypes...)>& __f,
_ArgTypes... __args)
{ {
__p.set_value(__f(std::forward<_ArgTypes>(__args)...)); __try {
_M_state->_M_result->_M_set(_M_fn());
} __catch(...) {
_M_state->_M_result->_M_error = current_exception();
} }
return std::move(_M_state->_M_result);
}
_StateT* _M_state;
std::function<_Res()> _M_fn;
}; };
/// Specialization used by packaged_task<void(...)> template<typename _StateT>
template<typename... _ArgTypes> struct __future_base::_Task_setter<_StateT, void>
struct _Run_task<void, _ArgTypes...>
{ {
static void typename _StateT::_Ptr_type operator()()
_S_run(promise<void>& __p, function<void(_ArgTypes...)>& __f,
_ArgTypes... __args)
{ {
__f(std::forward<_ArgTypes>(__args)...); __try {
__p.set_value(); _M_fn();
} __catch(...) {
_M_state->_M_result->_M_error = current_exception();
}
return std::move(_M_state->_M_result);
} }
_StateT* _M_state;
std::function<void()> _M_fn;
}; };
template<typename _Res, typename... _Args>
struct __future_base::_Task_state<_Res(_Args...)> : __future_base::_State
{
typedef _Res _Res_type;
_Task_state(std::function<_Res(_Args...)> __task)
: _M_result(new _Result<_Res>()), _M_task(std::move(__task))
{ }
// TODO: needs allocator_arg_t
/*
template<typename _Func, typename _Alloc>
_Task_state(_Func&& __task, const _Alloc& __a)
: _M_result(_S_allocate_result<_Res>(__a))
, _M_task(allocator_arg, __a, std::move(__task))
{ }
*/
void
_M_run(_Args... __args)
{
// bound arguments decay so wrap lvalue references
auto __bound = std::bind<_Res>(_M_task,
_S_maybe_wrap_ref(std::forward<_Args>(__args))...);
_Task_setter<_Task_state> __setter{ this, std::move(__bound) };
_M_set_result(std::move(__setter));
}
template<typename, typename> friend class _Task_setter;
typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
_Ptr_type _M_result;
std::function<_Res(_Args...)> _M_task;
template<typename _Tp>
static reference_wrapper<_Tp>
_S_maybe_wrap_ref(_Tp& __t)
{ return std::ref(__t); }
template<typename _Tp>
static typename enable_if<!is_lvalue_reference<_Tp>::value,
_Tp>::type&&
_S_maybe_wrap_ref(_Tp&& __t)
{ return std::forward<_Tp>(__t); }
};
/// packaged_task /// packaged_task
template<typename _Res, typename... _ArgTypes> template<typename _Res, typename... _ArgTypes>
class packaged_task<_Res(_ArgTypes...)> class packaged_task<_Res(_ArgTypes...)>
{ {
function<_Res(_ArgTypes...)> _M_task; typedef __future_base::_Task_state<_Res(_ArgTypes...)> _State_type;
promise<_Res> _M_promise; shared_ptr<_State_type> _M_state;
public: public:
typedef _Res result_type; typedef _Res result_type;
...@@ -859,31 +1174,35 @@ namespace std ...@@ -859,31 +1174,35 @@ namespace std
template<typename _Fn> template<typename _Fn>
explicit explicit
packaged_task(const _Fn& __fn) : _M_task(__fn) { } packaged_task(const _Fn& __fn)
: _M_state(std::make_shared<_State_type>(__fn))
{ }
template<typename _Fn> template<typename _Fn>
explicit explicit
packaged_task(_Fn&& __fn) : _M_task(std::move(__fn)) { } packaged_task(_Fn&& __fn)
: _M_state(std::make_shared<_State_type>(std::move(__fn)))
{ }
explicit explicit
packaged_task(_Res(*__fn)(_ArgTypes...)) : _M_task(__fn) { } packaged_task(_Res(*__fn)(_ArgTypes...))
: _M_state(std::make_shared<_State_type>(__fn))
{ }
// TODO: requires allocator concepts // TODO: needs allocator_arg_t
/* /*
template<typename _Fn, typename _Allocator> template<typename _Fn, typename _Allocator>
explicit explicit
packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn) packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn __fn)
: _M_task(__tag, __a, __fn), _M_promise(__tag, __a) : _M_state(std::allocate_shared<_State_type>(__a, std::move(__fn)))
{ }
template<typename _Fn, typename _Allocator>
explicit
packaged_task(allocator_arg_t __tag, const _Allocator& __a, _Fn&& __fn)
: _M_task(__tag, __a, std::move(__fn)), _M_promise(__tag, __a)
{ } { }
*/ */
~packaged_task() = default; ~packaged_task()
{
if (static_cast<bool>(_M_state) && !_M_state.unique())
_M_state->_M_break_promise(std::move(_M_state->_M_result));
}
// No copy // No copy
packaged_task(packaged_task&) = delete; packaged_task(packaged_task&) = delete;
...@@ -901,49 +1220,113 @@ namespace std ...@@ -901,49 +1220,113 @@ namespace std
void void
swap(packaged_task& __other) swap(packaged_task& __other)
{ { _M_state.swap(__other._M_state); }
_M_task.swap(__other._M_task);
_M_promise.swap(__other._M_promise);
}
explicit operator bool() const { return static_cast<bool>(_M_task); } explicit operator bool() const { return static_cast<bool>(_M_state); }
// Result retrieval // Result retrieval
unique_future<_Res> future<_Res>
get_future() get_future()
{ return future<_Res>(_M_state); }
// Execution
void
operator()(_ArgTypes... __args)
{ {
__try __future_base::_State::_S_check(_M_state);
{ _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
return _M_promise.get_future();
} }
__catch (const future_error& __e)
void
reset()
{ {
if (__e.code() == future_errc::future_already_retrieved) __future_base::_State::_S_check(_M_state);
__throw_bad_function_call(); packaged_task(std::move(_M_state->_M_task)).swap(*this);
__throw_exception_again;
} }
};
template<typename _Res>
class __future_base::_Deferred_state : public __future_base::_State
{
public:
typedef _Res _Res_type;
explicit
_Deferred_state(std::function<_Res()>&& __fn)
: _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
{ }
private:
template<typename, typename> friend class _Task_setter;
typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
_Ptr_type _M_result;
std::function<_Res()> _M_fn;
virtual void
_M_run_deferred()
{
_Task_setter<_Deferred_state> __setter{ this, _M_fn };
// safe to call multiple times so ignore failure
_M_set_result(std::move(__setter), true);
} }
};
// Execution template<typename _Res>
void class __future_base::_Async_state : public __future_base::_State
operator()(_ArgTypes... __args)
{ {
if (!static_cast<bool>(_M_task) || _M_promise._M_satisfied()) public:
__throw_bad_function_call(); typedef _Res _Res_type;
__try explicit
_Async_state(std::function<_Res()>&& __fn)
: _M_result(new _Result<_Res>()), _M_fn(std::move(__fn)),
_M_thread(mem_fn(&_Async_state::_M_do_run), this)
{ }
~_Async_state() { _M_thread.join(); }
private:
void _M_do_run()
{ {
_Run_task<_Res, _ArgTypes...>::_S_run(_M_promise, _M_task, _Task_setter<_Async_state> __setter{ this, std::move(_M_fn) };
std::forward<_ArgTypes>(__args)...); _M_set_result(std::move(__setter));
} }
__catch (...)
template<typename, typename> friend class _Task_setter;
typedef typename __future_base::_Ptr<_Result<_Res>>::type _Ptr_type;
_Ptr_type _M_result;
std::function<_Res()> _M_fn;
thread _M_thread;
};
template<typename _Fn, typename... _Args>
future<typename _Fn::result_type>
async(launch __policy, _Fn&& __fn, _Args&&... __args)
{
typedef typename _Fn::result_type result_type;
std::shared_ptr<__future_base::_State> __state;
if (__policy == launch::async)
{
typedef typename __future_base::_Async_state<result_type> _State;
__state = std::make_shared<_State>(std::bind<result_type>(
std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
}
else
{ {
_M_promise.set_exception(current_exception()); typedef typename __future_base::_Deferred_state<result_type> _State;
__state = std::make_shared<_State>(std::bind<result_type>(
std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
} }
return future<result_type>(__state);
} }
void reset() { promise<_Res>().swap(_M_promise); } template<typename _Fn, typename... _Args>
}; future<typename _Fn::result_type>
async(_Fn&& __fn, _Args&&... __args)
{
return async(launch::any, std::forward<_Fn>(__fn),
std::forward<_Args>(__args)...);
}
#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
// && _GLIBCXX_ATOMIC_BUILTINS_4 // && _GLIBCXX_ATOMIC_BUILTINS_4
......
...@@ -103,7 +103,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) ...@@ -103,7 +103,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
void void
__throw_future_error(int __i) __throw_future_error(int __i)
{ throw future_error(future_errc(__i)); } { throw future_error(make_error_code(future_errc(__i))); }
void void
__throw_bad_function_call() __throw_bad_function_call()
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
// { dg-require-gthreads "" } // { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" } // { dg-require-atomic-builtins "" }
// Copyright (C) 2009 Free Software Foundation, Inc. // Copyright (C) 2010 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -27,44 +27,32 @@ ...@@ -27,44 +27,32 @@
#include <future> #include <future>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
void test01() struct sum {
{ typedef int result_type;
bool test __attribute__((unused)) = true; int operator()(int i, int& j, const int& k) { return i + j + k; }
};
std::promise<int> p1;
std::shared_future<int> f1(p1.get_future());
std::shared_future<int> f2(f1);
VERIFY( !f1.has_exception() );
VERIFY( !f2.has_exception() );
p1.set_exception(std::copy_exception(1));
VERIFY( f1.has_exception() ); void test01()
VERIFY( f2.has_exception() );
}
void test02()
{ {
std::promise<int> p1;
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::shared_future<int> f1(p1.get_future()); using namespace std;
std::shared_future<int> f2(f1);
VERIFY( !f1.has_exception() ); int a = 1;
VERIFY( !f2.has_exception() ); int b = 10;
int c = 100;
future<int> f1 = async(launch::any, sum(), a, ref(b), cref(c));
future<int> f2 = async(sum(), a, ref(b), cref(c));
p1.set_value(1); VERIFY( f1.valid() );
VERIFY( f2.valid() );
VERIFY( !f1.has_exception() ); int r1 = f1.get();
VERIFY( !f2.has_exception() ); int r2 = f2.get();
VERIFY( r1 == r2 );
} }
int main() int main()
{ {
test01(); test01();
test02();
return 0; return 0;
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
// { dg-require-gthreads "" } // { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" } // { dg-require-atomic-builtins "" }
// Copyright (C) 2009 Free Software Foundation, Inc. // Copyright (C) 2010 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -27,38 +27,31 @@ ...@@ -27,38 +27,31 @@
#include <future> #include <future>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
void test01() using namespace std;
{
bool test __attribute__((unused)) = true;
std::promise<int> p1;
std::unique_future<int> f1(p1.get_future());
VERIFY( !f1.has_exception() );
p1.set_exception(std::copy_exception(1));
VERIFY( f1.has_exception() ); struct work {
} typedef void result_type;
void operator()(mutex& m, condition_variable& cv)
{
unique_lock<mutex> l(m);
cv.notify_one();
}
};
void test02() void test01()
{ {
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int> p1; mutex m;
std::unique_future<int> f1(p1.get_future()); condition_variable cv;
unique_lock<mutex> l(m);
VERIFY( !f1.has_exception() ); future<void> f1 = async(launch::async, work(), ref(m), ref(cv));
cv.wait(l);
p1.set_value(1); f1.get();
VERIFY( !f1.has_exception() );
} }
int main() int main()
{ {
test01(); test01();
test02();
return 0; return 0;
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
// { dg-require-gthreads "" } // { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" } // { dg-require-atomic-builtins "" }
// Copyright (C) 2009 Free Software Foundation, Inc. // Copyright (C) 2010 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -27,39 +27,28 @@ ...@@ -27,39 +27,28 @@
#include <future> #include <future>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
void test01() struct sum {
{ typedef int result_type;
bool test __attribute__((unused)) = true; int operator()(int i, int& j, const int& k) { return i + j + k; }
};
std::promise<int> p1;
std::unique_future<int> f1(p1.get_future());
VERIFY( !f1.has_value() );
p1.set_value(1);
VERIFY( f1.has_value() ); void test01()
}
void test02()
{ {
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int> p1; using namespace std;
std::unique_future<int> f1(p1.get_future());
VERIFY( !f1.has_value() ); int a = 1;
int b = 10;
int c = 100;
future<int> f1 = async(launch::sync, sum(), a, ref(b), cref(c));
p1.set_exception(std::copy_exception(1)); VERIFY( f1.valid() );
VERIFY( f1.get() == 111 );
VERIFY( !f1.has_value() );
} }
int main() int main()
{ {
test01(); test01();
test02();
return 0; return 0;
} }
...@@ -31,9 +31,9 @@ void test01() ...@@ -31,9 +31,9 @@ void test01()
typedef std::future_error error_t; typedef std::future_error error_t;
typedef std::unique_future<int> uniq_t; typedef std::future<int> uniq_t;
typedef std::unique_future<int&> uniqr_t; typedef std::future<int&> uniqr_t;
typedef std::unique_future<void> uniqv_t; typedef std::future<void> uniqv_t;
typedef std::shared_future<int> shar_t; typedef std::shared_future<int> shar_t;
typedef std::shared_future<int&> sharr_t; typedef std::shared_future<int&> sharr_t;
...@@ -46,4 +46,6 @@ void test01() ...@@ -46,4 +46,6 @@ void test01()
typedef std::packaged_task<int> ptask_t; typedef std::packaged_task<int> ptask_t;
typedef std::packaged_task<int&> ptaskr_t; typedef std::packaged_task<int&> ptaskr_t;
typedef std::packaged_task<void> ptaskv_t; typedef std::packaged_task<void> ptaskv_t;
using std::async;
} }
// { dg-do compile { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } }
// { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" }
// Copyright (C) 2010 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <future>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
int f() { return 5; }
void test01()
{
bool test __attribute__((unused)) = true;
using std::packaged_task;
using std::allocator_arg;
using __gnu_test::uneq_allocator;
uneq_allocator<char> alloc(99);
packaged_task<int ()> p1(allocator_arg, alloc, f); // { dg-excess-errors "" }
VERIFY( static_cast<bool>(p1) );
p1();
VERIFY( p1.get_future().get() == 5 );
}
int main()
{
test01();
return 0;
}
...@@ -33,4 +33,4 @@ void test01() ...@@ -33,4 +33,4 @@ void test01()
} }
// { dg-error "used here" "" { target *-*-* } 32 } // { dg-error "used here" "" { target *-*-* } 32 }
// { dg-error "deleted function" "" { target *-*-* } 890 } // { dg-error "deleted function" "" { target *-*-* } 1209 }
...@@ -32,4 +32,4 @@ void test01() ...@@ -32,4 +32,4 @@ void test01()
} }
// { dg-error "used here" "" { target *-*-* } 31 } // { dg-error "used here" "" { target *-*-* } 31 }
// { dg-error "deleted function" "" { target *-*-* } 889 } // { dg-error "deleted function" "" { target *-*-* } 1208 }
...@@ -34,9 +34,10 @@ void test01() ...@@ -34,9 +34,10 @@ void test01()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::packaged_task<int&(int&)> p1(inc); std::packaged_task<int&(int&)> p1(inc);
std::unique_future<int&> f1 = p1.get_future(); std::future<int&> f1 = p1.get_future();
VERIFY( !f1.is_ready() ); VERIFY( f1.valid() );
VERIFY( !f1.wait_for(std::chrono::milliseconds(1)) );
int i1 = 0; int i1 = 0;
......
...@@ -42,8 +42,9 @@ void test01() ...@@ -42,8 +42,9 @@ void test01()
p1.get_future(); p1.get_future();
VERIFY( false ); VERIFY( false );
} }
catch (std::bad_function_call&) catch (const std::future_error& e)
{ {
VERIFY( e.code() == std::future_errc::future_already_retrieved );
test = true; test = true;
} }
......
...@@ -34,12 +34,12 @@ void test01() ...@@ -34,12 +34,12 @@ void test01()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::packaged_task<int()> p1(zero); std::packaged_task<int()> p1(zero);
std::unique_future<int> f1 = p1.get_future(); std::future<int> f1 = p1.get_future();
p1(); p1();
VERIFY( static_cast<bool>(p1) ); VERIFY( static_cast<bool>(p1) );
VERIFY( f1.has_value() ); VERIFY( f1.get() == 0 );
} }
int main() int main()
......
...@@ -41,8 +41,9 @@ void test01() ...@@ -41,8 +41,9 @@ void test01()
{ {
p1(4); p1(4);
} }
catch (std::bad_function_call&) catch (const std::future_error& e)
{ {
VERIFY( e.code() == std::future_errc::promise_already_satisfied );
test = true; test = true;
} }
......
...@@ -44,8 +44,9 @@ void test01() ...@@ -44,8 +44,9 @@ void test01()
{ {
p1(i1); p1(i1);
} }
catch (std::bad_function_call&) catch (const std::future_error& e)
{ {
VERIFY( e.code() == std::future_errc::promise_already_satisfied );
test = true; test = true;
} }
......
...@@ -31,14 +31,19 @@ void thrower() { throw 0; } ...@@ -31,14 +31,19 @@ void thrower() { throw 0; }
void test01() void test01()
{ {
bool test __attribute__((unused)) = true; bool test = false;
std::packaged_task<void()> p1(thrower); std::packaged_task<void()> p1(thrower);
std::unique_future<void> f1 = p1.get_future(); std::future<void> f1 = p1.get_future();
p1(); p1();
VERIFY( f1.has_exception() ); try {
f1.get();
} catch (int) {
test = true;
}
VERIFY( test );
} }
int main() int main()
......
...@@ -36,15 +36,13 @@ void test01() ...@@ -36,15 +36,13 @@ void test01()
using namespace std; using namespace std;
packaged_task<int()> p1(zero); packaged_task<int()> p1(zero);
unique_future<int> f1 = p1.get_future(); future<int> f1 = p1.get_future();
p1.reset(); p1.reset();
VERIFY( static_cast<bool>(p1) ); VERIFY( static_cast<bool>(p1) );
unique_future<int> f2 = p1.get_future(); future<int> f2 = p1.get_future();
VERIFY( !f2.is_ready() );
VERIFY( f1.has_exception() );
try try
{ {
f1.get(); f1.get();
......
...@@ -27,23 +27,24 @@ ...@@ -27,23 +27,24 @@
#include <future> #include <future>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
int zero() { return 0; } int iota() { static int i = 0; return i++; }
void test01() void test01()
{ {
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::packaged_task<int()> p1(zero); std::packaged_task<int()> p1(iota);
std::unique_future<int> f1 = p1.get_future(); std::future<int> f1 = p1.get_future();
p1(); p1();
p1.reset(); p1.reset();
VERIFY( static_cast<bool>(p1) ); VERIFY( static_cast<bool>(p1) );
VERIFY( f1.has_value() ); VERIFY( f1.get() == 0 );
std::unique_future<int> f2 = p1.get_future(); std::future<int> f2 = p1.get_future();
VERIFY( !f2.is_ready() ); p1();
VERIFY( f2.get() == 1 );
} }
int main() int main()
......
// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } } // { dg-do compile { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } } // { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
// { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } } // { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } }
// { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } } // { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
// { dg-require-gthreads "" } // { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" } // { dg-require-atomic-builtins "" }
// Copyright (C) 2009 Free Software Foundation, Inc. // Copyright (C) 2010 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -23,48 +23,25 @@ ...@@ -23,48 +23,25 @@
// with this library; see the file COPYING3. If not see // with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
#include <future> #include <future>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
#include <testsuite_allocator.h>
void test01() void test01()
{ {
bool test __attribute__((unused)) = true; using std::promise;
using std::allocator_arg;
std::promise<int> p1; using __gnu_test::uneq_allocator;
std::shared_future<int> f1(p1.get_future());
std::shared_future<int> f2(f1);
VERIFY( !f1.has_value() );
VERIFY( !f2.has_value() );
p1.set_value(1); uneq_allocator<char> alloc(99);
VERIFY( f1.has_value() ); promise<int> p1(allocator_arg, alloc); // { dg-excess-errors "" }
VERIFY( f2.has_value() ); p1.set_value(5);
} VERIFY( p1.get_future().get() == 5 );
void test02()
{
bool test __attribute__((unused)) = true;
std::promise<int> p1;
std::shared_future<int> f1(p1.get_future());
std::shared_future<int> f2(f1);
VERIFY( !f1.has_value() );
VERIFY( !f2.has_value() );
p1.set_exception(std::copy_exception(1));
VERIFY( !f1.has_value() );
VERIFY( !f2.has_value() );
} }
int main() int main()
{ {
test01(); test01();
test02();
return 0; return 0;
} }
...@@ -33,4 +33,4 @@ void test01() ...@@ -33,4 +33,4 @@ void test01()
} }
// { dg-error "used here" "" { target *-*-* } 32 } // { dg-error "used here" "" { target *-*-* } 32 }
// { dg-error "deleted function" "" { target *-*-* } 608 } // { dg-error "deleted function" "" { target *-*-* } 864 }
...@@ -32,4 +32,4 @@ void test01() ...@@ -32,4 +32,4 @@ void test01()
} }
// { dg-error "used here" "" { target *-*-* } 31 } // { dg-error "used here" "" { target *-*-* } 31 }
// { dg-error "deleted function" "" { target *-*-* } 592 } // { dg-error "deleted function" "" { target *-*-* } 848 }
...@@ -44,7 +44,7 @@ void test01() ...@@ -44,7 +44,7 @@ void test01()
} }
catch (std::future_error& e) catch (std::future_error& e)
{ {
VERIFY(e.code() == make_error_code(future_errc::future_already_retrieved)); VERIFY(e.code() == make_error_code(future_errc::no_state));
} }
} }
......
...@@ -45,7 +45,7 @@ void test01() ...@@ -45,7 +45,7 @@ void test01()
} }
catch (future_error& e) catch (future_error& e)
{ {
VERIFY(e.code() == make_error_code(future_errc::future_already_retrieved)); VERIFY(e.code() == make_error_code(future_errc::no_state));
} }
} }
......
...@@ -32,9 +32,9 @@ void test01() ...@@ -32,9 +32,9 @@ void test01()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int&> p1; std::promise<int&> p1;
std::unique_future<int&> f1 = p1.get_future(); std::future<int&> f1 = p1.get_future();
VERIFY( !f1.is_ready() ); VERIFY( f1.valid() );
int i1 = 0; int i1 = 0;
......
...@@ -29,17 +29,25 @@ ...@@ -29,17 +29,25 @@
void test01() void test01()
{ {
bool test __attribute__((unused)) = true; bool test = false;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1 = p1.get_future(); std::future<int> f1 = p1.get_future();
VERIFY( !f1.is_ready() ); VERIFY( f1.valid() );
p1.set_exception(std::copy_exception(0)); p1.set_exception(std::copy_exception(0));
VERIFY( f1.has_exception() ); try
VERIFY( !f1.has_value() ); {
f1.get();
}
catch (int)
{
test = true;
}
VERIFY( test );
VERIFY( !f1.valid() );
} }
int main() int main()
......
...@@ -32,7 +32,7 @@ void test01() ...@@ -32,7 +32,7 @@ void test01()
bool test = false; bool test = false;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1 = p1.get_future(); std::future<int> f1 = p1.get_future();
p1.set_exception(std::copy_exception(0)); p1.set_exception(std::copy_exception(0));
...@@ -66,7 +66,7 @@ void test02() ...@@ -66,7 +66,7 @@ void test02()
bool test = false; bool test = false;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1 = p1.get_future(); std::future<int> f1 = p1.get_future();
p1.set_value(2); p1.set_value(2);
...@@ -82,8 +82,6 @@ void test02() ...@@ -82,8 +82,6 @@ void test02()
test = true; test = true;
} }
VERIFY( f1.has_value() );
VERIFY( !f1.has_exception() );
VERIFY( test ); VERIFY( test );
} }
......
...@@ -33,9 +33,9 @@ void test01() ...@@ -33,9 +33,9 @@ void test01()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1 = p1.get_future(); std::future<int> f1 = p1.get_future();
VERIFY( !f1.is_ready() ); VERIFY( f1.valid() );
p1.set_value(0); p1.set_value(0);
...@@ -50,15 +50,15 @@ void test02() ...@@ -50,15 +50,15 @@ void test02()
using __gnu_test::rvalstruct; using __gnu_test::rvalstruct;
std::promise<rvalstruct> p1; std::promise<rvalstruct> p1;
std::unique_future<rvalstruct> f1 = p1.get_future(); std::future<rvalstruct> f1 = p1.get_future();
VERIFY( !f1.is_ready() ); VERIFY( f1.valid() );
p1.set_value(rvalstruct(1)); p1.set_value(rvalstruct(1));
rvalstruct r1(f1.get()); rvalstruct r1(f1.get());
VERIFY( r1.valid ); VERIFY( !f1.valid() );
VERIFY( r1.val == 1 ); VERIFY( r1.val == 1 );
} }
...@@ -68,14 +68,15 @@ void test03() ...@@ -68,14 +68,15 @@ void test03()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int&> p1; std::promise<int&> p1;
std::unique_future<int&> f1 = p1.get_future(); std::future<int&> f1 = p1.get_future();
VERIFY( !f1.is_ready() ); VERIFY( f1.valid() );
int i1 = 0; int i1 = 0;
p1.set_value(i1); p1.set_value(i1);
int& i2 = f1.get(); int& i2 = f1.get();
VERIFY( !f1.valid() );
VERIFY( &i1 == &i2 ); VERIFY( &i1 == &i2 );
} }
...@@ -84,14 +85,14 @@ void test04() ...@@ -84,14 +85,14 @@ void test04()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<void> p1; std::promise<void> p1;
std::unique_future<void> f1 = p1.get_future(); std::future<void> f1 = p1.get_future();
VERIFY( !f1.is_ready() ); VERIFY( f1.valid() );
p1.set_value(); p1.set_value();
f1.get(); f1.get();
VERIFY( f1.is_ready() ); VERIFY( !f1.valid() );
} }
int main() int main()
......
...@@ -32,7 +32,7 @@ void test01() ...@@ -32,7 +32,7 @@ void test01()
bool test = false; bool test = false;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1 = p1.get_future(); std::future<int> f1 = p1.get_future();
p1.set_value(1); p1.set_value(1);
...@@ -48,7 +48,7 @@ void test01() ...@@ -48,7 +48,7 @@ void test01()
test = true; test = true;
} }
VERIFY( f1.has_value() ); VERIFY( f1.wait_for(std::chrono::milliseconds(1)) );
VERIFY( f1.get() == 1 ); VERIFY( f1.get() == 1 );
VERIFY( test ); VERIFY( test );
} }
...@@ -58,7 +58,7 @@ void test02() ...@@ -58,7 +58,7 @@ void test02()
bool test = false; bool test = false;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1 = p1.get_future(); std::future<int> f1 = p1.get_future();
p1.set_value(3); p1.set_value(3);
...@@ -74,8 +74,7 @@ void test02() ...@@ -74,8 +74,7 @@ void test02()
test = true; test = true;
} }
VERIFY( f1.has_value() ); VERIFY( f1.wait_for(std::chrono::milliseconds(1)) );
VERIFY( !f1.has_exception() );
VERIFY( f1.get() == 3 ); VERIFY( f1.get() == 3 );
VERIFY( test ); VERIFY( test );
} }
......
...@@ -41,33 +41,33 @@ struct tester ...@@ -41,33 +41,33 @@ struct tester
}; };
std::promise<tester> pglobal; std::promise<tester> pglobal;
std::unique_future<tester> fglobal = pglobal.get_future(); std::future<tester> fglobal = pglobal.get_future();
tester::tester(int) tester::tester(int)
{ {
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
VERIFY (!fglobal.is_ready()); VERIFY (!fglobal.wait_for(std::chrono::milliseconds(1)));
} }
tester::tester(const tester&) tester::tester(const tester&)
{ {
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
// if this copy happens while a mutex is locked next line could deadlock: // if this copy happens while a mutex is locked next line could deadlock:
VERIFY (!fglobal.is_ready()); VERIFY (!fglobal.wait_for(std::chrono::milliseconds(1)));
} }
tester& tester::operator=(const tester&) tester& tester::operator=(const tester&)
{ {
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
// if this copy happens while a mutex is locked next line could deadlock: // if this copy happens while a mutex is locked next line could deadlock:
VERIFY (!fglobal.is_ready()); VERIFY (!fglobal.wait_for(std::chrono::milliseconds(1)));
return *this; return *this;
} }
tester::~tester() tester::~tester()
{ {
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
VERIFY (fglobal.is_ready()); VERIFY (fglobal.wait_for(std::chrono::milliseconds(1)));
} }
void test01() void test01()
...@@ -76,7 +76,7 @@ void test01() ...@@ -76,7 +76,7 @@ void test01()
pglobal.set_value( tester(1) ); pglobal.set_value( tester(1) );
VERIFY( fglobal.is_ready() ); VERIFY( fglobal.wait_for(std::chrono::milliseconds(1)) );
} }
int main() int main()
......
...@@ -35,8 +35,8 @@ void test01() ...@@ -35,8 +35,8 @@ void test01()
std::promise<int> p2; std::promise<int> p2;
p1.set_value(1); p1.set_value(1);
p1.swap(p2); p1.swap(p2);
VERIFY( !p1.get_future().is_ready() ); VERIFY( !p1.get_future().wait_for(std::chrono::milliseconds(1)) );
VERIFY( p2.get_future().is_ready() ); VERIFY( p2.get_future().wait_for(std::chrono::milliseconds(1)) );
} }
int main() int main()
......
// { dg-do compile }
// { dg-options "-std=gnu++0x" } // { dg-options "-std=gnu++0x" }
// { dg-require-cstdint "" } // { dg-require-cstdint "" }
// { dg-require-gthreads "" } // { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" } // { dg-require-atomic-builtins "" }
// Copyright (C) 2009 Free Software Foundation, Inc. // Copyright (C) 2010 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -23,16 +22,23 @@ ...@@ -23,16 +22,23 @@
#include <future> #include <future>
#include <testsuite_hooks.h>
extern std::shared_future<int>& get(); std::future<int> get() { return std::promise<int>().get_future(); }
void test01() void test01()
{ {
// assign // assign
std::shared_future<int>& p1 = get(); std::shared_future<int> p1;
std::shared_future<int>& p2 = get(); std::shared_future<int> p2 = get();
p1 = p2; p1 = p2;
VERIFY( p1.valid() );
VERIFY( p2.valid() );
}
int main()
{
test01();
return 0;
} }
// { dg-error "used here" "" { target *-*-* } 34 }
// { dg-error "deleted function" "" { target *-*-* } 493 }
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#include <future> #include <future>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
extern std::unique_future<int>&& get(); extern std::future<int>&& get();
void test01() void test01()
{ {
......
// { dg-do compile }
// { dg-options "-std=gnu++0x" } // { dg-options "-std=gnu++0x" }
// { dg-require-cstdint "" } // { dg-require-cstdint "" }
// { dg-require-gthreads "" } // { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" } // { dg-require-atomic-builtins "" }
// Copyright (C) 2009 Free Software Foundation, Inc. // Copyright (C) 2010 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -23,6 +22,7 @@ ...@@ -23,6 +22,7 @@
#include <future> #include <future>
#include <testsuite_hooks.h>
#include <testsuite_tr1.h> #include <testsuite_tr1.h>
void test01() void test01()
...@@ -30,11 +30,18 @@ void test01() ...@@ -30,11 +30,18 @@ void test01()
using std::shared_future; using std::shared_future;
using namespace __gnu_test; using namespace __gnu_test;
shared_future<int> p1; // { dg-error "22: error: no match" } bool __attribute__((unused)) test = true;
shared_future<int&> p2; // { dg-error "23: error: no match" }
shared_future<void> p3; // { dg-error "23: error: no match" } shared_future<int> p1;
shared_future<ClassType> p4; // { dg-error "28: error: no match" } VERIFY( !p1.valid() );
shared_future<AbstractClass&> p5; // { dg-error "33: error: no match" } shared_future<int&> p2;
VERIFY( !p2.valid() );
shared_future<void> p3;
VERIFY( !p3.valid() );
shared_future<ClassType> p4;
VERIFY( !p4.valid() );
shared_future<AbstractClass&> p5;
VERIFY( !p5.valid() );
} }
int main() int main()
...@@ -42,4 +49,3 @@ int main() ...@@ -42,4 +49,3 @@ int main()
test01(); test01();
return 0; return 0;
} }
// { dg-excess-errors "note" }
...@@ -28,9 +28,9 @@ ...@@ -28,9 +28,9 @@
void test01() void test01()
{ {
// construct from rvalue unique_future // construct from rvalue future
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1(p1.get_future()); std::future<int> f1(p1.get_future());
std::shared_future<int> f2(std::move(f1)); std::shared_future<int> f2(std::move(f1));
} }
......
// { dg-do compile }
// { dg-options "-std=gnu++0x" } // { dg-options "-std=gnu++0x" }
// { dg-require-cstdint "" } // { dg-require-cstdint "" }
// { dg-require-gthreads "" } // { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" } // { dg-require-atomic-builtins "" }
// Copyright (C) 2009 Free Software Foundation, Inc. // Copyright (C) 2010 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -23,18 +22,18 @@ ...@@ -23,18 +22,18 @@
#include <future> #include <future>
#include <testsuite_tr1.h> #include <testsuite_hooks.h>
std::future<int> get() { return std::promise<int>().get_future(); }
void test01() void test01()
{ {
using std::unique_future; // assign
using namespace __gnu_test; std::shared_future<int> p1;
std::shared_future<int> p2 = get();
unique_future<int> p1; // { dg-error "22: error: no match" } p1 = std::move(p2);
unique_future<int&> p2; // { dg-error "23: error: no match" } VERIFY( p1.valid() );
unique_future<void> p3; // { dg-error "23: error: no match" } VERIFY( !p2.valid() );
unique_future<ClassType> p4; // { dg-error "28: error: no match" }
unique_future<AbstractClass&> p5; // { dg-error "33: error: no match" }
} }
int main() int main()
...@@ -42,4 +41,3 @@ int main() ...@@ -42,4 +41,3 @@ int main()
test01(); test01();
return 0; return 0;
} }
// { dg-excess-errors "note" }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
// { dg-require-gthreads "" } // { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" } // { dg-require-atomic-builtins "" }
// Copyright (C) 2009 Free Software Foundation, Inc. // Copyright (C) 2010 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -31,17 +31,27 @@ void test01() ...@@ -31,17 +31,27 @@ void test01()
{ {
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::shared_future<int> f0;
VERIFY( !f0.valid() );
std::promise<int> p1; std::promise<int> p1;
std::shared_future<int> f1(p1.get_future()); std::shared_future<int> f1(p1.get_future());
std::shared_future<int> f2(f1); std::shared_future<int> f2(f1);
VERIFY( !f1.is_ready() ); VERIFY( f1.valid() );
VERIFY( !f2.is_ready() ); VERIFY( f2.valid() );
p1.set_value(1); p1.set_value(1);
VERIFY( f1.is_ready() ); VERIFY( f1.valid() );
VERIFY( f2.is_ready() ); VERIFY( f2.valid() );
f1 = std::move(f0);
VERIFY( !f0.valid() );
VERIFY( !f1.valid() );
VERIFY( f2.valid() );
} }
int main() int main()
......
...@@ -24,15 +24,15 @@ ...@@ -24,15 +24,15 @@
#include <future> #include <future>
extern std::unique_future<int>& get(); extern std::future<int>& get();
void test01() void test01()
{ {
// assign // assign
std::unique_future<int>& p1 = get(); std::future<int>& p1 = get();
std::unique_future<int>& p2 = get(); std::future<int>& p2 = get();
p1 = p2; p1 = p2;
} }
// { dg-error "used here" "" { target *-*-* } 34 } // { dg-error "used here" "" { target *-*-* } 34 }
// { dg-error "deleted function" "" { target *-*-* } 419 } // { dg-error "deleted function" "" { target *-*-* } 578 }
...@@ -24,14 +24,14 @@ ...@@ -24,14 +24,14 @@
#include <future> #include <future>
extern std::unique_future<int>& get(); extern std::future<int>& get();
void test01() void test01()
{ {
// copy // copy
std::unique_future<int>& p1 = get(); std::future<int>& p1 = get();
std::unique_future<int> p2(p1); std::future<int> p2(p1);
} }
// { dg-error "used here" "" { target *-*-* } 33 } // { dg-error "used here" "" { target *-*-* } 33 }
// { dg-error "deleted function" "" { target *-*-* } 418 } // { dg-error "deleted function" "" { target *-*-* } 577 }
// { dg-options "-std=gnu++0x" }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" }
// Copyright (C) 2010 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <future>
#include <testsuite_hooks.h>
#include <testsuite_tr1.h>
void test01()
{
using std::future;
using namespace __gnu_test;
bool __attribute__((unused)) test = true;
future<int> p1;
VERIFY( !p1.valid() );
future<int&> p2;
VERIFY( !p2.valid() );
future<void> p3;
VERIFY( !p3.valid() );
future<ClassType> p4;
VERIFY( !p4.valid() );
future<AbstractClass&> p5;
VERIFY( !p5.valid() );
}
int main()
{
test01();
return 0;
}
...@@ -30,8 +30,8 @@ void test01() ...@@ -30,8 +30,8 @@ void test01()
{ {
// move // move
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1(p1.get_future()); std::future<int> f1(p1.get_future());
std::unique_future<int> f2(std::move(f1)); std::future<int> f2(std::move(f1));
} }
int main() int main()
......
// { dg-options "-std=gnu++0x" }
// { dg-require-cstdint "" }
// { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" }
// Copyright (C) 2010 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <future>
#include <testsuite_hooks.h>
std::future<int> get() { return std::promise<int>().get_future(); }
void test01()
{
// assign
std::future<int> p1;
std::future<int> p2 = get();
p1 = std::move(p2);
VERIFY( p1.valid() );
VERIFY( !p2.valid() );
}
int main()
{
test01();
return 0;
}
...@@ -34,10 +34,11 @@ void test01() ...@@ -34,10 +34,11 @@ void test01()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1(p1.get_future()); std::future<int> f1(p1.get_future());
p1.set_value(value); p1.set_value(value);
VERIFY( f1.get() == value ); VERIFY( f1.get() == value );
VERIFY( !f1.valid() );
} }
void test02() void test02()
...@@ -45,19 +46,21 @@ void test02() ...@@ -45,19 +46,21 @@ void test02()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int&> p1; std::promise<int&> p1;
std::unique_future<int&> f1(p1.get_future()); std::future<int&> f1(p1.get_future());
p1.set_value(value); p1.set_value(value);
VERIFY( &f1.get() == &value ); VERIFY( &f1.get() == &value );
VERIFY( !f1.valid() );
} }
void test03() void test03()
{ {
std::promise<void> p1; std::promise<void> p1;
std::unique_future<void> f1(p1.get_future()); std::future<void> f1(p1.get_future());
p1.set_value(); p1.set_value();
f1.get(); f1.get();
VERIFY( !f1.valid() );
} }
int main() int main()
......
...@@ -35,7 +35,7 @@ void test01() ...@@ -35,7 +35,7 @@ void test01()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1(p1.get_future()); std::future<int> f1(p1.get_future());
p1.set_exception(std::copy_exception(value)); p1.set_exception(std::copy_exception(value));
try try
...@@ -47,6 +47,7 @@ void test01() ...@@ -47,6 +47,7 @@ void test01()
{ {
VERIFY( e == value ); VERIFY( e == value );
} }
VERIFY( !f1.valid() );
} }
void test02() void test02()
...@@ -54,7 +55,7 @@ void test02() ...@@ -54,7 +55,7 @@ void test02()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int&> p1; std::promise<int&> p1;
std::unique_future<int&> f1(p1.get_future()); std::future<int&> f1(p1.get_future());
p1.set_exception(std::copy_exception(value)); p1.set_exception(std::copy_exception(value));
try try
...@@ -66,6 +67,7 @@ void test02() ...@@ -66,6 +67,7 @@ void test02()
{ {
VERIFY( e == value ); VERIFY( e == value );
} }
VERIFY( !f1.valid() );
} }
void test03() void test03()
...@@ -73,7 +75,7 @@ void test03() ...@@ -73,7 +75,7 @@ void test03()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<void> p1; std::promise<void> p1;
std::unique_future<void> f1(p1.get_future()); std::future<void> f1(p1.get_future());
p1.set_exception(std::copy_exception(value)); p1.set_exception(std::copy_exception(value));
try try
...@@ -85,6 +87,7 @@ void test03() ...@@ -85,6 +87,7 @@ void test03()
{ {
VERIFY( e == value ); VERIFY( e == value );
} }
VERIFY( !f1.valid() );
} }
int main() int main()
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
// { dg-require-gthreads "" } // { dg-require-gthreads "" }
// { dg-require-atomic-builtins "" } // { dg-require-atomic-builtins "" }
// Copyright (C) 2009 Free Software Foundation, Inc. // Copyright (C) 2010 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the // software; you can redistribute it and/or modify it under the
...@@ -31,14 +31,22 @@ void test01() ...@@ -31,14 +31,22 @@ void test01()
{ {
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::future<int> f0;
VERIFY( !f0.valid() );
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1(p1.get_future()); std::future<int> f1(p1.get_future());
VERIFY( !f1.is_ready() ); VERIFY( f1.valid() );
p1.set_value(1); p1.set_value(1);
VERIFY( f1.is_ready() ); VERIFY( f1.valid() );
f1 = std::move(f0);
VERIFY( !f1.valid() );
VERIFY( !f0.valid() );
} }
int main() int main()
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
void wait(std::unique_future<void>& f) void wait(std::future<void>& f)
{ {
f.wait(); f.wait();
} }
...@@ -39,7 +39,7 @@ void wait(std::unique_future<void>& f) ...@@ -39,7 +39,7 @@ void wait(std::unique_future<void>& f)
void test01() void test01()
{ {
std::promise<void> p1; std::promise<void> p1;
std::unique_future<void> f1(p1.get_future()); std::future<void> f1(p1.get_future());
std::thread t1(wait, std::ref(f1)); std::thread t1(wait, std::ref(f1));
......
...@@ -33,7 +33,7 @@ void test01() ...@@ -33,7 +33,7 @@ void test01()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1(p1.get_future()); std::future<int> f1(p1.get_future());
std::chrono::milliseconds delay(100); std::chrono::milliseconds delay(100);
......
...@@ -38,7 +38,7 @@ void test01() ...@@ -38,7 +38,7 @@ void test01()
bool test __attribute__((unused)) = true; bool test __attribute__((unused)) = true;
std::promise<int> p1; std::promise<int> p1;
std::unique_future<int> f1(p1.get_future()); std::future<int> f1(p1.get_future());
auto when = make_time(10); auto when = make_time(10);
VERIFY( !f1.wait_until(when) ); VERIFY( !f1.wait_until(when) );
......
...@@ -26,9 +26,9 @@ ...@@ -26,9 +26,9 @@
#include <testsuite_tr1.h> #include <testsuite_tr1.h>
using namespace __gnu_test; using namespace __gnu_test;
using std::unique_future; using std::future;
template class unique_future<int>; template class future<int>;
template class unique_future<int&>; template class future<int&>;
template class unique_future<void>; template class future<void>;
template class unique_future<ClassType>; template class future<ClassType>;
template class unique_future<ClassType&>; template class future<ClassType&>;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment