head	1.7;
access;
symbols
	RELENG_9_1_0_RELEASE:1.2.2.2
	RELENG_9_1:1.2.2.2.0.2
	RELENG_9_1_BP:1.2.2.2
	RELENG_9:1.2.0.2;
locks; strict;
comment	@# @;


1.7
date	2013.07.11.00.34.38;	author svnexp;	state Exp;
branches;
next	1.6;

1.6
date	2013.04.28.00.41.54;	author svnexp;	state Exp;
branches;
next	1.5;

1.5
date	2013.02.08.05.09.54;	author svnexp;	state Exp;
branches;
next	1.4;

1.4
date	2012.11.17.03.36.33;	author svnexp;	state Exp;
branches;
next	1.3;

1.3
date	2012.10.22.18.25.04;	author dim;	state Exp;
branches;
next	1.2;

1.2
date	2012.03.14.00.09.36;	author theraven;	state Exp;
branches
	1.2.2.1;
next	1.1;

1.1
date	2011.11.25.20.59.04;	author theraven;	state Exp;
branches;
next	;

1.2.2.1
date	2012.05.22.18.30.14;	author theraven;	state dead;
branches;
next	1.2.2.2;

1.2.2.2
date	2012.05.22.18.30.14;	author theraven;	state Exp;
branches;
next	1.2.2.3;

1.2.2.3
date	2012.11.21.18.41.40;	author svnexp;	state Exp;
branches;
next	1.2.2.4;

1.2.2.4
date	2012.11.29.21.29.14;	author svnexp;	state Exp;
branches;
next	1.2.2.5;

1.2.2.5
date	2013.05.11.17.01.44;	author svnexp;	state Exp;
branches;
next	1.2.2.6;

1.2.2.6
date	2013.07.11.22.01.44;	author svnexp;	state Exp;
branches;
next	1.2.2.7;

1.2.2.7
date	2014.03.05.20.01.45;	author svnexp;	state Exp;
branches;
next	;


desc
@@


1.7
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/253159
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@// -*- C++ -*-
//===--------------------------- future -----------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_FUTURE
#define _LIBCPP_FUTURE

/*
    future synopsis

namespace std
{

enum class future_errc
{
    broken_promise,
    future_already_retrieved,
    promise_already_satisfied,
    no_state
};

enum class launch
{
    async = 1,
    deferred = 2,
    any = async | deferred
};

enum class future_status
{
    ready,
    timeout,
    deferred
};

template <> struct is_error_code_enum<future_errc> : public true_type { };
error_code make_error_code(future_errc e) noexcept;
error_condition make_error_condition(future_errc e) noexcept;

const error_category& future_category() noexcept;

class future_error
    : public logic_error
{
public:
    future_error(error_code ec);  // exposition only

    const error_code& code() const noexcept;
    const char*       what() const noexcept;
};

template <class R>
class promise
{
public:
    promise();
    template <class Allocator>
        promise(allocator_arg_t, const Allocator& a);
    promise(promise&& rhs) noexcept;
    promise(const promise& rhs) = delete;
    ~promise();

    // assignment
    promise& operator=(promise&& rhs) noexcept;
    promise& operator=(const promise& rhs) = delete;
    void swap(promise& other) noexcept;

    // retrieving the result
    future<R> get_future();

    // setting the result
    void set_value(const R& r);
    void set_value(R&& r);
    void set_exception(exception_ptr p);

    // setting the result with deferred notification
    void set_value_at_thread_exit(const R& r);
    void set_value_at_thread_exit(R&& r);
    void set_exception_at_thread_exit(exception_ptr p);
};

template <class R>
class promise<R&>
{
public:
    promise();
    template <class Allocator>
        promise(allocator_arg_t, const Allocator& a);
    promise(promise&& rhs) noexcept;
    promise(const promise& rhs) = delete;
    ~promise();

    // assignment
    promise& operator=(promise&& rhs) noexcept;
    promise& operator=(const promise& rhs) = delete;
    void swap(promise& other) noexcept;

    // retrieving the result
    future<R&> get_future();

    // setting the result
    void set_value(R& r);
    void set_exception(exception_ptr p);

    // setting the result with deferred notification
    void set_value_at_thread_exit(R&);
    void set_exception_at_thread_exit(exception_ptr p);
};

template <>
class promise<void>
{
public:
    promise();
    template <class Allocator>
        promise(allocator_arg_t, const Allocator& a);
    promise(promise&& rhs) noexcept;
    promise(const promise& rhs) = delete;
    ~promise();

    // assignment
    promise& operator=(promise&& rhs) noexcept;
    promise& operator=(const promise& rhs) = delete;
    void swap(promise& other) noexcept;

    // retrieving the result
    future<void> get_future();

    // setting the result
    void set_value();
    void set_exception(exception_ptr p);

    // setting the result with deferred notification
    void set_value_at_thread_exit();
    void set_exception_at_thread_exit(exception_ptr p);
};

template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;

template <class R, class Alloc>
    struct uses_allocator<promise<R>, Alloc> : public true_type {};

template <class R>
class future
{
public:
    future() noexcept;
    future(future&&) noexcept;
    future(const future& rhs) = delete;
    ~future();
    future& operator=(const future& rhs) = delete;
    future& operator=(future&&) noexcept;
    shared_future<R> share();

    // retrieving the value
    R get();

    // functions to check state
    bool valid() const noexcept;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <class R>
class future<R&>
{
public:
    future() noexcept;
    future(future&&) noexcept;
    future(const future& rhs) = delete;
    ~future();
    future& operator=(const future& rhs) = delete;
    future& operator=(future&&) noexcept;
    shared_future<R&> share();

    // retrieving the value
    R& get();

    // functions to check state
    bool valid() const noexcept;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <>
class future<void>
{
public:
    future() noexcept;
    future(future&&) noexcept;
    future(const future& rhs) = delete;
    ~future();
    future& operator=(const future& rhs) = delete;
    future& operator=(future&&) noexcept;
    shared_future<void> share();

    // retrieving the value
    void get();

    // functions to check state
    bool valid() const noexcept;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <class R>
class shared_future
{
public:
    shared_future() noexcept;
    shared_future(const shared_future& rhs);
    shared_future(future<R>&&) noexcept;
    shared_future(shared_future&& rhs) noexcept;
    ~shared_future();
    shared_future& operator=(const shared_future& rhs);
    shared_future& operator=(shared_future&& rhs) noexcept;

    // retrieving the value
    const R& get() const;

    // functions to check state
    bool valid() const noexcept;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <class R>
class shared_future<R&>
{
public:
    shared_future() noexcept;
    shared_future(const shared_future& rhs);
    shared_future(future<R&>&&) noexcept;
    shared_future(shared_future&& rhs) noexcept;
    ~shared_future();
    shared_future& operator=(const shared_future& rhs);
    shared_future& operator=(shared_future&& rhs) noexcept;

    // retrieving the value
    R& get() const;

    // functions to check state
    bool valid() const noexcept;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <>
class shared_future<void>
{
public:
    shared_future() noexcept;
    shared_future(const shared_future& rhs);
    shared_future(future<void>&&) noexcept;
    shared_future(shared_future&& rhs) noexcept;
    ~shared_future();
    shared_future& operator=(const shared_future& rhs);
    shared_future& operator=(shared_future&& rhs) noexcept;

    // retrieving the value
    void get() const;

    // functions to check state
    bool valid() const noexcept;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <class F, class... Args>
  future<typename result_of<F(Args...)>::type>
  async(F&& f, Args&&... args);

template <class F, class... Args>
  future<typename result_of<F(Args...)>::type>
  async(launch policy, F&& f, Args&&... args);

template <class> class packaged_task; // undefined

template <class R, class... ArgTypes>
class packaged_task<R(ArgTypes...)>
{
public:
    typedef R result_type;

    // construction and destruction
    packaged_task() noexcept;
    template <class F>
        explicit packaged_task(F&& f);
    template <class F, class Allocator>
        explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
    ~packaged_task();

    // no copy
    packaged_task(const packaged_task&) = delete;
    packaged_task& operator=(const packaged_task&) = delete;

    // move support
    packaged_task(packaged_task&& other) noexcept;
    packaged_task& operator=(packaged_task&& other) noexcept;
    void swap(packaged_task& other) noexcept;

    bool valid() const noexcept;

    // result retrieval
    future<R> get_future();

    // execution
    void operator()(ArgTypes... );
    void make_ready_at_thread_exit(ArgTypes...);

    void reset();
};

template <class R>
  void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;

template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;

}  // std

*/

#include <__config>
#include <system_error>
#include <memory>
#include <chrono>
#include <exception>
#include <mutex>
#include <thread>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

//enum class future_errc
_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
{
    broken_promise,
    future_already_retrieved,
    promise_already_satisfied,
    no_state
};
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)

template <>
struct _LIBCPP_TYPE_VIS is_error_code_enum<future_errc> : public true_type {};

#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
template <>
struct _LIBCPP_TYPE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
#endif

//enum class launch
_LIBCPP_DECLARE_STRONG_ENUM(launch)
{
    async = 1,
    deferred = 2,
    any = async | deferred
};
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)

#ifndef _LIBCPP_HAS_NO_STRONG_ENUMS

#ifdef _LIBCXX_UNDERLYING_TYPE
typedef underlying_type<launch>::type __launch_underlying_type;
#else
typedef int __launch_underlying_type;
#endif

inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
launch
operator&(launch __x, launch __y)
{
    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
                               static_cast<__launch_underlying_type>(__y));
}

inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
launch
operator|(launch __x, launch __y)
{
    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
                               static_cast<__launch_underlying_type>(__y));
}

inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
launch
operator^(launch __x, launch __y)
{
    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
                               static_cast<__launch_underlying_type>(__y));
}

inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
launch
operator~(launch __x)
{
    return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
}

inline _LIBCPP_INLINE_VISIBILITY
launch&
operator&=(launch& __x, launch __y)
{
    __x = __x & __y; return __x;
}

inline _LIBCPP_INLINE_VISIBILITY
launch&
operator|=(launch& __x, launch __y)
{
    __x = __x | __y; return __x;
}

inline _LIBCPP_INLINE_VISIBILITY
launch&
operator^=(launch& __x, launch __y)
{
    __x = __x ^ __y; return __x;
}

#endif  // !_LIBCPP_HAS_NO_STRONG_ENUMS

//enum class future_status
_LIBCPP_DECLARE_STRONG_ENUM(future_status)
{
    ready,
    timeout,
    deferred
};
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)

_LIBCPP_FUNC_VIS
const error_category& future_category() _NOEXCEPT;

inline _LIBCPP_INLINE_VISIBILITY
error_code
make_error_code(future_errc __e) _NOEXCEPT
{
    return error_code(static_cast<int>(__e), future_category());
}

inline _LIBCPP_INLINE_VISIBILITY
error_condition
make_error_condition(future_errc __e) _NOEXCEPT
{
    return error_condition(static_cast<int>(__e), future_category());
}

class _LIBCPP_EXCEPTION_ABI future_error
    : public logic_error
{
    error_code __ec_;
public:
    future_error(error_code __ec);

    _LIBCPP_INLINE_VISIBILITY
    const error_code& code() const _NOEXCEPT {return __ec_;}

    virtual ~future_error() _NOEXCEPT;
};

class __assoc_sub_state
    : public __shared_count
{
protected:
    exception_ptr __exception_;
    mutable mutex __mut_;
    mutable condition_variable __cv_;
    unsigned __state_;

    virtual void __on_zero_shared() _NOEXCEPT;
    void __sub_wait(unique_lock<mutex>& __lk);
public:
    enum
    {
        __constructed = 1,
        __future_attached = 2,
        ready = 4,
        deferred = 8
    };

    _LIBCPP_INLINE_VISIBILITY
    __assoc_sub_state() : __state_(0) {}

    _LIBCPP_INLINE_VISIBILITY
    bool __has_value() const
        {return (__state_ & __constructed) || (__exception_ != nullptr);}

    _LIBCPP_INLINE_VISIBILITY
    void __set_future_attached()
    {
        lock_guard<mutex> __lk(__mut_);
        __state_ |= __future_attached;
    }
    _LIBCPP_INLINE_VISIBILITY
    bool __has_future_attached() const {return __state_ & __future_attached;}

    _LIBCPP_INLINE_VISIBILITY
    void __set_deferred() {__state_ |= deferred;}

    void __make_ready();
    _LIBCPP_INLINE_VISIBILITY
    bool __is_ready() const {return __state_ & ready;}

    void set_value();
    void set_value_at_thread_exit();

    void set_exception(exception_ptr __p);
    void set_exception_at_thread_exit(exception_ptr __p);

    void copy();

    void wait();
    template <class _Rep, class _Period>
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
    template <class _Clock, class _Duration>
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;

    virtual void __execute();
};

template <class _Clock, class _Duration>
future_status
__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
{
    unique_lock<mutex> __lk(__mut_);
    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;
    return future_status::timeout;
}

template <class _Rep, class _Period>
inline _LIBCPP_INLINE_VISIBILITY
future_status
__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
{
    return wait_until(chrono::steady_clock::now() + __rel_time);
}

template <class _Rp>
class __assoc_state
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;
    typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
protected:
    _Up __value_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:

    template <class _Arg>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
        void set_value(_Arg&& __arg);
#else
        void set_value(_Arg& __arg);
#endif

    template <class _Arg>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
        void set_value_at_thread_exit(_Arg&& __arg);
#else
        void set_value_at_thread_exit(_Arg& __arg);
#endif

    _Rp move();
    typename add_lvalue_reference<_Rp>::type copy();
};

template <class _Rp>
void
__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
{
    if (this->__state_ & base::__constructed)
        reinterpret_cast<_Rp*>(&__value_)->~_Rp();
    delete this;
}

template <class _Rp>
template <class _Arg>
void
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__assoc_state<_Rp>::set_value(_Arg&& __arg)
#else
__assoc_state<_Rp>::set_value(_Arg& __arg)
#endif
{
    unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (this->__has_value())
        throw future_error(make_error_code(future_errc::promise_already_satisfied));
#endif
    ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
    this->__state_ |= base::__constructed | base::ready;
    __lk.unlock();
    __cv_.notify_all();
}

template <class _Rp>
template <class _Arg>
void
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
#else
__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
#endif
{
    unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (this->__has_value())
        throw future_error(make_error_code(future_errc::promise_already_satisfied));
#endif
    ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
    this->__state_ |= base::__constructed;
    __thread_local_data()->__make_ready_at_thread_exit(this);
    __lk.unlock();
}

template <class _Rp>
_Rp
__assoc_state<_Rp>::move()
{
    unique_lock<mutex> __lk(this->__mut_);
    this->__sub_wait(__lk);
    if (this->__exception_ != nullptr)
        rethrow_exception(this->__exception_);
    return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
}

template <class _Rp>
typename add_lvalue_reference<_Rp>::type
__assoc_state<_Rp>::copy()
{
    unique_lock<mutex> __lk(this->__mut_);
    this->__sub_wait(__lk);
    if (this->__exception_ != nullptr)
        rethrow_exception(this->__exception_);
    return *reinterpret_cast<_Rp*>(&__value_);
}

template <class _Rp>
class __assoc_state<_Rp&>
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;
    typedef _Rp* _Up;
protected:
    _Up __value_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:

    void set_value(_Rp& __arg);
    void set_value_at_thread_exit(_Rp& __arg);

    _Rp& copy();
};

template <class _Rp>
void
__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
{
    delete this;
}

template <class _Rp>
void
__assoc_state<_Rp&>::set_value(_Rp& __arg)
{
    unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (this->__has_value())
        throw future_error(make_error_code(future_errc::promise_already_satisfied));
#endif
    __value_ = &__arg;
    this->__state_ |= base::__constructed | base::ready;
    __lk.unlock();
    __cv_.notify_all();
}

template <class _Rp>
void
__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
{
    unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (this->__has_value())
        throw future_error(make_error_code(future_errc::promise_already_satisfied));
#endif
    __value_ = &__arg;
    this->__state_ |= base::__constructed;
    __thread_local_data()->__make_ready_at_thread_exit(this);
    __lk.unlock();
}

template <class _Rp>
_Rp&
__assoc_state<_Rp&>::copy()
{
    unique_lock<mutex> __lk(this->__mut_);
    this->__sub_wait(__lk);
    if (this->__exception_ != nullptr)
        rethrow_exception(this->__exception_);
    return *__value_;
}

template <class _Rp, class _Alloc>
class __assoc_state_alloc
    : public __assoc_state<_Rp>
{
    typedef __assoc_state<_Rp> base;
    _Alloc __alloc_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
    _LIBCPP_INLINE_VISIBILITY
    explicit __assoc_state_alloc(const _Alloc& __a)
        : __alloc_(__a) {}
};

template <class _Rp, class _Alloc>
void
__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
{
    if (this->__state_ & base::__constructed)
        reinterpret_cast<_Rp*>(&this->__value_)->~_Rp();
    typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
    this->~__assoc_state_alloc();
    __a.deallocate(this, 1);
}

template <class _Rp, class _Alloc>
class __assoc_state_alloc<_Rp&, _Alloc>
    : public __assoc_state<_Rp&>
{
    typedef __assoc_state<_Rp&> base;
    _Alloc __alloc_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
    _LIBCPP_INLINE_VISIBILITY
    explicit __assoc_state_alloc(const _Alloc& __a)
        : __alloc_(__a) {}
};

template <class _Rp, class _Alloc>
void
__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
{
    typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
    this->~__assoc_state_alloc();
    __a.deallocate(this, 1);
}

template <class _Alloc>
class __assoc_sub_state_alloc
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;
    _Alloc __alloc_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
    _LIBCPP_INLINE_VISIBILITY
    explicit __assoc_sub_state_alloc(const _Alloc& __a)
        : __alloc_(__a) {}
};

template <class _Alloc>
void
__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
{
    typename _Alloc::template rebind<__assoc_sub_state_alloc>::other __a(__alloc_);
    this->~__assoc_sub_state_alloc();
    __a.deallocate(this, 1);
}

template <class _Rp, class _Fp>
class __deferred_assoc_state
    : public __assoc_state<_Rp>
{
    typedef __assoc_state<_Rp> base;

    _Fp __func_;

public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    explicit __deferred_assoc_state(_Fp&& __f);
#endif

    virtual void __execute();
};

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp, class _Fp>
inline _LIBCPP_INLINE_VISIBILITY
__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
    : __func_(_VSTD::forward<_Fp>(__f))
{
    this->__set_deferred();
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp, class _Fp>
void
__deferred_assoc_state<_Rp, _Fp>::__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 _Fp>
class __deferred_assoc_state<void, _Fp>
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;

    _Fp __func_;

public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    explicit __deferred_assoc_state(_Fp&& __f);
#endif

    virtual void __execute();
};

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Fp>
inline _LIBCPP_INLINE_VISIBILITY
__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
    : __func_(_VSTD::forward<_Fp>(__f))
{
    this->__set_deferred();
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Fp>
void
__deferred_assoc_state<void, _Fp>::__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 _Rp, class _Fp>
class __async_assoc_state
    : public __assoc_state<_Rp>
{
    typedef __assoc_state<_Rp> base;

    _Fp __func_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    explicit __async_assoc_state(_Fp&& __f);
#endif

    virtual void __execute();
};

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp, class _Fp>
inline _LIBCPP_INLINE_VISIBILITY
__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
    : __func_(_VSTD::forward<_Fp>(__f))
{
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp, class _Fp>
void
__async_assoc_state<_Rp, _Fp>::__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 _Rp, class _Fp>
void
__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
{
    this->wait();
    base::__on_zero_shared();
}

template <class _Fp>
class __async_assoc_state<void, _Fp>
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;

    _Fp __func_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    explicit __async_assoc_state(_Fp&& __f);
#endif

    virtual void __execute();
};

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Fp>
inline _LIBCPP_INLINE_VISIBILITY
__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
    : __func_(_VSTD::forward<_Fp>(__f))
{
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Fp>
void
__async_assoc_state<void, _Fp>::__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 _Fp>
void
__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
{
    this->wait();
    base::__on_zero_shared();
}

template <class _Rp> class _LIBCPP_TYPE_VIS promise;
template <class _Rp> class _LIBCPP_TYPE_VIS shared_future;

// future

template <class _Rp> class _LIBCPP_TYPE_VIS future;

template <class _Rp, class _Fp>
future<_Rp>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__make_deferred_assoc_state(_Fp&& __f);
#else
__make_deferred_assoc_state(_Fp __f);
#endif

template <class _Rp, class _Fp>
future<_Rp>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__make_async_assoc_state(_Fp&& __f);
#else
__make_async_assoc_state(_Fp __f);
#endif

template <class _Rp>
class _LIBCPP_TYPE_VIS future
{
    __assoc_state<_Rp>* __state_;

    explicit future(__assoc_state<_Rp>* __state);

    template <class> friend class promise;
    template <class> friend class shared_future;

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
#else
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp __f);
#endif

public:
    _LIBCPP_INLINE_VISIBILITY
    future() _NOEXCEPT : __state_(nullptr) {}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    future(future&& __rhs) _NOEXCEPT
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    future(const future&) = delete;
    future& operator=(const future&) = delete;
    _LIBCPP_INLINE_VISIBILITY
    future& operator=(future&& __rhs) _NOEXCEPT
        {
            future(std::move(__rhs)).swap(*this);
            return *this;
        }
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    future(const future&);
    future& operator=(const future&);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~future();
    shared_future<_Rp> share();

    // retrieving the value
    _Rp get();

    _LIBCPP_INLINE_VISIBILITY
    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
future<_Rp>::future(__assoc_state<_Rp>* __state)
    : __state_(__state)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_->__has_future_attached())
        throw future_error(make_error_code(future_errc::future_already_retrieved));
#endif
    __state_->__add_shared();
    __state_->__set_future_attached();
}

struct __release_shared_count
{
    void operator()(__shared_count* p) {p->__release_shared();}
};

template <class _Rp>
future<_Rp>::~future()
{
    if (__state_)
        __state_->__release_shared();
}

template <class _Rp>
_Rp
future<_Rp>::get()
{
    unique_ptr<__shared_count, __release_shared_count> __(__state_);
    __assoc_state<_Rp>* __s = __state_;
    __state_ = nullptr;
    return __s->move();
}

template <class _Rp>
class _LIBCPP_TYPE_VIS future<_Rp&>
{
    __assoc_state<_Rp&>* __state_;

    explicit future(__assoc_state<_Rp&>* __state);

    template <class> friend class promise;
    template <class> friend class shared_future;

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
#else
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp __f);
#endif

public:
    _LIBCPP_INLINE_VISIBILITY
    future() _NOEXCEPT : __state_(nullptr) {}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    future(future&& __rhs) _NOEXCEPT
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    future(const future&) = delete;
    future& operator=(const future&) = delete;
    _LIBCPP_INLINE_VISIBILITY
    future& operator=(future&& __rhs) _NOEXCEPT
        {
            future(std::move(__rhs)).swap(*this);
            return *this;
        }
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    future(const future&);
    future& operator=(const future&);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~future();
    shared_future<_Rp&> share();

    // retrieving the value
    _Rp& get();

    _LIBCPP_INLINE_VISIBILITY
    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
future<_Rp&>::future(__assoc_state<_Rp&>* __state)
    : __state_(__state)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_->__has_future_attached())
        throw future_error(make_error_code(future_errc::future_already_retrieved));
#endif
    __state_->__add_shared();
    __state_->__set_future_attached();
}

template <class _Rp>
future<_Rp&>::~future()
{
    if (__state_)
        __state_->__release_shared();
}

template <class _Rp>
_Rp&
future<_Rp&>::get()
{
    unique_ptr<__shared_count, __release_shared_count> __(__state_);
    __assoc_state<_Rp&>* __s = __state_;
    __state_ = nullptr;
    return __s->copy();
}

template <>
class _LIBCPP_TYPE_VIS future<void>
{
    __assoc_sub_state* __state_;

    explicit future(__assoc_sub_state* __state);

    template <class> friend class promise;
    template <class> friend class shared_future;

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
#else
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp __f);
#endif

public:
    _LIBCPP_INLINE_VISIBILITY
    future() _NOEXCEPT : __state_(nullptr) {}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    future(future&& __rhs) _NOEXCEPT
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    future(const future&) = delete;
    future& operator=(const future&) = delete;
    _LIBCPP_INLINE_VISIBILITY
    future& operator=(future&& __rhs) _NOEXCEPT
        {
            future(std::move(__rhs)).swap(*this);
            return *this;
        }
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    future(const future&);
    future& operator=(const future&);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~future();
    shared_future<void> share();

    // retrieving the value
    void get();

    _LIBCPP_INLINE_VISIBILITY
    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
{
    __x.swap(__y);
}

// promise<R>

template <class _Callable> class packaged_task;

template <class _Rp>
class _LIBCPP_TYPE_VIS promise
{
    __assoc_state<_Rp>* __state_;

    _LIBCPP_INLINE_VISIBILITY
    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}

    template <class> friend class packaged_task;
public:
    promise();
    template <class _Alloc>
        promise(allocator_arg_t, const _Alloc& __a);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise(promise&& __rhs) _NOEXCEPT
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    promise(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~promise();

    // assignment
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise& operator=(promise&& __rhs) _NOEXCEPT
        {
            promise(std::move(__rhs)).swap(*this);
            return *this;
        }
    promise& operator=(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise& operator=(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}

    // retrieving the result
    future<_Rp> get_future();

    // setting the result
    void set_value(const _Rp& __r);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    void set_value(_Rp&& __r);
#endif
    void set_exception(exception_ptr __p);

    // setting the result with deferred notification
    void set_value_at_thread_exit(const _Rp& __r);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    void set_value_at_thread_exit(_Rp&& __r);
#endif
    void set_exception_at_thread_exit(exception_ptr __p);
};

template <class _Rp>
promise<_Rp>::promise()
    : __state_(new __assoc_state<_Rp>)
{
}

template <class _Rp>
template <class _Alloc>
promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
{
    typedef typename _Alloc::template rebind<__assoc_state_alloc<_Rp, _Alloc> >::other _A2;
    typedef __allocator_destructor<_A2> _D2;
    _A2 __a(__a0);
    unique_ptr<__assoc_state_alloc<_Rp, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
    ::new(__hold.get()) __assoc_state_alloc<_Rp, _Alloc>(__a0);
    __state_ = __hold.release();
}

template <class _Rp>
promise<_Rp>::~promise()
{
    if (__state_)
    {
        if (!__state_->__has_value() && __state_->use_count() > 1)
            __state_->set_exception(make_exception_ptr(
                      future_error(make_error_code(future_errc::broken_promise))
                                                      ));
        __state_->__release_shared();
    }
}

template <class _Rp>
future<_Rp>
promise<_Rp>::get_future()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    return future<_Rp>(__state_);
}

template <class _Rp>
void
promise<_Rp>::set_value(const _Rp& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value(__r);
}

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp>
void
promise<_Rp>::set_value(_Rp&& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value(_VSTD::move(__r));
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp>
void
promise<_Rp>::set_exception(exception_ptr __p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_exception(__p);
}

template <class _Rp>
void
promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value_at_thread_exit(__r);
}

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp>
void
promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value_at_thread_exit(_VSTD::move(__r));
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp>
void
promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_exception_at_thread_exit(__p);
}

// promise<R&>

template <class _Rp>
class _LIBCPP_TYPE_VIS promise<_Rp&>
{
    __assoc_state<_Rp&>* __state_;

    _LIBCPP_INLINE_VISIBILITY
    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}

    template <class> friend class packaged_task;

public:
    promise();
    template <class _Allocator>
        promise(allocator_arg_t, const _Allocator& __a);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise(promise&& __rhs) _NOEXCEPT
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    promise(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~promise();

    // assignment
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise& operator=(promise&& __rhs) _NOEXCEPT
        {
            promise(std::move(__rhs)).swap(*this);
            return *this;
        }
    promise& operator=(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise& operator=(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}

    // retrieving the result
    future<_Rp&> get_future();

    // setting the result
    void set_value(_Rp& __r);
    void set_exception(exception_ptr __p);

    // setting the result with deferred notification
    void set_value_at_thread_exit(_Rp&);
    void set_exception_at_thread_exit(exception_ptr __p);
};

template <class _Rp>
promise<_Rp&>::promise()
    : __state_(new __assoc_state<_Rp&>)
{
}

template <class _Rp>
template <class _Alloc>
promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
{
    typedef typename _Alloc::template rebind<__assoc_state_alloc<_Rp&, _Alloc> >::other _A2;
    typedef __allocator_destructor<_A2> _D2;
    _A2 __a(__a0);
    unique_ptr<__assoc_state_alloc<_Rp&, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
    ::new(__hold.get()) __assoc_state_alloc<_Rp&, _Alloc>(__a0);
    __state_ = __hold.release();
}

template <class _Rp>
promise<_Rp&>::~promise()
{
    if (__state_)
    {
        if (!__state_->__has_value() && __state_->use_count() > 1)
            __state_->set_exception(make_exception_ptr(
                      future_error(make_error_code(future_errc::broken_promise))
                                                      ));
        __state_->__release_shared();
    }
}

template <class _Rp>
future<_Rp&>
promise<_Rp&>::get_future()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    return future<_Rp&>(__state_);
}

template <class _Rp>
void
promise<_Rp&>::set_value(_Rp& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value(__r);
}

template <class _Rp>
void
promise<_Rp&>::set_exception(exception_ptr __p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_exception(__p);
}

template <class _Rp>
void
promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value_at_thread_exit(__r);
}

template <class _Rp>
void
promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_exception_at_thread_exit(__p);
}

// promise<void>

template <>
class _LIBCPP_TYPE_VIS promise<void>
{
    __assoc_sub_state* __state_;

    _LIBCPP_INLINE_VISIBILITY
    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}

    template <class> friend class packaged_task;

public:
    promise();
    template <class _Allocator>
        promise(allocator_arg_t, const _Allocator& __a);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise(promise&& __rhs) _NOEXCEPT
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    promise(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~promise();

    // assignment
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise& operator=(promise&& __rhs) _NOEXCEPT
        {
            promise(std::move(__rhs)).swap(*this);
            return *this;
        }
    promise& operator=(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise& operator=(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}

    // retrieving the result
    future<void> get_future();

    // setting the result
    void set_value();
    void set_exception(exception_ptr __p);

    // setting the result with deferred notification
    void set_value_at_thread_exit();
    void set_exception_at_thread_exit(exception_ptr __p);
};

template <class _Alloc>
promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
{
    typedef typename _Alloc::template rebind<__assoc_sub_state_alloc<_Alloc> >::other _A2;
    typedef __allocator_destructor<_A2> _D2;
    _A2 __a(__a0);
    unique_ptr<__assoc_sub_state_alloc<_Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
    ::new(__hold.get()) __assoc_sub_state_alloc<_Alloc>(__a0);
    __state_ = __hold.release();
}

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
{
    __x.swap(__y);
}

template <class _Rp, class _Alloc>
    struct _LIBCPP_TYPE_VIS uses_allocator<promise<_Rp>, _Alloc>
        : public true_type {};

#ifndef _LIBCPP_HAS_NO_VARIADICS

// packaged_task

template<class _Fp> class __packaged_task_base;

template<class _Rp, class ..._ArgTypes>
class __packaged_task_base<_Rp(_ArgTypes...)>
{
    __packaged_task_base(const __packaged_task_base&);
    __packaged_task_base& operator=(const __packaged_task_base&);
public:
    _LIBCPP_INLINE_VISIBILITY
    __packaged_task_base() {}
    _LIBCPP_INLINE_VISIBILITY
    virtual ~__packaged_task_base() {}
    virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
    virtual void destroy() = 0;
    virtual void destroy_deallocate() = 0;
    virtual _Rp operator()(_ArgTypes&& ...) = 0;
};

template<class _FD, class _Alloc, class _FB> class __packaged_task_func;

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
    : public  __packaged_task_base<_Rp(_ArgTypes...)>
{
    __compressed_pair<_Fp, _Alloc> __f_;
public:
    _LIBCPP_INLINE_VISIBILITY
    explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
    _LIBCPP_INLINE_VISIBILITY
    explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
    _LIBCPP_INLINE_VISIBILITY
    __packaged_task_func(const _Fp& __f, const _Alloc& __a)
        : __f_(__f, __a) {}
    _LIBCPP_INLINE_VISIBILITY
    __packaged_task_func(_Fp&& __f, const _Alloc& __a)
        : __f_(_VSTD::move(__f), __a) {}
    virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
    virtual void destroy();
    virtual void destroy_deallocate();
    virtual _Rp operator()(_ArgTypes&& ... __args);
};

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
void
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
                              __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
{
    ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
}

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
void
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
{
    __f_.~__compressed_pair<_Fp, _Alloc>();
}

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
void
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
{
    typedef typename _Alloc::template rebind<__packaged_task_func>::other _Ap;
    _Ap __a(__f_.second());
    __f_.~__compressed_pair<_Fp, _Alloc>();
    __a.deallocate(this, 1);
}

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
_Rp
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
{
    return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
}

template <class _Callable> class __packaged_task_function;

template<class _Rp, class ..._ArgTypes>
class __packaged_task_function<_Rp(_ArgTypes...)>
{
    typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
    typename aligned_storage<3*sizeof(void*)>::type __buf_;
    __base* __f_;

public:
    typedef _Rp result_type;

    // construct/copy/destroy:
    _LIBCPP_INLINE_VISIBILITY
    __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
    template<class _Fp>
      __packaged_task_function(_Fp&& __f);
    template<class _Fp, class _Alloc>
      __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);

    __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
    __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;

    __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&) _NOEXCEPT;

    _Rp operator()(_ArgTypes...) const;
};

template<class _Rp, class ..._ArgTypes>
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
{
    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<class _Rp, class ..._ArgTypes>
template <class _Fp>
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
    : __f_(nullptr)
{
    typedef typename remove_reference<_Fp>::type _FR;
    typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
    if (sizeof(_FF) <= sizeof(__buf_))
    {
        __f_ = (__base*)&__buf_;
        ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
    }
    else
    {
        typedef allocator<_FF> _Ap;
        _Ap __a;
        typedef __allocator_destructor<_Ap> _Dp;
        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
        ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
        __f_ = __hold.release();
    }
}

template<class _Rp, class ..._ArgTypes>
template <class _Fp, class _Alloc>
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
                                  allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
    : __f_(nullptr)
{
    typedef allocator_traits<_Alloc> __alloc_traits;
    typedef typename remove_reference<_Fp>::type _FR;
    typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
    if (sizeof(_FF) <= sizeof(__buf_))
    {
        __f_ = (__base*)&__buf_;
        ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
    }
    else
    {
        typedef typename __alloc_traits::template
#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
            rebind_alloc<_FF>
#else
            rebind_alloc<_FF>::other
#endif
                                                     _Ap;
        _Ap __a(__a0);
        typedef __allocator_destructor<_Ap> _Dp;
        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
        ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
        __f_ = __hold.release();
    }
}

template<class _Rp, class ..._ArgTypes>
__packaged_task_function<_Rp(_ArgTypes...)>&
__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
{
    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;
    }
    return *this;
}

template<class _Rp, class ..._ArgTypes>
__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
{
    if (__f_ == (__base*)&__buf_)
        __f_->destroy();
    else if (__f_)
        __f_->destroy_deallocate();
}

template<class _Rp, class ..._ArgTypes>
void
__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
{
    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
    {
        typename aligned_storage<sizeof(__buf_)>::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
        _VSTD::swap(__f_, __f.__f_);
}

template<class _Rp, class ..._ArgTypes>
inline _LIBCPP_INLINE_VISIBILITY
_Rp
__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
{
    return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
}

template<class _Rp, class ..._ArgTypes>
class _LIBCPP_TYPE_VIS packaged_task<_Rp(_ArgTypes...)>
{
public:
    typedef _Rp result_type;

private:
    __packaged_task_function<result_type(_ArgTypes...)> __f_;
    promise<result_type>                                __p_;

public:
    // construction and destruction
    _LIBCPP_INLINE_VISIBILITY
    packaged_task() _NOEXCEPT : __p_(nullptr) {}
    template <class _Fp>
        _LIBCPP_INLINE_VISIBILITY
        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
    template <class _Fp, class _Allocator>
        _LIBCPP_INLINE_VISIBILITY
        explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
             : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
               __p_(allocator_arg, __a) {}
    // ~packaged_task() = default;

    // no copy
    packaged_task(const packaged_task&) = delete;
    packaged_task& operator=(const packaged_task&) = delete;

    // move support
    _LIBCPP_INLINE_VISIBILITY
    packaged_task(packaged_task&& __other) _NOEXCEPT
        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
    _LIBCPP_INLINE_VISIBILITY
    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
    {
        __f_ = _VSTD::move(__other.__f_);
        __p_ = _VSTD::move(__other.__p_);
        return *this;
    }
    _LIBCPP_INLINE_VISIBILITY
    void swap(packaged_task& __other) _NOEXCEPT
    {
        __f_.swap(__other.__f_);
        __p_.swap(__other.__p_);
    }

    _LIBCPP_INLINE_VISIBILITY
    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}

    // result retrieval
    _LIBCPP_INLINE_VISIBILITY
    future<result_type> get_future() {return __p_.get_future();}

    // execution
    void operator()(_ArgTypes... __args);
    void make_ready_at_thread_exit(_ArgTypes... __args);

    void reset();
};

template<class _Rp, class ..._ArgTypes>
void
packaged_task<_Rp(_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_(_VSTD::forward<_ArgTypes>(__args)...));
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
        __p_.set_exception(current_exception());
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
}

template<class _Rp, class ..._ArgTypes>
void
packaged_task<_Rp(_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_(_VSTD::forward<_ArgTypes>(__args)...));
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
        __p_.set_exception_at_thread_exit(current_exception());
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
}

template<class _Rp, class ..._ArgTypes>
void
packaged_task<_Rp(_ArgTypes...)>::reset()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (!valid())
        throw future_error(make_error_code(future_errc::no_state));
#endif  // _LIBCPP_NO_EXCEPTIONS
    __p_ = promise<result_type>();
}

template<class ..._ArgTypes>
class _LIBCPP_TYPE_VIS packaged_task<void(_ArgTypes...)>
{
public:
    typedef void result_type;

private:
    __packaged_task_function<result_type(_ArgTypes...)> __f_;
    promise<result_type>                                __p_;

public:
    // construction and destruction
    _LIBCPP_INLINE_VISIBILITY
    packaged_task() _NOEXCEPT : __p_(nullptr) {}
    template <class _Fp>
        _LIBCPP_INLINE_VISIBILITY
        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
    template <class _Fp, class _Allocator>
        _LIBCPP_INLINE_VISIBILITY
        explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
             : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
               __p_(allocator_arg, __a) {}
    // ~packaged_task() = default;

    // no copy
    packaged_task(const packaged_task&) = delete;
    packaged_task& operator=(const packaged_task&) = delete;

    // move support
    _LIBCPP_INLINE_VISIBILITY
    packaged_task(packaged_task&& __other) _NOEXCEPT
        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
    _LIBCPP_INLINE_VISIBILITY
    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
    {
        __f_ = _VSTD::move(__other.__f_);
        __p_ = _VSTD::move(__other.__p_);
        return *this;
    }
    _LIBCPP_INLINE_VISIBILITY
    void swap(packaged_task& __other) _NOEXCEPT
    {
        __f_.swap(__other.__f_);
        __p_.swap(__other.__p_);
    }

    _LIBCPP_INLINE_VISIBILITY
    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}

    // result retrieval
    _LIBCPP_INLINE_VISIBILITY
    future<result_type> get_future() {return __p_.get_future();}

    // execution
    void operator()(_ArgTypes... __args);
    void make_ready_at_thread_exit(_ArgTypes... __args);

    void reset();
};

template<class ..._ArgTypes>
void
packaged_task<void(_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
        __f_(_VSTD::forward<_ArgTypes>(__args)...);
        __p_.set_value();
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
        __p_.set_exception(current_exception());
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
}

template<class ..._ArgTypes>
void
packaged_task<void(_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
        __f_(_VSTD::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<class ..._ArgTypes>
void
packaged_task<void(_ArgTypes...)>::reset()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (!valid())
        throw future_error(make_error_code(future_errc::no_state));
#endif  // _LIBCPP_NO_EXCEPTIONS
    __p_ = promise<result_type>();
}

template <class _Callable>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
{
    __x.swap(__y);
}

template <class _Callable, class _Alloc>
struct _LIBCPP_TYPE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
    : public true_type {};

template <class _Rp, class _Fp>
future<_Rp>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__make_deferred_assoc_state(_Fp&& __f)
#else
__make_deferred_assoc_state(_Fp __f)
#endif
{
    unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
        __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
    return future<_Rp>(__h.get());
}

template <class _Rp, class _Fp>
future<_Rp>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__make_async_assoc_state(_Fp&& __f)
#else
__make_async_assoc_state(_Fp __f)
#endif
{
    unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
        __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
    _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
    return future<_Rp>(__h.get());
}

template <class _Fp, class... _Args>
class __async_func
{
    tuple<_Fp, _Args...> __f_;

public:
    typedef typename __invoke_of<_Fp, _Args...>::type _Rp;

    _LIBCPP_INLINE_VISIBILITY
    explicit __async_func(_Fp&& __f, _Args&&... __args)
        : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}

    _LIBCPP_INLINE_VISIBILITY
    __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}

    _Rp operator()()
    {
        typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
        return __execute(_Index());
    }
private:
    template <size_t ..._Indices>
    _Rp
    __execute(__tuple_indices<_Indices...>)
    {
        return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
    }
};

template <class _Fp, class... _Args>
future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
async(launch __policy, _Fp&& __f, _Args&&... __args)
{
    typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
    typedef typename _BF::_Rp _Rp;
    future<_Rp> __r;
    if (int(__policy) & int(launch::async))
        __r = _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
                                                     __decay_copy(_VSTD::forward<_Args>(__args))...));
    else if (int(__policy) & int(launch::deferred))
        __r = _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
                                                        __decay_copy(_VSTD::forward<_Args>(__args))...));
    return __r;
}

template <class _Fp, class... _Args>
inline _LIBCPP_INLINE_VISIBILITY
future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
async(_Fp&& __f, _Args&&... __args)
{
    return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
                                    _VSTD::forward<_Args>(__args)...);
}

#endif  // _LIBCPP_HAS_NO_VARIADICS

// shared_future

template <class _Rp>
class _LIBCPP_TYPE_VIS shared_future
{
    __assoc_state<_Rp>* __state_;

public:
    _LIBCPP_INLINE_VISIBILITY
    shared_future() _NOEXCEPT : __state_(nullptr) {}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
        {if (__state_) __state_->__add_shared();}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
        {__f.__state_ = nullptr;}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
        {__rhs.__state_ = nullptr;}
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~shared_future();
    shared_future& operator=(const shared_future& __rhs);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
        {
            shared_future(std::move(__rhs)).swap(*this);
            return *this;
        }
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    // retrieving the value
    _LIBCPP_INLINE_VISIBILITY
    const _Rp& get() const {return __state_->copy();}

    _LIBCPP_INLINE_VISIBILITY
    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
shared_future<_Rp>::~shared_future()
{
    if (__state_)
        __state_->__release_shared();
}

template <class _Rp>
shared_future<_Rp>&
shared_future<_Rp>::operator=(const shared_future& __rhs)
{
    if (__rhs.__state_)
        __rhs.__state_->__add_shared();
    if (__state_)
        __state_->__release_shared();
    __state_ = __rhs.__state_;
    return *this;
}

template <class _Rp>
class _LIBCPP_TYPE_VIS shared_future<_Rp&>
{
    __assoc_state<_Rp&>* __state_;

public:
    _LIBCPP_INLINE_VISIBILITY
    shared_future() _NOEXCEPT : __state_(nullptr) {}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
        {if (__state_) __state_->__add_shared();}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
        {__f.__state_ = nullptr;}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
        {__rhs.__state_ = nullptr;}
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~shared_future();
    shared_future& operator=(const shared_future& __rhs);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
        {
            shared_future(std::move(__rhs)).swap(*this);
            return *this;
        }
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    // retrieving the value
    _LIBCPP_INLINE_VISIBILITY
    _Rp& get() const {return __state_->copy();}

    _LIBCPP_INLINE_VISIBILITY
    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
shared_future<_Rp&>::~shared_future()
{
    if (__state_)
        __state_->__release_shared();
}

template <class _Rp>
shared_future<_Rp&>&
shared_future<_Rp&>::operator=(const shared_future& __rhs)
{
    if (__rhs.__state_)
        __rhs.__state_->__add_shared();
    if (__state_)
        __state_->__release_shared();
    __state_ = __rhs.__state_;
    return *this;
}

template <>
class _LIBCPP_TYPE_VIS shared_future<void>
{
    __assoc_sub_state* __state_;

public:
    _LIBCPP_INLINE_VISIBILITY
    shared_future() _NOEXCEPT : __state_(nullptr) {}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
        {if (__state_) __state_->__add_shared();}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
        {__f.__state_ = nullptr;}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
        {__rhs.__state_ = nullptr;}
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~shared_future();
    shared_future& operator=(const shared_future& __rhs);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
        {
            shared_future(std::move(__rhs)).swap(*this);
            return *this;
        }
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    // retrieving the value
    _LIBCPP_INLINE_VISIBILITY
    void get() const {__state_->copy();}

    _LIBCPP_INLINE_VISIBILITY
    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
{
    __x.swap(__y);
}

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
shared_future<_Rp>
future<_Rp>::share()
{
    return shared_future<_Rp>(_VSTD::move(*this));
}

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
shared_future<_Rp&>
future<_Rp&>::share()
{
    return shared_future<_Rp&>(_VSTD::move(*this));
}

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

inline _LIBCPP_INLINE_VISIBILITY
shared_future<void>
future<void>::share()
{
    return shared_future<void>(_VSTD::move(*this));
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

_LIBCPP_END_NAMESPACE_STD

#endif  // _LIBCPP_FUTURE
@


1.6
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/249998
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@d406 66
@


1.5
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/246487
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@d390 1
a390 1
struct _LIBCPP_VISIBLE is_error_code_enum<future_errc> : public true_type {};
d394 1
a394 1
struct _LIBCPP_VISIBLE is_error_code_enum<future_errc::__lx> : public true_type { };
d415 1
a415 1
_LIBCPP_VISIBLE
d969 2
a970 2
template <class _Rp> class _LIBCPP_VISIBLE promise;
template <class _Rp> class _LIBCPP_VISIBLE shared_future;
d974 1
a974 1
template <class _Rp> class _LIBCPP_VISIBLE future;
d993 1
a993 1
class _LIBCPP_VISIBLE future
d1097 1
a1097 1
class _LIBCPP_VISIBLE future<_Rp&>
d1196 1
a1196 1
class _LIBCPP_VISIBLE future<void>
d1278 1
a1278 1
class _LIBCPP_VISIBLE promise
d1456 1
a1456 1
class _LIBCPP_VISIBLE promise<_Rp&>
d1599 1
a1599 1
class _LIBCPP_VISIBLE promise<void>
d1673 1
a1673 1
    struct _LIBCPP_VISIBLE uses_allocator<promise<_Rp>, _Alloc>
d1937 1
a1937 1
class _LIBCPP_VISIBLE packaged_task<_Rp(_ArgTypes...)>
d2052 1
a2052 1
class _LIBCPP_VISIBLE packaged_task<void(_ArgTypes...)>
d2177 1
a2177 1
struct _LIBCPP_VISIBLE uses_allocator<packaged_task<_Callable>, _Alloc>
d2266 1
a2266 1
class _LIBCPP_VISIBLE shared_future
d2340 1
a2340 1
class _LIBCPP_VISIBLE shared_future<_Rp&>
d2414 1
a2414 1
class _LIBCPP_VISIBLE shared_future<void>
@


1.4
log
@## SVN ##
## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/242945
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ## r242945 | theraven | 2012-11-13 03:27:43 +0000 (Tue, 13 Nov 2012) | 2 lines
## SVN ##
## SVN ## Import new version of libc++ into base.
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ##
@
text
@d473 5
a477 1
    void __set_future_attached() {__state_ |= __future_attached;}
d1760 1
a1760 1
    aligned_storage<3*sizeof(void*)>::type __buf_;
@


1.3
log
@SVN rev 241903 on 2012-10-22 18:25:04Z by dim

Import libc++ trunk r165949.  Among other improvements and bug fixes,
this has many visibility problems fixed, which should help with
compiling certain ports that exercise C++11 mode (i.e. Firefox).

Also, belatedly add the LICENSE.TXT and accompanying CREDITS.TXT files,
which are referred to in all the source files.

MFC after:	1 month
@
text
@d394 1
a394 1
struct _LIBCPP_VISIBLE is_error_code_enum<future_errc::_> : public true_type { };
@


1.2
log
@SVN rev 232950 on 2012-03-14 00:09:36Z by theraven

Import new versions of libcxxrt and libc++.
Please tests any C++ code you care about with -stdlib=libc++!

Approved by:	dim (mentor)
@
text
@d43 2
a44 2
error_code make_error_code(future_errc e);
error_condition make_error_condition(future_errc e);
d46 1
a46 1
const error_category& future_category();
d54 2
a55 2
    const error_code& code() const throw();
    const char*       what() const throw();
d65 1
a65 1
    promise(promise&& rhs);
d70 1
a70 1
    promise& operator=(promise&& rhs);
d72 1
a72 1
    void swap(promise& other);
d95 1
a95 1
    promise(promise&& rhs);
d100 1
a100 1
    promise& operator=(promise&& rhs);
d102 1
a102 1
    void swap(promise& other);
d123 1
a123 1
    promise(promise&& rhs);
d128 1
a128 1
    promise& operator=(promise&& rhs);
d130 1
a130 1
    void swap(promise& other);
d144 1
a144 1
template <class R> void swap(promise<R>& x, promise<R>& y);
d153 2
a154 2
    future();
    future(future&&);
d158 2
a159 2
    future& operator=(future&&);
    shared_future<R> share() &&;
d165 1
a165 1
    bool valid() const;
d180 2
a181 2
    future();
    future(future&&);
d185 2
a186 2
    future& operator=(future&&);
    shared_future<R&> share() &&;
d192 1
a192 1
    bool valid() const;
d207 2
a208 2
    future();
    future(future&&);
d212 2
a213 2
    future& operator=(future&&);
    shared_future<void> share() &&;
d219 1
a219 1
    bool valid() const;
d234 1
a234 1
    shared_future();
d236 2
a237 2
    shared_future(future<R>&&);
    shared_future(shared_future&& rhs);
d240 1
a240 1
    shared_future& operator=(shared_future&& rhs);
d246 1
a246 1
    bool valid() const;
d261 1
a261 1
    shared_future();
d263 2
a264 2
    shared_future(future<R&>&&);
    shared_future(shared_future&& rhs);
d267 1
a267 1
    shared_future& operator=(shared_future&& rhs);
d273 1
a273 1
    bool valid() const;
d288 1
a288 1
    shared_future();
d290 2
a291 2
    shared_future(future<void>&&);
    shared_future(shared_future&& rhs);
d294 1
a294 1
    shared_future& operator=(shared_future&& rhs);
d300 1
a300 1
    bool valid() const;
d328 1
a328 1
    packaged_task();
d336 2
a337 2
    packaged_task(packaged_task&) = delete;
    packaged_task& operator=(packaged_task&) = delete;
d340 3
a342 3
    packaged_task(packaged_task&& other);
    packaged_task& operator=(packaged_task&& other);
    void swap(packaged_task& other);
d344 1
a344 1
    bool valid() const;
d357 1
a357 1
  void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&);
d416 1
a416 1
const error_category& future_category();
d420 1
a420 1
make_error_code(future_errc __e)
d427 1
a427 1
make_error_condition(future_errc __e)
d440 1
a440 1
    const error_code& code() const throw() {return __ec_;}
a757 1
    this->~base();
d965 2
a966 2
template <class _Rp> class promise;
template <class _Rp> class shared_future;
d970 1
a970 1
template <class _Rp> class future;
d1012 1
a1012 1
    future() : __state_(nullptr) {}
d1015 1
a1015 1
    future(future&& __rhs)
d1020 1
a1020 1
    future& operator=(future&& __rhs)
d1038 1
a1038 1
    void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
d1042 1
a1042 1
    bool valid() const {return __state_ != nullptr;}
d1116 1
a1116 1
    future() : __state_(nullptr) {}
d1119 1
a1119 1
    future(future&& __rhs)
d1124 1
a1124 1
    future& operator=(future&& __rhs)
d1142 1
a1142 1
    void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
d1146 1
a1146 1
    bool valid() const {return __state_ != nullptr;}
d1215 1
a1215 1
    future() : __state_(nullptr) {}
d1218 1
a1218 1
    future(future&& __rhs)
d1223 1
a1223 1
    future& operator=(future&& __rhs)
d1241 1
a1241 1
    void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
d1245 1
a1245 1
    bool valid() const {return __state_ != nullptr;}
d1264 1
a1264 1
swap(future<_Rp>& __x, future<_Rp>& __y)
d1279 1
a1279 1
    explicit promise(nullptr_t) : __state_(nullptr) {}
d1288 1
a1288 1
    promise(promise&& __rhs)
d1301 1
a1301 1
    promise& operator=(promise&& __rhs)
d1313 1
a1313 1
    void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
d1457 1
a1457 1
    explicit promise(nullptr_t) : __state_(nullptr) {}
d1467 1
a1467 1
    promise(promise&& __rhs)
d1480 1
a1480 1
    promise& operator=(promise&& __rhs)
d1492 1
a1492 1
    void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
d1600 1
a1600 1
    explicit promise(nullptr_t) : __state_(nullptr) {}
d1610 1
a1610 1
    promise(promise&& __rhs)
d1623 1
a1623 1
    promise& operator=(promise&& __rhs)
d1635 1
a1635 1
    void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
d1663 1
a1663 1
swap(promise<_Rp>& __x, promise<_Rp>& __y)
d1688 1
a1688 1
    virtual void __move_to(__packaged_task_base*) = 0;
d1712 1
a1712 1
    virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*);
d1721 1
a1721 1
                              __packaged_task_base<_Rp(_ArgTypes...)>* __p)
d1764 1
a1764 1
    __packaged_task_function() : __f_(nullptr) {}
d1770 2
a1771 2
    __packaged_task_function(__packaged_task_function&&);
    __packaged_task_function& operator=(__packaged_task_function&&);
d1778 1
a1778 1
    void swap(__packaged_task_function&);
d1784 1
a1784 1
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f)
d1856 1
a1856 1
__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f)
d1875 1
d1889 1
a1889 1
__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f)
d1945 1
a1945 1
    packaged_task() : __p_(nullptr) {}
d1957 2
a1958 2
    packaged_task(packaged_task&) = delete;
    packaged_task& operator=(packaged_task&) = delete;
d1962 1
a1962 1
    packaged_task(packaged_task&& __other)
d1965 1
a1965 1
    packaged_task& operator=(packaged_task&& __other)
d1972 1
a1972 1
    void swap(packaged_task& __other)
d1979 1
a1979 1
    bool valid() const {return __p_.__state_ != nullptr;}
d2060 1
a2060 1
    packaged_task() : __p_(nullptr) {}
d2072 2
a2073 2
    packaged_task(packaged_task&) = delete;
    packaged_task& operator=(packaged_task&) = delete;
d2077 1
a2077 1
    packaged_task(packaged_task&& __other)
d2080 1
a2080 1
    packaged_task& operator=(packaged_task&& __other)
d2087 1
a2087 1
    void swap(packaged_task& __other)
d2094 1
a2094 1
    bool valid() const {return __p_.__state_ != nullptr;}
d2167 1
a2167 1
swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y)
d2268 1
a2268 1
    shared_future() : __state_(nullptr) {}
d2274 1
a2274 1
    shared_future(future<_Rp>&& __f) : __state_(__f.__state_)
d2277 1
a2277 1
    shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
d2284 1
a2284 1
    shared_future& operator=(shared_future&& __rhs)
d2296 1
a2296 1
    void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
d2300 1
a2300 1
    bool valid() const {return __state_ != nullptr;}
d2342 1
a2342 1
    shared_future() : __state_(nullptr) {}
d2348 1
a2348 1
    shared_future(future<_Rp&>&& __f) : __state_(__f.__state_)
d2351 1
a2351 1
    shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
d2358 1
a2358 1
    shared_future& operator=(shared_future&& __rhs)
d2370 1
a2370 1
    void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
d2374 1
a2374 1
    bool valid() const {return __state_ != nullptr;}
d2416 1
a2416 1
    shared_future() : __state_(nullptr) {}
d2422 1
a2422 1
    shared_future(future<void>&& __f) : __state_(__f.__state_)
d2425 1
a2425 1
    shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
d2432 1
a2432 1
    shared_future& operator=(shared_future&& __rhs)
d2444 1
a2444 1
    void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}
d2448 1
a2448 1
    bool valid() const {return __state_ != nullptr;}
d2467 1
a2467 1
swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y)
@


1.2.2.1
log
@file future was added on branch RELENG_9 on 2012-05-22 18:32:26 +0000
@
text
@d1 2501
@


1.2.2.2
log
@SVN rev 235798 on 2012-05-22 18:30:14Z by theraven

Merged libcxxrt and libc++.  Now available for testing on 9-stable with
-stdlib=libc++.  Changes to libstdc++ not yet merged, so it is not yet possible
to mix libstdc++ and libc++ in the same program.

Merged revisions: 226702,226785,227006,227755,227983,227987,228531,228630,228761,229067,230127,232950,233098,234715-234716,234772
@
text
@a0 2501
// -*- C++ -*-
//===--------------------------- future -----------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_FUTURE
#define _LIBCPP_FUTURE

/*
    future synopsis

namespace std
{

enum class future_errc
{
    broken_promise,
    future_already_retrieved,
    promise_already_satisfied,
    no_state
};

enum class launch
{
    async = 1,
    deferred = 2,
    any = async | deferred
};

enum class future_status
{
    ready,
    timeout,
    deferred
};

template <> struct is_error_code_enum<future_errc> : public true_type { };
error_code make_error_code(future_errc e);
error_condition make_error_condition(future_errc e);

const error_category& future_category();

class future_error
    : public logic_error
{
public:
    future_error(error_code ec);  // exposition only

    const error_code& code() const throw();
    const char*       what() const throw();
};

template <class R>
class promise
{
public:
    promise();
    template <class Allocator>
        promise(allocator_arg_t, const Allocator& a);
    promise(promise&& rhs);
    promise(const promise& rhs) = delete;
    ~promise();

    // assignment
    promise& operator=(promise&& rhs);
    promise& operator=(const promise& rhs) = delete;
    void swap(promise& other);

    // retrieving the result
    future<R> get_future();

    // setting the result
    void set_value(const R& r);
    void set_value(R&& r);
    void set_exception(exception_ptr p);

    // setting the result with deferred notification
    void set_value_at_thread_exit(const R& r);
    void set_value_at_thread_exit(R&& r);
    void set_exception_at_thread_exit(exception_ptr p);
};

template <class R>
class promise<R&>
{
public:
    promise();
    template <class Allocator>
        promise(allocator_arg_t, const Allocator& a);
    promise(promise&& rhs);
    promise(const promise& rhs) = delete;
    ~promise();

    // assignment
    promise& operator=(promise&& rhs);
    promise& operator=(const promise& rhs) = delete;
    void swap(promise& other);

    // retrieving the result
    future<R&> get_future();

    // setting the result
    void set_value(R& r);
    void set_exception(exception_ptr p);

    // setting the result with deferred notification
    void set_value_at_thread_exit(R&);
    void set_exception_at_thread_exit(exception_ptr p);
};

template <>
class promise<void>
{
public:
    promise();
    template <class Allocator>
        promise(allocator_arg_t, const Allocator& a);
    promise(promise&& rhs);
    promise(const promise& rhs) = delete;
    ~promise();

    // assignment
    promise& operator=(promise&& rhs);
    promise& operator=(const promise& rhs) = delete;
    void swap(promise& other);

    // retrieving the result
    future<void> get_future();

    // setting the result
    void set_value();
    void set_exception(exception_ptr p);

    // setting the result with deferred notification
    void set_value_at_thread_exit();
    void set_exception_at_thread_exit(exception_ptr p);
};

template <class R> void swap(promise<R>& x, promise<R>& y);

template <class R, class Alloc>
    struct uses_allocator<promise<R>, Alloc> : public true_type {};

template <class R>
class future
{
public:
    future();
    future(future&&);
    future(const future& rhs) = delete;
    ~future();
    future& operator=(const future& rhs) = delete;
    future& operator=(future&&);
    shared_future<R> share() &&;

    // retrieving the value
    R get();

    // functions to check state
    bool valid() const;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <class R>
class future<R&>
{
public:
    future();
    future(future&&);
    future(const future& rhs) = delete;
    ~future();
    future& operator=(const future& rhs) = delete;
    future& operator=(future&&);
    shared_future<R&> share() &&;

    // retrieving the value
    R& get();

    // functions to check state
    bool valid() const;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <>
class future<void>
{
public:
    future();
    future(future&&);
    future(const future& rhs) = delete;
    ~future();
    future& operator=(const future& rhs) = delete;
    future& operator=(future&&);
    shared_future<void> share() &&;

    // retrieving the value
    void get();

    // functions to check state
    bool valid() const;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <class R>
class shared_future
{
public:
    shared_future();
    shared_future(const shared_future& rhs);
    shared_future(future<R>&&);
    shared_future(shared_future&& rhs);
    ~shared_future();
    shared_future& operator=(const shared_future& rhs);
    shared_future& operator=(shared_future&& rhs);

    // retrieving the value
    const R& get() const;

    // functions to check state
    bool valid() const;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <class R>
class shared_future<R&>
{
public:
    shared_future();
    shared_future(const shared_future& rhs);
    shared_future(future<R&>&&);
    shared_future(shared_future&& rhs);
    ~shared_future();
    shared_future& operator=(const shared_future& rhs);
    shared_future& operator=(shared_future&& rhs);

    // retrieving the value
    R& get() const;

    // functions to check state
    bool valid() const;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <>
class shared_future<void>
{
public:
    shared_future();
    shared_future(const shared_future& rhs);
    shared_future(future<void>&&);
    shared_future(shared_future&& rhs);
    ~shared_future();
    shared_future& operator=(const shared_future& rhs);
    shared_future& operator=(shared_future&& rhs);

    // retrieving the value
    void get() const;

    // functions to check state
    bool valid() const;

    void wait() const;
    template <class Rep, class Period>
        future_status
        wait_for(const chrono::duration<Rep, Period>& rel_time) const;
    template <class Clock, class Duration>
        future_status
        wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
};

template <class F, class... Args>
  future<typename result_of<F(Args...)>::type>
  async(F&& f, Args&&... args);

template <class F, class... Args>
  future<typename result_of<F(Args...)>::type>
  async(launch policy, F&& f, Args&&... args);

template <class> class packaged_task; // undefined

template <class R, class... ArgTypes>
class packaged_task<R(ArgTypes...)>
{
public:
    typedef R result_type;

    // construction and destruction
    packaged_task();
    template <class F>
        explicit packaged_task(F&& f);
    template <class F, class Allocator>
        explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
    ~packaged_task();

    // no copy
    packaged_task(packaged_task&) = delete;
    packaged_task& operator=(packaged_task&) = delete;

    // move support
    packaged_task(packaged_task&& other);
    packaged_task& operator=(packaged_task&& other);
    void swap(packaged_task& other);

    bool valid() const;

    // result retrieval
    future<R> get_future();

    // execution
    void operator()(ArgTypes... );
    void make_ready_at_thread_exit(ArgTypes...);

    void reset();
};

template <class R>
  void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&);

template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;

}  // std

*/

#include <__config>
#include <system_error>
#include <memory>
#include <chrono>
#include <exception>
#include <mutex>
#include <thread>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

//enum class future_errc
_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
{
    broken_promise,
    future_already_retrieved,
    promise_already_satisfied,
    no_state
};
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)

template <>
struct _LIBCPP_VISIBLE is_error_code_enum<future_errc> : public true_type {};

#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
template <>
struct _LIBCPP_VISIBLE is_error_code_enum<future_errc::_> : public true_type { };
#endif

//enum class launch
_LIBCPP_DECLARE_STRONG_ENUM(launch)
{
    async = 1,
    deferred = 2,
    any = async | deferred
};
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)

//enum class future_status
_LIBCPP_DECLARE_STRONG_ENUM(future_status)
{
    ready,
    timeout,
    deferred
};
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)

_LIBCPP_VISIBLE
const error_category& future_category();

inline _LIBCPP_INLINE_VISIBILITY
error_code
make_error_code(future_errc __e)
{
    return error_code(static_cast<int>(__e), future_category());
}

inline _LIBCPP_INLINE_VISIBILITY
error_condition
make_error_condition(future_errc __e)
{
    return error_condition(static_cast<int>(__e), future_category());
}

class _LIBCPP_EXCEPTION_ABI future_error
    : public logic_error
{
    error_code __ec_;
public:
    future_error(error_code __ec);

    _LIBCPP_INLINE_VISIBILITY
    const error_code& code() const throw() {return __ec_;}

    virtual ~future_error() _NOEXCEPT;
};

class __assoc_sub_state
    : public __shared_count
{
protected:
    exception_ptr __exception_;
    mutable mutex __mut_;
    mutable condition_variable __cv_;
    unsigned __state_;

    virtual void __on_zero_shared() _NOEXCEPT;
    void __sub_wait(unique_lock<mutex>& __lk);
public:
    enum
    {
        __constructed = 1,
        __future_attached = 2,
        ready = 4,
        deferred = 8
    };

    _LIBCPP_INLINE_VISIBILITY
    __assoc_sub_state() : __state_(0) {}

    _LIBCPP_INLINE_VISIBILITY
    bool __has_value() const
        {return (__state_ & __constructed) || (__exception_ != nullptr);}

    _LIBCPP_INLINE_VISIBILITY
    void __set_future_attached() {__state_ |= __future_attached;}
    _LIBCPP_INLINE_VISIBILITY
    bool __has_future_attached() const {return __state_ & __future_attached;}

    _LIBCPP_INLINE_VISIBILITY
    void __set_deferred() {__state_ |= deferred;}

    void __make_ready();
    _LIBCPP_INLINE_VISIBILITY
    bool __is_ready() const {return __state_ & ready;}

    void set_value();
    void set_value_at_thread_exit();

    void set_exception(exception_ptr __p);
    void set_exception_at_thread_exit(exception_ptr __p);

    void copy();

    void wait();
    template <class _Rep, class _Period>
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
    template <class _Clock, class _Duration>
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;

    virtual void __execute();
};

template <class _Clock, class _Duration>
future_status
__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
{
    unique_lock<mutex> __lk(__mut_);
    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;
    return future_status::timeout;
}

template <class _Rep, class _Period>
inline _LIBCPP_INLINE_VISIBILITY
future_status
__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
{
    return wait_until(chrono::steady_clock::now() + __rel_time);
}

template <class _Rp>
class __assoc_state
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;
    typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
protected:
    _Up __value_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:

    template <class _Arg>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
        void set_value(_Arg&& __arg);
#else
        void set_value(_Arg& __arg);
#endif

    template <class _Arg>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
        void set_value_at_thread_exit(_Arg&& __arg);
#else
        void set_value_at_thread_exit(_Arg& __arg);
#endif

    _Rp move();
    typename add_lvalue_reference<_Rp>::type copy();
};

template <class _Rp>
void
__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
{
    if (this->__state_ & base::__constructed)
        reinterpret_cast<_Rp*>(&__value_)->~_Rp();
    delete this;
}

template <class _Rp>
template <class _Arg>
void
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__assoc_state<_Rp>::set_value(_Arg&& __arg)
#else
__assoc_state<_Rp>::set_value(_Arg& __arg)
#endif
{
    unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (this->__has_value())
        throw future_error(make_error_code(future_errc::promise_already_satisfied));
#endif
    ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
    this->__state_ |= base::__constructed | base::ready;
    __lk.unlock();
    __cv_.notify_all();
}

template <class _Rp>
template <class _Arg>
void
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
#else
__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
#endif
{
    unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (this->__has_value())
        throw future_error(make_error_code(future_errc::promise_already_satisfied));
#endif
    ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
    this->__state_ |= base::__constructed;
    __thread_local_data()->__make_ready_at_thread_exit(this);
    __lk.unlock();
}

template <class _Rp>
_Rp
__assoc_state<_Rp>::move()
{
    unique_lock<mutex> __lk(this->__mut_);
    this->__sub_wait(__lk);
    if (this->__exception_ != nullptr)
        rethrow_exception(this->__exception_);
    return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
}

template <class _Rp>
typename add_lvalue_reference<_Rp>::type
__assoc_state<_Rp>::copy()
{
    unique_lock<mutex> __lk(this->__mut_);
    this->__sub_wait(__lk);
    if (this->__exception_ != nullptr)
        rethrow_exception(this->__exception_);
    return *reinterpret_cast<_Rp*>(&__value_);
}

template <class _Rp>
class __assoc_state<_Rp&>
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;
    typedef _Rp* _Up;
protected:
    _Up __value_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:

    void set_value(_Rp& __arg);
    void set_value_at_thread_exit(_Rp& __arg);

    _Rp& copy();
};

template <class _Rp>
void
__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
{
    delete this;
}

template <class _Rp>
void
__assoc_state<_Rp&>::set_value(_Rp& __arg)
{
    unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (this->__has_value())
        throw future_error(make_error_code(future_errc::promise_already_satisfied));
#endif
    __value_ = &__arg;
    this->__state_ |= base::__constructed | base::ready;
    __lk.unlock();
    __cv_.notify_all();
}

template <class _Rp>
void
__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
{
    unique_lock<mutex> __lk(this->__mut_);
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (this->__has_value())
        throw future_error(make_error_code(future_errc::promise_already_satisfied));
#endif
    __value_ = &__arg;
    this->__state_ |= base::__constructed;
    __thread_local_data()->__make_ready_at_thread_exit(this);
    __lk.unlock();
}

template <class _Rp>
_Rp&
__assoc_state<_Rp&>::copy()
{
    unique_lock<mutex> __lk(this->__mut_);
    this->__sub_wait(__lk);
    if (this->__exception_ != nullptr)
        rethrow_exception(this->__exception_);
    return *__value_;
}

template <class _Rp, class _Alloc>
class __assoc_state_alloc
    : public __assoc_state<_Rp>
{
    typedef __assoc_state<_Rp> base;
    _Alloc __alloc_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
    _LIBCPP_INLINE_VISIBILITY
    explicit __assoc_state_alloc(const _Alloc& __a)
        : __alloc_(__a) {}
};

template <class _Rp, class _Alloc>
void
__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
{
    if (this->__state_ & base::__constructed)
        reinterpret_cast<_Rp*>(&this->__value_)->~_Rp();
    typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
    this->~__assoc_state_alloc();
    __a.deallocate(this, 1);
}

template <class _Rp, class _Alloc>
class __assoc_state_alloc<_Rp&, _Alloc>
    : public __assoc_state<_Rp&>
{
    typedef __assoc_state<_Rp&> base;
    _Alloc __alloc_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
    _LIBCPP_INLINE_VISIBILITY
    explicit __assoc_state_alloc(const _Alloc& __a)
        : __alloc_(__a) {}
};

template <class _Rp, class _Alloc>
void
__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
{
    typename _Alloc::template rebind<__assoc_state_alloc>::other __a(__alloc_);
    this->~__assoc_state_alloc();
    __a.deallocate(this, 1);
}

template <class _Alloc>
class __assoc_sub_state_alloc
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;
    _Alloc __alloc_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
    _LIBCPP_INLINE_VISIBILITY
    explicit __assoc_sub_state_alloc(const _Alloc& __a)
        : __alloc_(__a) {}
};

template <class _Alloc>
void
__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
{
    this->~base();
    typename _Alloc::template rebind<__assoc_sub_state_alloc>::other __a(__alloc_);
    this->~__assoc_sub_state_alloc();
    __a.deallocate(this, 1);
}

template <class _Rp, class _Fp>
class __deferred_assoc_state
    : public __assoc_state<_Rp>
{
    typedef __assoc_state<_Rp> base;

    _Fp __func_;

public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    explicit __deferred_assoc_state(_Fp&& __f);
#endif

    virtual void __execute();
};

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp, class _Fp>
inline _LIBCPP_INLINE_VISIBILITY
__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
    : __func_(_VSTD::forward<_Fp>(__f))
{
    this->__set_deferred();
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp, class _Fp>
void
__deferred_assoc_state<_Rp, _Fp>::__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 _Fp>
class __deferred_assoc_state<void, _Fp>
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;

    _Fp __func_;

public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    explicit __deferred_assoc_state(_Fp&& __f);
#endif

    virtual void __execute();
};

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Fp>
inline _LIBCPP_INLINE_VISIBILITY
__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
    : __func_(_VSTD::forward<_Fp>(__f))
{
    this->__set_deferred();
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Fp>
void
__deferred_assoc_state<void, _Fp>::__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 _Rp, class _Fp>
class __async_assoc_state
    : public __assoc_state<_Rp>
{
    typedef __assoc_state<_Rp> base;

    _Fp __func_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    explicit __async_assoc_state(_Fp&& __f);
#endif

    virtual void __execute();
};

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp, class _Fp>
inline _LIBCPP_INLINE_VISIBILITY
__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
    : __func_(_VSTD::forward<_Fp>(__f))
{
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp, class _Fp>
void
__async_assoc_state<_Rp, _Fp>::__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 _Rp, class _Fp>
void
__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
{
    this->wait();
    base::__on_zero_shared();
}

template <class _Fp>
class __async_assoc_state<void, _Fp>
    : public __assoc_sub_state
{
    typedef __assoc_sub_state base;

    _Fp __func_;

    virtual void __on_zero_shared() _NOEXCEPT;
public:
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    explicit __async_assoc_state(_Fp&& __f);
#endif

    virtual void __execute();
};

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Fp>
inline _LIBCPP_INLINE_VISIBILITY
__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
    : __func_(_VSTD::forward<_Fp>(__f))
{
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Fp>
void
__async_assoc_state<void, _Fp>::__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 _Fp>
void
__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
{
    this->wait();
    base::__on_zero_shared();
}

template <class _Rp> class promise;
template <class _Rp> class shared_future;

// future

template <class _Rp> class future;

template <class _Rp, class _Fp>
future<_Rp>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__make_deferred_assoc_state(_Fp&& __f);
#else
__make_deferred_assoc_state(_Fp __f);
#endif

template <class _Rp, class _Fp>
future<_Rp>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__make_async_assoc_state(_Fp&& __f);
#else
__make_async_assoc_state(_Fp __f);
#endif

template <class _Rp>
class _LIBCPP_VISIBLE future
{
    __assoc_state<_Rp>* __state_;

    explicit future(__assoc_state<_Rp>* __state);

    template <class> friend class promise;
    template <class> friend class shared_future;

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
#else
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp __f);
#endif

public:
    _LIBCPP_INLINE_VISIBILITY
    future() : __state_(nullptr) {}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    future(future&& __rhs)
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    future(const future&) = delete;
    future& operator=(const future&) = delete;
    _LIBCPP_INLINE_VISIBILITY
    future& operator=(future&& __rhs)
        {
            future(std::move(__rhs)).swap(*this);
            return *this;
        }
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    future(const future&);
    future& operator=(const future&);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~future();
    shared_future<_Rp> share();

    // retrieving the value
    _Rp get();

    _LIBCPP_INLINE_VISIBILITY
    void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
future<_Rp>::future(__assoc_state<_Rp>* __state)
    : __state_(__state)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_->__has_future_attached())
        throw future_error(make_error_code(future_errc::future_already_retrieved));
#endif
    __state_->__add_shared();
    __state_->__set_future_attached();
}

struct __release_shared_count
{
    void operator()(__shared_count* p) {p->__release_shared();}
};

template <class _Rp>
future<_Rp>::~future()
{
    if (__state_)
        __state_->__release_shared();
}

template <class _Rp>
_Rp
future<_Rp>::get()
{
    unique_ptr<__shared_count, __release_shared_count> __(__state_);
    __assoc_state<_Rp>* __s = __state_;
    __state_ = nullptr;
    return __s->move();
}

template <class _Rp>
class _LIBCPP_VISIBLE future<_Rp&>
{
    __assoc_state<_Rp&>* __state_;

    explicit future(__assoc_state<_Rp&>* __state);

    template <class> friend class promise;
    template <class> friend class shared_future;

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
#else
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp __f);
#endif

public:
    _LIBCPP_INLINE_VISIBILITY
    future() : __state_(nullptr) {}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    future(future&& __rhs)
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    future(const future&) = delete;
    future& operator=(const future&) = delete;
    _LIBCPP_INLINE_VISIBILITY
    future& operator=(future&& __rhs)
        {
            future(std::move(__rhs)).swap(*this);
            return *this;
        }
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    future(const future&);
    future& operator=(const future&);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~future();
    shared_future<_Rp&> share();

    // retrieving the value
    _Rp& get();

    _LIBCPP_INLINE_VISIBILITY
    void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
future<_Rp&>::future(__assoc_state<_Rp&>* __state)
    : __state_(__state)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_->__has_future_attached())
        throw future_error(make_error_code(future_errc::future_already_retrieved));
#endif
    __state_->__add_shared();
    __state_->__set_future_attached();
}

template <class _Rp>
future<_Rp&>::~future()
{
    if (__state_)
        __state_->__release_shared();
}

template <class _Rp>
_Rp&
future<_Rp&>::get()
{
    unique_ptr<__shared_count, __release_shared_count> __(__state_);
    __assoc_state<_Rp&>* __s = __state_;
    __state_ = nullptr;
    return __s->copy();
}

template <>
class _LIBCPP_VISIBLE future<void>
{
    __assoc_sub_state* __state_;

    explicit future(__assoc_sub_state* __state);

    template <class> friend class promise;
    template <class> friend class shared_future;

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp&& __f);
#else
    template <class _R1, class _Fp>
        friend future<_R1> __make_deferred_assoc_state(_Fp __f);
    template <class _R1, class _Fp>
        friend future<_R1> __make_async_assoc_state(_Fp __f);
#endif

public:
    _LIBCPP_INLINE_VISIBILITY
    future() : __state_(nullptr) {}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    future(future&& __rhs)
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    future(const future&) = delete;
    future& operator=(const future&) = delete;
    _LIBCPP_INLINE_VISIBILITY
    future& operator=(future&& __rhs)
        {
            future(std::move(__rhs)).swap(*this);
            return *this;
        }
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    future(const future&);
    future& operator=(const future&);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~future();
    shared_future<void> share();

    // retrieving the value
    void get();

    _LIBCPP_INLINE_VISIBILITY
    void swap(future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(future<_Rp>& __x, future<_Rp>& __y)
{
    __x.swap(__y);
}

// promise<R>

template <class _Callable> class packaged_task;

template <class _Rp>
class _LIBCPP_VISIBLE promise
{
    __assoc_state<_Rp>* __state_;

    _LIBCPP_INLINE_VISIBILITY
    explicit promise(nullptr_t) : __state_(nullptr) {}

    template <class> friend class packaged_task;
public:
    promise();
    template <class _Alloc>
        promise(allocator_arg_t, const _Alloc& __a);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise(promise&& __rhs)
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    promise(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~promise();

    // assignment
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise& operator=(promise&& __rhs)
        {
            promise(std::move(__rhs)).swap(*this);
            return *this;
        }
    promise& operator=(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise& operator=(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}

    // retrieving the result
    future<_Rp> get_future();

    // setting the result
    void set_value(const _Rp& __r);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    void set_value(_Rp&& __r);
#endif
    void set_exception(exception_ptr __p);

    // setting the result with deferred notification
    void set_value_at_thread_exit(const _Rp& __r);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    void set_value_at_thread_exit(_Rp&& __r);
#endif
    void set_exception_at_thread_exit(exception_ptr __p);
};

template <class _Rp>
promise<_Rp>::promise()
    : __state_(new __assoc_state<_Rp>)
{
}

template <class _Rp>
template <class _Alloc>
promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
{
    typedef typename _Alloc::template rebind<__assoc_state_alloc<_Rp, _Alloc> >::other _A2;
    typedef __allocator_destructor<_A2> _D2;
    _A2 __a(__a0);
    unique_ptr<__assoc_state_alloc<_Rp, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
    ::new(__hold.get()) __assoc_state_alloc<_Rp, _Alloc>(__a0);
    __state_ = __hold.release();
}

template <class _Rp>
promise<_Rp>::~promise()
{
    if (__state_)
    {
        if (!__state_->__has_value() && __state_->use_count() > 1)
            __state_->set_exception(make_exception_ptr(
                      future_error(make_error_code(future_errc::broken_promise))
                                                      ));
        __state_->__release_shared();
    }
}

template <class _Rp>
future<_Rp>
promise<_Rp>::get_future()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    return future<_Rp>(__state_);
}

template <class _Rp>
void
promise<_Rp>::set_value(const _Rp& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value(__r);
}

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp>
void
promise<_Rp>::set_value(_Rp&& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value(_VSTD::move(__r));
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp>
void
promise<_Rp>::set_exception(exception_ptr __p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_exception(__p);
}

template <class _Rp>
void
promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value_at_thread_exit(__r);
}

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp>
void
promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value_at_thread_exit(_VSTD::move(__r));
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

template <class _Rp>
void
promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_exception_at_thread_exit(__p);
}

// promise<R&>

template <class _Rp>
class _LIBCPP_VISIBLE promise<_Rp&>
{
    __assoc_state<_Rp&>* __state_;

    _LIBCPP_INLINE_VISIBILITY
    explicit promise(nullptr_t) : __state_(nullptr) {}

    template <class> friend class packaged_task;

public:
    promise();
    template <class _Allocator>
        promise(allocator_arg_t, const _Allocator& __a);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise(promise&& __rhs)
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    promise(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~promise();

    // assignment
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise& operator=(promise&& __rhs)
        {
            promise(std::move(__rhs)).swap(*this);
            return *this;
        }
    promise& operator=(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise& operator=(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}

    // retrieving the result
    future<_Rp&> get_future();

    // setting the result
    void set_value(_Rp& __r);
    void set_exception(exception_ptr __p);

    // setting the result with deferred notification
    void set_value_at_thread_exit(_Rp&);
    void set_exception_at_thread_exit(exception_ptr __p);
};

template <class _Rp>
promise<_Rp&>::promise()
    : __state_(new __assoc_state<_Rp&>)
{
}

template <class _Rp>
template <class _Alloc>
promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
{
    typedef typename _Alloc::template rebind<__assoc_state_alloc<_Rp&, _Alloc> >::other _A2;
    typedef __allocator_destructor<_A2> _D2;
    _A2 __a(__a0);
    unique_ptr<__assoc_state_alloc<_Rp&, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
    ::new(__hold.get()) __assoc_state_alloc<_Rp&, _Alloc>(__a0);
    __state_ = __hold.release();
}

template <class _Rp>
promise<_Rp&>::~promise()
{
    if (__state_)
    {
        if (!__state_->__has_value() && __state_->use_count() > 1)
            __state_->set_exception(make_exception_ptr(
                      future_error(make_error_code(future_errc::broken_promise))
                                                      ));
        __state_->__release_shared();
    }
}

template <class _Rp>
future<_Rp&>
promise<_Rp&>::get_future()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    return future<_Rp&>(__state_);
}

template <class _Rp>
void
promise<_Rp&>::set_value(_Rp& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value(__r);
}

template <class _Rp>
void
promise<_Rp&>::set_exception(exception_ptr __p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_exception(__p);
}

template <class _Rp>
void
promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_value_at_thread_exit(__r);
}

template <class _Rp>
void
promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (__state_ == nullptr)
        throw future_error(make_error_code(future_errc::no_state));
#endif
    __state_->set_exception_at_thread_exit(__p);
}

// promise<void>

template <>
class _LIBCPP_VISIBLE promise<void>
{
    __assoc_sub_state* __state_;

    _LIBCPP_INLINE_VISIBILITY
    explicit promise(nullptr_t) : __state_(nullptr) {}

    template <class> friend class packaged_task;

public:
    promise();
    template <class _Allocator>
        promise(allocator_arg_t, const _Allocator& __a);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise(promise&& __rhs)
        : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
    promise(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~promise();

    // assignment
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    promise& operator=(promise&& __rhs)
        {
            promise(std::move(__rhs)).swap(*this);
            return *this;
        }
    promise& operator=(const promise& __rhs) = delete;
#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
private:
    promise& operator=(const promise& __rhs);
public:
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    void swap(promise& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}

    // retrieving the result
    future<void> get_future();

    // setting the result
    void set_value();
    void set_exception(exception_ptr __p);

    // setting the result with deferred notification
    void set_value_at_thread_exit();
    void set_exception_at_thread_exit(exception_ptr __p);
};

template <class _Alloc>
promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
{
    typedef typename _Alloc::template rebind<__assoc_sub_state_alloc<_Alloc> >::other _A2;
    typedef __allocator_destructor<_A2> _D2;
    _A2 __a(__a0);
    unique_ptr<__assoc_sub_state_alloc<_Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
    ::new(__hold.get()) __assoc_sub_state_alloc<_Alloc>(__a0);
    __state_ = __hold.release();
}

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(promise<_Rp>& __x, promise<_Rp>& __y)
{
    __x.swap(__y);
}

template <class _Rp, class _Alloc>
    struct _LIBCPP_VISIBLE uses_allocator<promise<_Rp>, _Alloc>
        : public true_type {};

#ifndef _LIBCPP_HAS_NO_VARIADICS

// packaged_task

template<class _Fp> class __packaged_task_base;

template<class _Rp, class ..._ArgTypes>
class __packaged_task_base<_Rp(_ArgTypes...)>
{
    __packaged_task_base(const __packaged_task_base&);
    __packaged_task_base& operator=(const __packaged_task_base&);
public:
    _LIBCPP_INLINE_VISIBILITY
    __packaged_task_base() {}
    _LIBCPP_INLINE_VISIBILITY
    virtual ~__packaged_task_base() {}
    virtual void __move_to(__packaged_task_base*) = 0;
    virtual void destroy() = 0;
    virtual void destroy_deallocate() = 0;
    virtual _Rp operator()(_ArgTypes&& ...) = 0;
};

template<class _FD, class _Alloc, class _FB> class __packaged_task_func;

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
    : public  __packaged_task_base<_Rp(_ArgTypes...)>
{
    __compressed_pair<_Fp, _Alloc> __f_;
public:
    _LIBCPP_INLINE_VISIBILITY
    explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
    _LIBCPP_INLINE_VISIBILITY
    explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
    _LIBCPP_INLINE_VISIBILITY
    __packaged_task_func(const _Fp& __f, const _Alloc& __a)
        : __f_(__f, __a) {}
    _LIBCPP_INLINE_VISIBILITY
    __packaged_task_func(_Fp&& __f, const _Alloc& __a)
        : __f_(_VSTD::move(__f), __a) {}
    virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*);
    virtual void destroy();
    virtual void destroy_deallocate();
    virtual _Rp operator()(_ArgTypes&& ... __args);
};

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
void
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
                              __packaged_task_base<_Rp(_ArgTypes...)>* __p)
{
    ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
}

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
void
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
{
    __f_.~__compressed_pair<_Fp, _Alloc>();
}

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
void
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
{
    typedef typename _Alloc::template rebind<__packaged_task_func>::other _Ap;
    _Ap __a(__f_.second());
    __f_.~__compressed_pair<_Fp, _Alloc>();
    __a.deallocate(this, 1);
}

template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
_Rp
__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
{
    return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
}

template <class _Callable> class __packaged_task_function;

template<class _Rp, class ..._ArgTypes>
class __packaged_task_function<_Rp(_ArgTypes...)>
{
    typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
    aligned_storage<3*sizeof(void*)>::type __buf_;
    __base* __f_;

public:
    typedef _Rp result_type;

    // construct/copy/destroy:
    _LIBCPP_INLINE_VISIBILITY
    __packaged_task_function() : __f_(nullptr) {}
    template<class _Fp>
      __packaged_task_function(_Fp&& __f);
    template<class _Fp, class _Alloc>
      __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __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&);

    _Rp operator()(_ArgTypes...) const;
};

template<class _Rp, class ..._ArgTypes>
__packaged_task_function<_Rp(_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<class _Rp, class ..._ArgTypes>
template <class _Fp>
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
    : __f_(nullptr)
{
    typedef typename remove_reference<_Fp>::type _FR;
    typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
    if (sizeof(_FF) <= sizeof(__buf_))
    {
        __f_ = (__base*)&__buf_;
        ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
    }
    else
    {
        typedef allocator<_FF> _Ap;
        _Ap __a;
        typedef __allocator_destructor<_Ap> _Dp;
        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
        ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
        __f_ = __hold.release();
    }
}

template<class _Rp, class ..._ArgTypes>
template <class _Fp, class _Alloc>
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
                                  allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
    : __f_(nullptr)
{
    typedef allocator_traits<_Alloc> __alloc_traits;
    typedef typename remove_reference<_Fp>::type _FR;
    typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
    if (sizeof(_FF) <= sizeof(__buf_))
    {
        __f_ = (__base*)&__buf_;
        ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
    }
    else
    {
        typedef typename __alloc_traits::template
#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
            rebind_alloc<_FF>
#else
            rebind_alloc<_FF>::other
#endif
                                                     _Ap;
        _Ap __a(__a0);
        typedef __allocator_destructor<_Ap> _Dp;
        unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
        ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
        __f_ = __hold.release();
    }
}

template<class _Rp, class ..._ArgTypes>
__packaged_task_function<_Rp(_ArgTypes...)>&
__packaged_task_function<_Rp(_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<class _Rp, class ..._ArgTypes>
__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
{
    if (__f_ == (__base*)&__buf_)
        __f_->destroy();
    else if (__f_)
        __f_->destroy_deallocate();
}

template<class _Rp, class ..._ArgTypes>
void
__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f)
{
    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
    {
        typename aligned_storage<sizeof(__buf_)>::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
        _VSTD::swap(__f_, __f.__f_);
}

template<class _Rp, class ..._ArgTypes>
inline _LIBCPP_INLINE_VISIBILITY
_Rp
__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
{
    return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
}

template<class _Rp, class ..._ArgTypes>
class _LIBCPP_VISIBLE packaged_task<_Rp(_ArgTypes...)>
{
public:
    typedef _Rp result_type;

private:
    __packaged_task_function<result_type(_ArgTypes...)> __f_;
    promise<result_type>                                __p_;

public:
    // construction and destruction
    _LIBCPP_INLINE_VISIBILITY
    packaged_task() : __p_(nullptr) {}
    template <class _Fp>
        _LIBCPP_INLINE_VISIBILITY
        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
    template <class _Fp, class _Allocator>
        _LIBCPP_INLINE_VISIBILITY
        explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
             : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
               __p_(allocator_arg, __a) {}
    // ~packaged_task() = default;

    // no copy
    packaged_task(packaged_task&) = delete;
    packaged_task& operator=(packaged_task&) = delete;

    // move support
    _LIBCPP_INLINE_VISIBILITY
    packaged_task(packaged_task&& __other)
        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
    _LIBCPP_INLINE_VISIBILITY
    packaged_task& operator=(packaged_task&& __other)
    {
        __f_ = _VSTD::move(__other.__f_);
        __p_ = _VSTD::move(__other.__p_);
        return *this;
    }
    _LIBCPP_INLINE_VISIBILITY
    void swap(packaged_task& __other)
    {
        __f_.swap(__other.__f_);
        __p_.swap(__other.__p_);
    }

    _LIBCPP_INLINE_VISIBILITY
    bool valid() const {return __p_.__state_ != nullptr;}

    // result retrieval
    _LIBCPP_INLINE_VISIBILITY
    future<result_type> get_future() {return __p_.get_future();}

    // execution
    void operator()(_ArgTypes... __args);
    void make_ready_at_thread_exit(_ArgTypes... __args);

    void reset();
};

template<class _Rp, class ..._ArgTypes>
void
packaged_task<_Rp(_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_(_VSTD::forward<_ArgTypes>(__args)...));
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
        __p_.set_exception(current_exception());
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
}

template<class _Rp, class ..._ArgTypes>
void
packaged_task<_Rp(_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_(_VSTD::forward<_ArgTypes>(__args)...));
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
        __p_.set_exception_at_thread_exit(current_exception());
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
}

template<class _Rp, class ..._ArgTypes>
void
packaged_task<_Rp(_ArgTypes...)>::reset()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (!valid())
        throw future_error(make_error_code(future_errc::no_state));
#endif  // _LIBCPP_NO_EXCEPTIONS
    __p_ = promise<result_type>();
}

template<class ..._ArgTypes>
class _LIBCPP_VISIBLE packaged_task<void(_ArgTypes...)>
{
public:
    typedef void result_type;

private:
    __packaged_task_function<result_type(_ArgTypes...)> __f_;
    promise<result_type>                                __p_;

public:
    // construction and destruction
    _LIBCPP_INLINE_VISIBILITY
    packaged_task() : __p_(nullptr) {}
    template <class _Fp>
        _LIBCPP_INLINE_VISIBILITY
        explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
    template <class _Fp, class _Allocator>
        _LIBCPP_INLINE_VISIBILITY
        explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
             : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
               __p_(allocator_arg, __a) {}
    // ~packaged_task() = default;

    // no copy
    packaged_task(packaged_task&) = delete;
    packaged_task& operator=(packaged_task&) = delete;

    // move support
    _LIBCPP_INLINE_VISIBILITY
    packaged_task(packaged_task&& __other)
        : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
    _LIBCPP_INLINE_VISIBILITY
    packaged_task& operator=(packaged_task&& __other)
    {
        __f_ = _VSTD::move(__other.__f_);
        __p_ = _VSTD::move(__other.__p_);
        return *this;
    }
    _LIBCPP_INLINE_VISIBILITY
    void swap(packaged_task& __other)
    {
        __f_.swap(__other.__f_);
        __p_.swap(__other.__p_);
    }

    _LIBCPP_INLINE_VISIBILITY
    bool valid() const {return __p_.__state_ != nullptr;}

    // result retrieval
    _LIBCPP_INLINE_VISIBILITY
    future<result_type> get_future() {return __p_.get_future();}

    // execution
    void operator()(_ArgTypes... __args);
    void make_ready_at_thread_exit(_ArgTypes... __args);

    void reset();
};

template<class ..._ArgTypes>
void
packaged_task<void(_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
        __f_(_VSTD::forward<_ArgTypes>(__args)...);
        __p_.set_value();
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch (...)
    {
        __p_.set_exception(current_exception());
    }
#endif  // _LIBCPP_NO_EXCEPTIONS
}

template<class ..._ArgTypes>
void
packaged_task<void(_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
        __f_(_VSTD::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<class ..._ArgTypes>
void
packaged_task<void(_ArgTypes...)>::reset()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    if (!valid())
        throw future_error(make_error_code(future_errc::no_state));
#endif  // _LIBCPP_NO_EXCEPTIONS
    __p_ = promise<result_type>();
}

template <class _Callable>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y)
{
    __x.swap(__y);
}

template <class _Callable, class _Alloc>
struct _LIBCPP_VISIBLE uses_allocator<packaged_task<_Callable>, _Alloc>
    : public true_type {};

template <class _Rp, class _Fp>
future<_Rp>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__make_deferred_assoc_state(_Fp&& __f)
#else
__make_deferred_assoc_state(_Fp __f)
#endif
{
    unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
        __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
    return future<_Rp>(__h.get());
}

template <class _Rp, class _Fp>
future<_Rp>
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__make_async_assoc_state(_Fp&& __f)
#else
__make_async_assoc_state(_Fp __f)
#endif
{
    unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
        __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
    _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
    return future<_Rp>(__h.get());
}

template <class _Fp, class... _Args>
class __async_func
{
    tuple<_Fp, _Args...> __f_;

public:
    typedef typename __invoke_of<_Fp, _Args...>::type _Rp;

    _LIBCPP_INLINE_VISIBILITY
    explicit __async_func(_Fp&& __f, _Args&&... __args)
        : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}

    _LIBCPP_INLINE_VISIBILITY
    __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}

    _Rp operator()()
    {
        typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
        return __execute(_Index());
    }
private:
    template <size_t ..._Indices>
    _Rp
    __execute(__tuple_indices<_Indices...>)
    {
        return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
    }
};

template <class _Fp, class... _Args>
future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
async(launch __policy, _Fp&& __f, _Args&&... __args)
{
    typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
    typedef typename _BF::_Rp _Rp;
    future<_Rp> __r;
    if (int(__policy) & int(launch::async))
        __r = _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
                                                     __decay_copy(_VSTD::forward<_Args>(__args))...));
    else if (int(__policy) & int(launch::deferred))
        __r = _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
                                                        __decay_copy(_VSTD::forward<_Args>(__args))...));
    return __r;
}

template <class _Fp, class... _Args>
inline _LIBCPP_INLINE_VISIBILITY
future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
async(_Fp&& __f, _Args&&... __args)
{
    return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
                                    _VSTD::forward<_Args>(__args)...);
}

#endif  // _LIBCPP_HAS_NO_VARIADICS

// shared_future

template <class _Rp>
class _LIBCPP_VISIBLE shared_future
{
    __assoc_state<_Rp>* __state_;

public:
    _LIBCPP_INLINE_VISIBILITY
    shared_future() : __state_(nullptr) {}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
        {if (__state_) __state_->__add_shared();}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future(future<_Rp>&& __f) : __state_(__f.__state_)
        {__f.__state_ = nullptr;}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
        {__rhs.__state_ = nullptr;}
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~shared_future();
    shared_future& operator=(const shared_future& __rhs);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future& operator=(shared_future&& __rhs)
        {
            shared_future(std::move(__rhs)).swap(*this);
            return *this;
        }
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    // retrieving the value
    _LIBCPP_INLINE_VISIBILITY
    const _Rp& get() const {return __state_->copy();}

    _LIBCPP_INLINE_VISIBILITY
    void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
shared_future<_Rp>::~shared_future()
{
    if (__state_)
        __state_->__release_shared();
}

template <class _Rp>
shared_future<_Rp>&
shared_future<_Rp>::operator=(const shared_future& __rhs)
{
    if (__rhs.__state_)
        __rhs.__state_->__add_shared();
    if (__state_)
        __state_->__release_shared();
    __state_ = __rhs.__state_;
    return *this;
}

template <class _Rp>
class _LIBCPP_VISIBLE shared_future<_Rp&>
{
    __assoc_state<_Rp&>* __state_;

public:
    _LIBCPP_INLINE_VISIBILITY
    shared_future() : __state_(nullptr) {}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
        {if (__state_) __state_->__add_shared();}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future(future<_Rp&>&& __f) : __state_(__f.__state_)
        {__f.__state_ = nullptr;}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
        {__rhs.__state_ = nullptr;}
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~shared_future();
    shared_future& operator=(const shared_future& __rhs);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future& operator=(shared_future&& __rhs)
        {
            shared_future(std::move(__rhs)).swap(*this);
            return *this;
        }
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    // retrieving the value
    _LIBCPP_INLINE_VISIBILITY
    _Rp& get() const {return __state_->copy();}

    _LIBCPP_INLINE_VISIBILITY
    void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
shared_future<_Rp&>::~shared_future()
{
    if (__state_)
        __state_->__release_shared();
}

template <class _Rp>
shared_future<_Rp&>&
shared_future<_Rp&>::operator=(const shared_future& __rhs)
{
    if (__rhs.__state_)
        __rhs.__state_->__add_shared();
    if (__state_)
        __state_->__release_shared();
    __state_ = __rhs.__state_;
    return *this;
}

template <>
class _LIBCPP_VISIBLE shared_future<void>
{
    __assoc_sub_state* __state_;

public:
    _LIBCPP_INLINE_VISIBILITY
    shared_future() : __state_(nullptr) {}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
        {if (__state_) __state_->__add_shared();}
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future(future<void>&& __f) : __state_(__f.__state_)
        {__f.__state_ = nullptr;}
    _LIBCPP_INLINE_VISIBILITY
    shared_future(shared_future&& __rhs) : __state_(__rhs.__state_)
        {__rhs.__state_ = nullptr;}
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    ~shared_future();
    shared_future& operator=(const shared_future& __rhs);
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    shared_future& operator=(shared_future&& __rhs)
        {
            shared_future(std::move(__rhs)).swap(*this);
            return *this;
        }
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    // retrieving the value
    _LIBCPP_INLINE_VISIBILITY
    void get() const {__state_->copy();}

    _LIBCPP_INLINE_VISIBILITY
    void swap(shared_future& __rhs) {_VSTD::swap(__state_, __rhs.__state_);}

    // functions to check state
    _LIBCPP_INLINE_VISIBILITY
    bool valid() const {return __state_ != nullptr;}

    _LIBCPP_INLINE_VISIBILITY
    void wait() const {__state_->wait();}
    template <class _Rep, class _Period>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
            {return __state_->wait_for(__rel_time);}
    template <class _Clock, class _Duration>
        _LIBCPP_INLINE_VISIBILITY
        future_status
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
            {return __state_->wait_until(__abs_time);}
};

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y)
{
    __x.swap(__y);
}

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
shared_future<_Rp>
future<_Rp>::share()
{
    return shared_future<_Rp>(_VSTD::move(*this));
}

template <class _Rp>
inline _LIBCPP_INLINE_VISIBILITY
shared_future<_Rp&>
future<_Rp&>::share()
{
    return shared_future<_Rp&>(_VSTD::move(*this));
}

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

inline _LIBCPP_INLINE_VISIBILITY
shared_future<void>
future<void>::share()
{
    return shared_future<void>(_VSTD::move(*this));
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

_LIBCPP_END_NAMESPACE_STD

#endif  // _LIBCPP_FUTURE
@


1.2.2.3
log
@## SVN ##
## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/ 243376
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ## r243376 | dim | 2012-11-21 18:38:56 +0000 (Wed, 21 Nov 2012) | 14 lines
## SVN ##
## SVN ## MFC r241903:
## SVN ##
## SVN ##   Import libc++ trunk r165949.  Among other improvements and bug fixes,
## SVN ##   this has many visibility problems fixed, which should help with
## SVN ##   compiling certain ports that exercise C++11 mode (i.e. Firefox).
## SVN ##
## SVN ##   Also, belatedly add the LICENSE.TXT and accompanying CREDITS.TXT files,
## SVN ##   which are referred to in all the source files.
## SVN ##
## SVN ## MFC r241907:
## SVN ##
## SVN ##   Fix two -Wsystem-header warnings in libc++ that were exposed by the new
## SVN ##   ATF import.  These have also been sent upstream.
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ##
@
text
@d43 2
a44 2
error_code make_error_code(future_errc e) noexcept;
error_condition make_error_condition(future_errc e) noexcept;
d46 1
a46 1
const error_category& future_category() noexcept;
d54 2
a55 2
    const error_code& code() const noexcept;
    const char*       what() const noexcept;
d65 1
a65 1
    promise(promise&& rhs) noexcept;
d70 1
a70 1
    promise& operator=(promise&& rhs) noexcept;
d72 1
a72 1
    void swap(promise& other) noexcept;
d95 1
a95 1
    promise(promise&& rhs) noexcept;
d100 1
a100 1
    promise& operator=(promise&& rhs) noexcept;
d102 1
a102 1
    void swap(promise& other) noexcept;
d123 1
a123 1
    promise(promise&& rhs) noexcept;
d128 1
a128 1
    promise& operator=(promise&& rhs) noexcept;
d130 1
a130 1
    void swap(promise& other) noexcept;
d144 1
a144 1
template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
d153 2
a154 2
    future() noexcept;
    future(future&&) noexcept;
d158 2
a159 2
    future& operator=(future&&) noexcept;
    shared_future<R> share();
d165 1
a165 1
    bool valid() const noexcept;
d180 2
a181 2
    future() noexcept;
    future(future&&) noexcept;
d185 2
a186 2
    future& operator=(future&&) noexcept;
    shared_future<R&> share();
d192 1
a192 1
    bool valid() const noexcept;
d207 2
a208 2
    future() noexcept;
    future(future&&) noexcept;
d212 2
a213 2
    future& operator=(future&&) noexcept;
    shared_future<void> share();
d219 1
a219 1
    bool valid() const noexcept;
d234 1
a234 1
    shared_future() noexcept;
d236 2
a237 2
    shared_future(future<R>&&) noexcept;
    shared_future(shared_future&& rhs) noexcept;
d240 1
a240 1
    shared_future& operator=(shared_future&& rhs) noexcept;
d246 1
a246 1
    bool valid() const noexcept;
d261 1
a261 1
    shared_future() noexcept;
d263 2
a264 2
    shared_future(future<R&>&&) noexcept;
    shared_future(shared_future&& rhs) noexcept;
d267 1
a267 1
    shared_future& operator=(shared_future&& rhs) noexcept;
d273 1
a273 1
    bool valid() const noexcept;
d288 1
a288 1
    shared_future() noexcept;
d290 2
a291 2
    shared_future(future<void>&&) noexcept;
    shared_future(shared_future&& rhs) noexcept;
d294 1
a294 1
    shared_future& operator=(shared_future&& rhs) noexcept;
d300 1
a300 1
    bool valid() const noexcept;
d328 1
a328 1
    packaged_task() noexcept;
d336 2
a337 2
    packaged_task(const packaged_task&) = delete;
    packaged_task& operator=(const packaged_task&) = delete;
d340 3
a342 3
    packaged_task(packaged_task&& other) noexcept;
    packaged_task& operator=(packaged_task&& other) noexcept;
    void swap(packaged_task& other) noexcept;
d344 1
a344 1
    bool valid() const noexcept;
d357 1
a357 1
  void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
d416 1
a416 1
const error_category& future_category() _NOEXCEPT;
d420 1
a420 1
make_error_code(future_errc __e) _NOEXCEPT
d427 1
a427 1
make_error_condition(future_errc __e) _NOEXCEPT
d440 1
a440 1
    const error_code& code() const _NOEXCEPT {return __ec_;}
d758 1
d966 2
a967 2
template <class _Rp> class _LIBCPP_VISIBLE promise;
template <class _Rp> class _LIBCPP_VISIBLE shared_future;
d971 1
a971 1
template <class _Rp> class _LIBCPP_VISIBLE future;
d1013 1
a1013 1
    future() _NOEXCEPT : __state_(nullptr) {}
d1016 1
a1016 1
    future(future&& __rhs) _NOEXCEPT
d1021 1
a1021 1
    future& operator=(future&& __rhs) _NOEXCEPT
d1039 1
a1039 1
    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
d1043 1
a1043 1
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
d1117 1
a1117 1
    future() _NOEXCEPT : __state_(nullptr) {}
d1120 1
a1120 1
    future(future&& __rhs) _NOEXCEPT
d1125 1
a1125 1
    future& operator=(future&& __rhs) _NOEXCEPT
d1143 1
a1143 1
    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
d1147 1
a1147 1
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
d1216 1
a1216 1
    future() _NOEXCEPT : __state_(nullptr) {}
d1219 1
a1219 1
    future(future&& __rhs) _NOEXCEPT
d1224 1
a1224 1
    future& operator=(future&& __rhs) _NOEXCEPT
d1242 1
a1242 1
    void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
d1246 1
a1246 1
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
d1265 1
a1265 1
swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
d1280 1
a1280 1
    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
d1289 1
a1289 1
    promise(promise&& __rhs) _NOEXCEPT
d1302 1
a1302 1
    promise& operator=(promise&& __rhs) _NOEXCEPT
d1314 1
a1314 1
    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
d1458 1
a1458 1
    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
d1468 1
a1468 1
    promise(promise&& __rhs) _NOEXCEPT
d1481 1
a1481 1
    promise& operator=(promise&& __rhs) _NOEXCEPT
d1493 1
a1493 1
    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
d1601 1
a1601 1
    explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
d1611 1
a1611 1
    promise(promise&& __rhs) _NOEXCEPT
d1624 1
a1624 1
    promise& operator=(promise&& __rhs) _NOEXCEPT
d1636 1
a1636 1
    void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
d1664 1
a1664 1
swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
d1689 1
a1689 1
    virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
d1713 1
a1713 1
    virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
d1722 1
a1722 1
                              __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
d1765 1
a1765 1
    __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
d1771 2
a1772 2
    __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
    __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
d1779 1
a1779 1
    void swap(__packaged_task_function&) _NOEXCEPT;
d1785 1
a1785 1
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
d1857 1
a1857 1
__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
a1875 1
    return *this;
d1889 1
a1889 1
__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
d1945 1
a1945 1
    packaged_task() _NOEXCEPT : __p_(nullptr) {}
d1957 2
a1958 2
    packaged_task(const packaged_task&) = delete;
    packaged_task& operator=(const packaged_task&) = delete;
d1962 1
a1962 1
    packaged_task(packaged_task&& __other) _NOEXCEPT
d1965 1
a1965 1
    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
d1972 1
a1972 1
    void swap(packaged_task& __other) _NOEXCEPT
d1979 1
a1979 1
    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
d2060 1
a2060 1
    packaged_task() _NOEXCEPT : __p_(nullptr) {}
d2072 2
a2073 2
    packaged_task(const packaged_task&) = delete;
    packaged_task& operator=(const packaged_task&) = delete;
d2077 1
a2077 1
    packaged_task(packaged_task&& __other) _NOEXCEPT
d2080 1
a2080 1
    packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
d2087 1
a2087 1
    void swap(packaged_task& __other) _NOEXCEPT
d2094 1
a2094 1
    bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
d2167 1
a2167 1
swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
d2268 1
a2268 1
    shared_future() _NOEXCEPT : __state_(nullptr) {}
d2274 1
a2274 1
    shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
d2277 1
a2277 1
    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
d2284 1
a2284 1
    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
d2296 1
a2296 1
    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
d2300 1
a2300 1
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
d2342 1
a2342 1
    shared_future() _NOEXCEPT : __state_(nullptr) {}
d2348 1
a2348 1
    shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
d2351 1
a2351 1
    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
d2358 1
a2358 1
    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
d2370 1
a2370 1
    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
d2374 1
a2374 1
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
d2416 1
a2416 1
    shared_future() _NOEXCEPT : __state_(nullptr) {}
d2422 1
a2422 1
    shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
d2425 1
a2425 1
    shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
d2432 1
a2432 1
    shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
d2444 1
a2444 1
    void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
d2448 1
a2448 1
    bool valid() const _NOEXCEPT {return __state_ != nullptr;}
d2467 1
a2467 1
swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
@


1.2.2.4
log
@## SVN ##
## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/243683
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ## r243683 | dim | 2012-11-29 21:26:57 +0000 (Thu, 29 Nov 2012) | 4 lines
## SVN ##
## SVN ## MFC r242945 (by theraven):
## SVN ##
## SVN ##   Import new version of libc++ into base.
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ##
@
text
@d394 1
a394 1
struct _LIBCPP_VISIBLE is_error_code_enum<future_errc::__lx> : public true_type { };
@


1.2.2.5
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/250514
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@d390 1
a390 1
struct _LIBCPP_TYPE_VIS is_error_code_enum<future_errc> : public true_type {};
d394 1
a394 1
struct _LIBCPP_TYPE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
d415 1
a415 1
_LIBCPP_FUNC_VIS
d473 1
a473 5
    void __set_future_attached()
    {
        lock_guard<mutex> __lk(__mut_);
        __state_ |= __future_attached;
    }
d965 2
a966 2
template <class _Rp> class _LIBCPP_TYPE_VIS promise;
template <class _Rp> class _LIBCPP_TYPE_VIS shared_future;
d970 1
a970 1
template <class _Rp> class _LIBCPP_TYPE_VIS future;
d989 1
a989 1
class _LIBCPP_TYPE_VIS future
d1093 1
a1093 1
class _LIBCPP_TYPE_VIS future<_Rp&>
d1192 1
a1192 1
class _LIBCPP_TYPE_VIS future<void>
d1274 1
a1274 1
class _LIBCPP_TYPE_VIS promise
d1452 1
a1452 1
class _LIBCPP_TYPE_VIS promise<_Rp&>
d1595 1
a1595 1
class _LIBCPP_TYPE_VIS promise<void>
d1669 1
a1669 1
    struct _LIBCPP_TYPE_VIS uses_allocator<promise<_Rp>, _Alloc>
d1756 1
a1756 1
    typename aligned_storage<3*sizeof(void*)>::type __buf_;
d1933 1
a1933 1
class _LIBCPP_TYPE_VIS packaged_task<_Rp(_ArgTypes...)>
d2048 1
a2048 1
class _LIBCPP_TYPE_VIS packaged_task<void(_ArgTypes...)>
d2173 1
a2173 1
struct _LIBCPP_TYPE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
d2262 1
a2262 1
class _LIBCPP_TYPE_VIS shared_future
d2336 1
a2336 1
class _LIBCPP_TYPE_VIS shared_future<_Rp&>
d2410 1
a2410 1
class _LIBCPP_TYPE_VIS shared_future<void>
@


1.2.2.6
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/253222
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@a405 66
#ifndef _LIBCPP_HAS_NO_STRONG_ENUMS

#ifdef _LIBCXX_UNDERLYING_TYPE
typedef underlying_type<launch>::type __launch_underlying_type;
#else
typedef int __launch_underlying_type;
#endif

inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
launch
operator&(launch __x, launch __y)
{
    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
                               static_cast<__launch_underlying_type>(__y));
}

inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
launch
operator|(launch __x, launch __y)
{
    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
                               static_cast<__launch_underlying_type>(__y));
}

inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
launch
operator^(launch __x, launch __y)
{
    return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
                               static_cast<__launch_underlying_type>(__y));
}

inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
launch
operator~(launch __x)
{
    return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
}

inline _LIBCPP_INLINE_VISIBILITY
launch&
operator&=(launch& __x, launch __y)
{
    __x = __x & __y; return __x;
}

inline _LIBCPP_INLINE_VISIBILITY
launch&
operator|=(launch& __x, launch __y)
{
    __x = __x | __y; return __x;
}

inline _LIBCPP_INLINE_VISIBILITY
launch&
operator^=(launch& __x, launch __y)
{
    __x = __x ^ __y; return __x;
}

#endif  // !_LIBCPP_HAS_NO_STRONG_ENUMS

@


1.2.2.7
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/262801
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@d22 2
a23 1
    future_already_retrieved = 1,
d25 1
a25 2
    no_state,
    broken_promise
d312 1
a312 1
  future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
d316 1
a316 1
  future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
d382 2
a383 1
    future_already_retrieved = 1,
d385 1
a385 2
    no_state,
    broken_promise
d390 1
a390 1
struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc> : public true_type {};
d394 1
a394 1
struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum<future_errc::__lx> : public true_type { };
d511 1
a511 1
class _LIBCPP_TYPE_VIS __assoc_sub_state
d545 1
a545 1
    bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
d552 1
a552 1
    bool __is_ready() const {return (__state_ & ready) != 0;}
d730 1
a730 1
    __value_ = _VSTD::addressof(__arg);
d745 1
a745 1
    __value_ = _VSTD::addressof(__arg);
d781 1
a781 1
        reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
d1035 2
a1036 2
template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY promise;
template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY shared_future;
d1040 1
a1040 1
template <class _Rp> class _LIBCPP_TYPE_VIS_ONLY future;
d1059 1
a1059 1
class _LIBCPP_TYPE_VIS_ONLY future
d1163 1
a1163 1
class _LIBCPP_TYPE_VIS_ONLY future<_Rp&>
d1344 1
a1344 1
class _LIBCPP_TYPE_VIS_ONLY promise
d1522 1
a1522 1
class _LIBCPP_TYPE_VIS_ONLY promise<_Rp&>
d1739 1
a1739 1
    struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<promise<_Rp>, _Alloc>
d2003 1
a2003 1
class _LIBCPP_TYPE_VIS_ONLY packaged_task<_Rp(_ArgTypes...)>
d2016 1
a2016 9
    template <class _Fp,
              class = typename enable_if
              <
                  !is_same<
                      typename decay<_Fp>::type, 
                      packaged_task
                      >::value
                  >::type
             >
d2019 1
a2019 9
    template <class _Fp, class _Allocator,
              class = typename enable_if
              <
                  !is_same<
                      typename decay<_Fp>::type, 
                      packaged_task
                      >::value
                  >::type
              >
d2118 1
a2118 1
class _LIBCPP_TYPE_VIS_ONLY packaged_task<void(_ArgTypes...)>
d2131 1
a2131 9
    template <class _Fp,
              class = typename enable_if
              <
                  !is_same<
                      typename decay<_Fp>::type, 
                      packaged_task
                      >::value
                  >::type
              >
d2134 1
a2134 9
    template <class _Fp, class _Allocator,
              class = typename enable_if
              <
                  !is_same<
                      typename decay<_Fp>::type, 
                      packaged_task
                      >::value
                  >::type
              >    
d2243 1
a2243 1
struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<packaged_task<_Callable>, _Alloc>
a2301 3
inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
{ return (int(__policy) & int(__value)) != 0; }

d2308 3
a2310 7

#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif
        if (__does_policy_contain(__policy, launch::async))
        return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
d2312 2
a2313 7
#ifndef _LIBCPP_NO_EXCEPTIONS
    }
    catch ( ... ) { if (__policy == launch::async) throw ; }
#endif

    if (__does_policy_contain(__policy, launch::deferred))
        return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
d2315 1
a2315 1
    return future<_Rp>{};
d2332 1
a2332 1
class _LIBCPP_TYPE_VIS_ONLY shared_future
d2406 1
a2406 1
class _LIBCPP_TYPE_VIS_ONLY shared_future<_Rp&>
@


1.1
log
@SVN rev 227983 on 2011-11-25 20:59:04Z by theraven

Import libc++ / libcxxrt into base.  Not build by default yet (use
MK_LIBCPLUSPLUS=yes to enable).  This is a work-in-progress.  It works for
me, but is not guaranteed to work for anyone else and may eat your dog.

To build C++ using libc++, add -stdlib=libc++ to your CXX and LD flags.

Bug reports welcome, bug fixes even more welcome...

Approved by:	dim (mentor)
@
text
@d380 1
a380 1
struct _LIBCPP_VISIBLE future_errc
a381 1
enum _ {
d387 1
d389 2
a390 6
    _ __v_;

    _LIBCPP_INLINE_VISIBILITY future_errc(_ __v) : __v_(__v) {}
    _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;}

};
d392 1
d394 2
a395 1
struct _LIBCPP_VISIBLE is_error_code_enum<future_errc> : public true_type {};
d398 1
a398 1
struct _LIBCPP_VISIBLE launch
a399 1
enum _ {
d404 1
a404 7

    _ __v_;

    _LIBCPP_INLINE_VISIBILITY launch(_ __v) : __v_(__v) {}
    _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;}

};
d407 1
a407 1
struct _LIBCPP_VISIBLE future_status
a408 1
enum _ {
d413 1
a413 7

    _ __v_;

    _LIBCPP_INLINE_VISIBILITY future_status(_ __v) : __v_(__v) {}
    _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;}

};
d525 1
a525 1
template <class _R>
d530 1
a530 1
    typedef typename aligned_storage<sizeof(_R), alignment_of<_R>::value>::type _U;
d532 1
a532 1
    _U __value_;
d551 2
a552 2
    _R move();
    typename add_lvalue_reference<_R>::type copy();
d555 1
a555 1
template <class _R>
d557 1
a557 1
__assoc_state<_R>::__on_zero_shared() _NOEXCEPT
d560 1
a560 1
        reinterpret_cast<_R*>(&__value_)->~_R();
d564 1
a564 1
template <class _R>
d568 1
a568 1
__assoc_state<_R>::set_value(_Arg&& __arg)
d570 1
a570 1
__assoc_state<_R>::set_value(_Arg& __arg)
d578 1
a578 1
    ::new(&__value_) _R(_VSTD::forward<_Arg>(__arg));
d584 1
a584 1
template <class _R>
d588 1
a588 1
__assoc_state<_R>::set_value_at_thread_exit(_Arg&& __arg)
d590 1
a590 1
__assoc_state<_R>::set_value_at_thread_exit(_Arg& __arg)
d598 1
a598 1
    ::new(&__value_) _R(_VSTD::forward<_Arg>(__arg));
d604 3
a606 3
template <class _R>
_R
__assoc_state<_R>::move()
d612 1
a612 1
    return _VSTD::move(*reinterpret_cast<_R*>(&__value_));
d615 3
a617 3
template <class _R>
typename add_lvalue_reference<_R>::type
__assoc_state<_R>::copy()
d623 1
a623 1
    return *reinterpret_cast<_R*>(&__value_);
d626 2
a627 2
template <class _R>
class __assoc_state<_R&>
d631 1
a631 1
    typedef _R* _U;
d633 1
a633 1
    _U __value_;
d638 2
a639 2
    void set_value(_R& __arg);
    void set_value_at_thread_exit(_R& __arg);
d641 1
a641 1
    _R& copy();
d644 1
a644 1
template <class _R>
d646 1
a646 1
__assoc_state<_R&>::__on_zero_shared() _NOEXCEPT
d651 1
a651 1
template <class _R>
d653 1
a653 1
__assoc_state<_R&>::set_value(_R& __arg)
d666 1
a666 1
template <class _R>
d668 1
a668 1
__assoc_state<_R&>::set_value_at_thread_exit(_R& __arg)
d681 3
a683 3
template <class _R>
_R&
__assoc_state<_R&>::copy()
d692 1
a692 1
template <class _R, class _Alloc>
d694 1
a694 1
    : public __assoc_state<_R>
d696 1
a696 1
    typedef __assoc_state<_R> base;
d706 1
a706 1
template <class _R, class _Alloc>
d708 1
a708 1
__assoc_state_alloc<_R, _Alloc>::__on_zero_shared() _NOEXCEPT
d711 1
a711 1
        reinterpret_cast<_R*>(&this->__value_)->~_R();
d717 3
a719 3
template <class _R, class _Alloc>
class __assoc_state_alloc<_R&, _Alloc>
    : public __assoc_state<_R&>
d721 1
a721 1
    typedef __assoc_state<_R&> base;
d731 1
a731 1
template <class _R, class _Alloc>
d733 1
a733 1
__assoc_state_alloc<_R&, _Alloc>::__on_zero_shared() _NOEXCEPT
d764 1
a764 1
template <class _R, class _F>
d766 1
a766 1
    : public __assoc_state<_R>
d768 1
a768 1
    typedef __assoc_state<_R> base;
d770 1
a770 1
    _F __func_;
d774 1
a774 1
    explicit __deferred_assoc_state(_F&& __f);
d782 1
a782 1
template <class _R, class _F>
d784 2
a785 2
__deferred_assoc_state<_R, _F>::__deferred_assoc_state(_F&& __f)
    : __func_(_VSTD::forward<_F>(__f))
d792 1
a792 1
template <class _R, class _F>
d794 1
a794 1
__deferred_assoc_state<_R, _F>::__execute()
d810 2
a811 2
template <class _F>
class __deferred_assoc_state<void, _F>
d816 1
a816 1
    _F __func_;
d820 1
a820 1
    explicit __deferred_assoc_state(_F&& __f);
d828 1
a828 1
template <class _F>
d830 2
a831 2
__deferred_assoc_state<void, _F>::__deferred_assoc_state(_F&& __f)
    : __func_(_VSTD::forward<_F>(__f))
d838 1
a838 1
template <class _F>
d840 1
a840 1
__deferred_assoc_state<void, _F>::__execute()
d857 1
a857 1
template <class _R, class _F>
d859 1
a859 1
    : public __assoc_state<_R>
d861 1
a861 1
    typedef __assoc_state<_R> base;
d863 1
a863 1
    _F __func_;
d868 1
a868 1
    explicit __async_assoc_state(_F&& __f);
d876 1
a876 1
template <class _R, class _F>
d878 2
a879 2
__async_assoc_state<_R, _F>::__async_assoc_state(_F&& __f)
    : __func_(_VSTD::forward<_F>(__f))
d885 1
a885 1
template <class _R, class _F>
d887 1
a887 1
__async_assoc_state<_R, _F>::__execute()
d903 1
a903 1
template <class _R, class _F>
d905 1
a905 1
__async_assoc_state<_R, _F>::__on_zero_shared() _NOEXCEPT
d911 2
a912 2
template <class _F>
class __async_assoc_state<void, _F>
d917 1
a917 1
    _F __func_;
d922 1
a922 1
    explicit __async_assoc_state(_F&& __f);
d930 1
a930 1
template <class _F>
d932 2
a933 2
__async_assoc_state<void, _F>::__async_assoc_state(_F&& __f)
    : __func_(_VSTD::forward<_F>(__f))
d939 1
a939 1
template <class _F>
d941 1
a941 1
__async_assoc_state<void, _F>::__execute()
d958 1
a958 1
template <class _F>
d960 1
a960 1
__async_assoc_state<void, _F>::__on_zero_shared() _NOEXCEPT
d966 2
a967 2
template <class _R> class promise;
template <class _R> class shared_future;
d971 1
a971 1
template <class _R> class future;
d973 2
a974 2
template <class _R, class _F>
future<_R>
d976 1
a976 1
__make_deferred_assoc_state(_F&& __f);
d978 1
a978 1
__make_deferred_assoc_state(_F __f);
d981 2
a982 2
template <class _R, class _F>
future<_R>
d984 1
a984 1
__make_async_assoc_state(_F&& __f);
d986 1
a986 1
__make_async_assoc_state(_F __f);
d989 1
a989 1
template <class _R>
d992 1
a992 1
    __assoc_state<_R>* __state_;
d994 1
a994 1
    explicit future(__assoc_state<_R>* __state);
d1000 4
a1003 4
    template <class _R1, class _F>
        friend future<_R1> __make_deferred_assoc_state(_F&& __f);
    template <class _R1, class _F>
        friend future<_R1> __make_async_assoc_state(_F&& __f);
d1005 4
a1008 4
    template <class _R1, class _F>
        friend future<_R1> __make_deferred_assoc_state(_F __f);
    template <class _R1, class _F>
        friend future<_R1> __make_async_assoc_state(_F __f);
d1033 1
a1033 1
    shared_future<_R> share();
d1036 1
a1036 1
    _R get();
d1059 2
a1060 2
template <class _R>
future<_R>::future(__assoc_state<_R>* __state)
d1076 2
a1077 2
template <class _R>
future<_R>::~future()
d1083 3
a1085 3
template <class _R>
_R
future<_R>::get()
d1088 1
a1088 1
    __assoc_state<_R>* __s = __state_;
d1093 2
a1094 2
template <class _R>
class _LIBCPP_VISIBLE future<_R&>
d1096 1
a1096 1
    __assoc_state<_R&>* __state_;
d1098 1
a1098 1
    explicit future(__assoc_state<_R&>* __state);
d1104 4
a1107 4
    template <class _R1, class _F>
        friend future<_R1> __make_deferred_assoc_state(_F&& __f);
    template <class _R1, class _F>
        friend future<_R1> __make_async_assoc_state(_F&& __f);
d1109 4
a1112 4
    template <class _R1, class _F>
        friend future<_R1> __make_deferred_assoc_state(_F __f);
    template <class _R1, class _F>
        friend future<_R1> __make_async_assoc_state(_F __f);
d1137 1
a1137 1
    shared_future<_R&> share();
d1140 1
a1140 1
    _R& get();
d1163 2
a1164 2
template <class _R>
future<_R&>::future(__assoc_state<_R&>* __state)
d1175 2
a1176 2
template <class _R>
future<_R&>::~future()
d1182 3
a1184 3
template <class _R>
_R&
future<_R&>::get()
d1187 1
a1187 1
    __assoc_state<_R&>* __s = __state_;
d1203 4
a1206 4
    template <class _R1, class _F>
        friend future<_R1> __make_deferred_assoc_state(_F&& __f);
    template <class _R1, class _F>
        friend future<_R1> __make_async_assoc_state(_F&& __f);
d1208 4
a1211 4
    template <class _R1, class _F>
        friend future<_R1> __make_deferred_assoc_state(_F __f);
    template <class _R1, class _F>
        friend future<_R1> __make_async_assoc_state(_F __f);
d1262 1
a1262 1
template <class _R>
d1265 1
a1265 1
swap(future<_R>& __x, future<_R>& __y)
d1274 1
a1274 1
template <class _R>
d1277 1
a1277 1
    __assoc_state<_R>* __state_;
d1317 1
a1317 1
    future<_R> get_future();
d1320 1
a1320 1
    void set_value(const _R& __r);
d1322 1
a1322 1
    void set_value(_R&& __r);
d1327 1
a1327 1
    void set_value_at_thread_exit(const _R& __r);
d1329 1
a1329 1
    void set_value_at_thread_exit(_R&& __r);
d1334 3
a1336 3
template <class _R>
promise<_R>::promise()
    : __state_(new __assoc_state<_R>)
d1340 1
a1340 1
template <class _R>
d1342 1
a1342 1
promise<_R>::promise(allocator_arg_t, const _Alloc& __a0)
d1344 1
a1344 1
    typedef typename _Alloc::template rebind<__assoc_state_alloc<_R, _Alloc> >::other _A2;
d1347 2
a1348 2
    unique_ptr<__assoc_state_alloc<_R, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
    ::new(__hold.get()) __assoc_state_alloc<_R, _Alloc>(__a0);
d1352 2
a1353 2
template <class _R>
promise<_R>::~promise()
d1365 3
a1367 3
template <class _R>
future<_R>
promise<_R>::get_future()
d1373 1
a1373 1
    return future<_R>(__state_);
d1376 1
a1376 1
template <class _R>
d1378 1
a1378 1
promise<_R>::set_value(const _R& __r)
d1389 1
a1389 1
template <class _R>
d1391 1
a1391 1
promise<_R>::set_value(_R&& __r)
d1402 1
a1402 1
template <class _R>
d1404 1
a1404 1
promise<_R>::set_exception(exception_ptr __p)
d1413 1
a1413 1
template <class _R>
d1415 1
a1415 1
promise<_R>::set_value_at_thread_exit(const _R& __r)
d1426 1
a1426 1
template <class _R>
d1428 1
a1428 1
promise<_R>::set_value_at_thread_exit(_R&& __r)
d1439 1
a1439 1
template <class _R>
d1441 1
a1441 1
promise<_R>::set_exception_at_thread_exit(exception_ptr __p)
d1452 2
a1453 2
template <class _R>
class _LIBCPP_VISIBLE promise<_R&>
d1455 1
a1455 1
    __assoc_state<_R&>* __state_;
d1496 1
a1496 1
    future<_R&> get_future();
d1499 1
a1499 1
    void set_value(_R& __r);
d1503 1
a1503 1
    void set_value_at_thread_exit(_R&);
d1507 3
a1509 3
template <class _R>
promise<_R&>::promise()
    : __state_(new __assoc_state<_R&>)
d1513 1
a1513 1
template <class _R>
d1515 1
a1515 1
promise<_R&>::promise(allocator_arg_t, const _Alloc& __a0)
d1517 1
a1517 1
    typedef typename _Alloc::template rebind<__assoc_state_alloc<_R&, _Alloc> >::other _A2;
d1520 2
a1521 2
    unique_ptr<__assoc_state_alloc<_R&, _Alloc>, _D2> __hold(__a.allocate(1), _D2(__a, 1));
    ::new(__hold.get()) __assoc_state_alloc<_R&, _Alloc>(__a0);
d1525 2
a1526 2
template <class _R>
promise<_R&>::~promise()
d1538 3
a1540 3
template <class _R>
future<_R&>
promise<_R&>::get_future()
d1546 1
a1546 1
    return future<_R&>(__state_);
d1549 1
a1549 1
template <class _R>
d1551 1
a1551 1
promise<_R&>::set_value(_R& __r)
d1560 1
a1560 1
template <class _R>
d1562 1
a1562 1
promise<_R&>::set_exception(exception_ptr __p)
d1571 1
a1571 1
template <class _R>
d1573 1
a1573 1
promise<_R&>::set_value_at_thread_exit(_R& __r)
d1582 1
a1582 1
template <class _R>
d1584 1
a1584 1
promise<_R&>::set_exception_at_thread_exit(exception_ptr __p)
d1661 1
a1661 1
template <class _R>
d1664 1
a1664 1
swap(promise<_R>& __x, promise<_R>& __y)
d1669 2
a1670 2
template <class _R, class _Alloc>
    struct _LIBCPP_VISIBLE uses_allocator<promise<_R>, _Alloc>
d1679 2
a1680 2
template<class _R, class ..._ArgTypes>
class __packaged_task_base<_R(_ArgTypes...)>
d1692 1
a1692 1
    virtual _R operator()(_ArgTypes&& ...) = 0;
d1697 3
a1699 3
template<class _F, class _Alloc, class _R, class ..._ArgTypes>
class __packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>
    : public  __packaged_task_base<_R(_ArgTypes...)>
d1701 1
a1701 1
    __compressed_pair<_F, _Alloc> __f_;
d1704 1
a1704 1
    explicit __packaged_task_func(const _F& __f) : __f_(__f) {}
d1706 1
a1706 1
    explicit __packaged_task_func(_F&& __f) : __f_(_VSTD::move(__f)) {}
d1708 1
a1708 1
    __packaged_task_func(const _F& __f, const _Alloc& __a)
d1711 1
a1711 1
    __packaged_task_func(_F&& __f, const _Alloc& __a)
d1713 1
a1713 1
    virtual void __move_to(__packaged_task_base<_R(_ArgTypes...)>*);
d1716 1
a1716 1
    virtual _R operator()(_ArgTypes&& ... __args);
d1719 1
a1719 1
template<class _F, class _Alloc, class _R, class ..._ArgTypes>
d1721 2
a1722 2
__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::__move_to(
                              __packaged_task_base<_R(_ArgTypes...)>* __p)
d1727 1
a1727 1
template<class _F, class _Alloc, class _R, class ..._ArgTypes>
d1729 1
a1729 1
__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy()
d1731 1
a1731 1
    __f_.~__compressed_pair<_F, _Alloc>();
d1734 1
a1734 1
template<class _F, class _Alloc, class _R, class ..._ArgTypes>
d1736 1
a1736 1
__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::destroy_deallocate()
d1738 3
a1740 3
    typedef typename _Alloc::template rebind<__packaged_task_func>::other _A;
    _A __a(__f_.second());
    __f_.~__compressed_pair<_F, _Alloc>();
d1744 3
a1746 3
template<class _F, class _Alloc, class _R, class ..._ArgTypes>
_R
__packaged_task_func<_F, _Alloc, _R(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
d1753 2
a1754 2
template<class _R, class ..._ArgTypes>
class __packaged_task_function<_R(_ArgTypes...)>
d1756 1
a1756 1
    typedef __packaged_task_base<_R(_ArgTypes...)> __base;
d1761 1
a1761 1
    typedef _R result_type;
d1766 4
a1769 4
    template<class _F>
      __packaged_task_function(_F&& __f);
    template<class _F, class _Alloc>
      __packaged_task_function(allocator_arg_t, const _Alloc& __a, _F&& __f);
d1781 1
a1781 1
    _R operator()(_ArgTypes...) const;
d1784 2
a1785 2
template<class _R, class ..._ArgTypes>
__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f)
d1801 3
a1803 3
template<class _R, class ..._ArgTypes>
template <class _F>
__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(_F&& __f)
d1806 2
a1807 2
    typedef typename remove_reference<_F>::type _FR;
    typedef __packaged_task_func<_FR, allocator<_FR>, _R(_ArgTypes...)> _FF;
d1811 1
a1811 1
        ::new (__f_) _FF(_VSTD::forward<_F>(__f));
d1815 5
a1819 5
        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(_VSTD::forward<_F>(__f), allocator<_FR>(__a));
d1824 4
a1827 4
template<class _R, class ..._ArgTypes>
template <class _F, class _Alloc>
__packaged_task_function<_R(_ArgTypes...)>::__packaged_task_function(
                                  allocator_arg_t, const _Alloc& __a0, _F&& __f)
d1831 2
a1832 2
    typedef typename remove_reference<_F>::type _FR;
    typedef __packaged_task_func<_FR, _Alloc, _R(_ArgTypes...)> _FF;
d1836 1
a1836 1
        ::new (__f_) _FF(_VSTD::forward<_F>(__f));
d1846 5
a1850 5
                                                     _A;
        _A __a(__a0);
        typedef __allocator_destructor<_A> _D;
        unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
        ::new (__hold.get()) _FF(_VSTD::forward<_F>(__f), _Alloc(__a));
d1855 3
a1857 3
template<class _R, class ..._ArgTypes>
__packaged_task_function<_R(_ArgTypes...)>&
__packaged_task_function<_R(_ArgTypes...)>::operator=(__packaged_task_function&& __f)
d1878 2
a1879 2
template<class _R, class ..._ArgTypes>
__packaged_task_function<_R(_ArgTypes...)>::~__packaged_task_function()
d1887 1
a1887 1
template<class _R, class ..._ArgTypes>
d1889 1
a1889 1
__packaged_task_function<_R(_ArgTypes...)>::swap(__packaged_task_function& __f)
d1924 1
a1924 1
template<class _R, class ..._ArgTypes>
d1926 2
a1927 2
_R
__packaged_task_function<_R(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
d1932 2
a1933 2
template<class _R, class ..._ArgTypes>
class _LIBCPP_VISIBLE packaged_task<_R(_ArgTypes...)>
d1936 1
a1936 1
    typedef _R result_type;
d1946 1
a1946 1
    template <class _F>
d1948 2
a1949 2
        explicit packaged_task(_F&& __f) : __f_(_VSTD::forward<_F>(__f)) {}
    template <class _F, class _Allocator>
d1951 2
a1952 2
        explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f)
             : __f_(allocator_arg, __a, _VSTD::forward<_F>(__f)),
d1992 1
a1992 1
template<class _R, class ..._ArgTypes>
d1994 1
a1994 1
packaged_task<_R(_ArgTypes...)>::operator()(_ArgTypes... __args)
d2014 1
a2014 1
template<class _R, class ..._ArgTypes>
d2016 1
a2016 1
packaged_task<_R(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
d2036 1
a2036 1
template<class _R, class ..._ArgTypes>
d2038 1
a2038 1
packaged_task<_R(_ArgTypes...)>::reset()
d2061 1
a2061 1
    template <class _F>
d2063 2
a2064 2
        explicit packaged_task(_F&& __f) : __f_(_VSTD::forward<_F>(__f)) {}
    template <class _F, class _Allocator>
d2066 2
a2067 2
        explicit packaged_task(allocator_arg_t, const _Allocator& __a, _F&& __f)
             : __f_(allocator_arg, __a, _VSTD::forward<_F>(__f)),
d2176 2
a2177 2
template <class _R, class _F>
future<_R>
d2179 1
a2179 1
__make_deferred_assoc_state(_F&& __f)
d2181 1
a2181 1
__make_deferred_assoc_state(_F __f)
d2184 3
a2186 3
    unique_ptr<__deferred_assoc_state<_R, _F>, __release_shared_count>
        __h(new __deferred_assoc_state<_R, _F>(_VSTD::forward<_F>(__f)));
    return future<_R>(__h.get());
d2189 2
a2190 2
template <class _R, class _F>
future<_R>
d2192 1
a2192 1
__make_async_assoc_state(_F&& __f)
d2194 1
a2194 1
__make_async_assoc_state(_F __f)
d2197 4
a2200 4
    unique_ptr<__async_assoc_state<_R, _F>, __release_shared_count>
        __h(new __async_assoc_state<_R, _F>(_VSTD::forward<_F>(__f)));
    _VSTD::thread(&__async_assoc_state<_R, _F>::__execute, __h.get()).detach();
    return future<_R>(__h.get());
d2203 1
a2203 1
template <class _F, class... _Args>
d2206 1
a2206 1
    tuple<_F, _Args...> __f_;
d2209 1
a2209 1
    typedef typename __invoke_of<_F, _Args...>::type _R;
d2212 1
a2212 1
    explicit __async_func(_F&& __f, _Args&&... __args)
d2218 1
a2218 1
    _R operator()()
d2225 1
a2225 1
    _R
d2232 9
a2240 9
template <class _F, class... _Args>
future<typename __invoke_of<typename decay<_F>::type, typename decay<_Args>::type...>::type>
async(launch __policy, _F&& __f, _Args&&... __args)
{
    typedef __async_func<typename decay<_F>::type, typename decay<_Args>::type...> _BF;
    typedef typename _BF::_R _R;
    future<_R> __r;
    if (__policy & launch::async)
        __r = _VSTD::__make_async_assoc_state<_R>(_BF(__decay_copy(_VSTD::forward<_F>(__f)),
d2242 2
a2243 2
    else if (__policy & launch::deferred)
        __r = _VSTD::__make_deferred_assoc_state<_R>(_BF(__decay_copy(_VSTD::forward<_F>(__f)),
d2248 1
a2248 1
template <class _F, class... _Args>
d2250 2
a2251 2
future<typename __invoke_of<typename decay<_F>::type, typename decay<_Args>::type...>::type>
async(_F&& __f, _Args&&... __args)
d2253 1
a2253 1
    return _VSTD::async(launch::any, _VSTD::forward<_F>(__f),
d2261 1
a2261 1
template <class _R>
d2264 1
a2264 1
    __assoc_state<_R>* __state_;
d2274 1
a2274 1
    shared_future(future<_R>&& __f) : __state_(__f.__state_)
d2293 1
a2293 1
    const _R& get() const {return __state_->copy();}
d2316 2
a2317 2
template <class _R>
shared_future<_R>::~shared_future()
d2323 3
a2325 3
template <class _R>
shared_future<_R>&
shared_future<_R>::operator=(const shared_future& __rhs)
d2335 2
a2336 2
template <class _R>
class _LIBCPP_VISIBLE shared_future<_R&>
d2338 1
a2338 1
    __assoc_state<_R&>* __state_;
d2348 1
a2348 1
    shared_future(future<_R&>&& __f) : __state_(__f.__state_)
d2367 1
a2367 1
    _R& get() const {return __state_->copy();}
d2390 2
a2391 2
template <class _R>
shared_future<_R&>::~shared_future()
d2397 3
a2399 3
template <class _R>
shared_future<_R&>&
shared_future<_R&>::operator=(const shared_future& __rhs)
d2464 1
a2464 1
template <class _R>
d2467 1
a2467 1
swap(shared_future<_R>& __x, shared_future<_R>& __y)
d2472 1
a2472 1
template <class _R>
d2474 2
a2475 2
shared_future<_R>
future<_R>::share()
d2477 1
a2477 1
    return shared_future<_R>(_VSTD::move(*this));
d2480 1
a2480 1
template <class _R>
d2482 2
a2483 2
shared_future<_R&>
future<_R&>::share()
d2485 1
a2485 1
    return shared_future<_R&>(_VSTD::move(*this));
@

