[futures.atomic_future] and notify_all_at_thread_exit. This completes the header <future> and all of Chapter 30 (for C++0x enabled compilers).

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@113017 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant
2010-09-03 21:46:37 +00:00
parent 99be8237db
commit e6e4d01553
17 changed files with 1010 additions and 3 deletions

View File

@@ -439,7 +439,7 @@ template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
#include <memory>
#include <chrono>
#include <exception>
#include <__mutex_base>
#include <mutex>
#include <thread>
#pragma GCC system_header
@@ -2066,6 +2066,8 @@ async(_F&& __f, _Args&&... __args)
#endif // _LIBCPP_HAS_NO_VARIADICS
// shared_future
template <class _R>
class shared_future
{
@@ -2244,6 +2246,200 @@ swap(shared_future<_R>& __x, shared_future<_R>& __y)
__x.swap(__y);
}
// atomic_future
template <class _R>
class atomic_future
{
__assoc_state<_R>* __state_;
mutable mutex __mut_;
public:
atomic_future() : __state_(nullptr) {}
atomic_future(const atomic_future& __rhs) : __state_(__rhs.__state_)
{if (__state_) __state_->__add_shared();}
#ifdef _LIBCPP_MOVE
atomic_future(future<_R>&& __f) : __state_(__f.__state_)
{__f.__state_ = nullptr;}
#endif // _LIBCPP_MOVE
~atomic_future();
atomic_future& operator=(const atomic_future& __rhs);
// retrieving the value
const _R& get() const {return __state_->copy();}
void swap(atomic_future& __rhs);
// functions to check state
bool valid() const {return __state_ != nullptr;}
void wait() const {__state_->wait();}
template <class _Rep, class _Period>
future_status
wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
{return __state_->wait_for(__rel_time);}
template <class _Clock, class _Duration>
future_status
wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
{return __state_->wait_until(__abs_time);}
};
template <class _R>
atomic_future<_R>::~atomic_future()
{
if (__state_)
__state_->__release_shared();
}
template <class _R>
atomic_future<_R>&
atomic_future<_R>::operator=(const atomic_future& __rhs)
{
if (this != &__rhs)
{
unique_lock<mutex> __this(__mut_, defer_lock);
unique_lock<mutex> __that(__rhs.__mut_, defer_lock);
_STD::lock(__this, __that);
if (__rhs.__state_)
__rhs.__state_->__add_shared();
if (__state_)
__state_->__release_shared();
__state_ = __rhs.__state_;
}
return *this;
}
template <class _R>
void
atomic_future<_R>::swap(atomic_future& __rhs)
{
if (this != &__rhs)
{
unique_lock<mutex> __this(__mut_, defer_lock);
unique_lock<mutex> __that(__rhs.__mut_, defer_lock);
_STD::lock(__this, __that);
_STD::swap(__state_, __rhs.__state_);
}
}
template <class _R>
class atomic_future<_R&>
{
__assoc_state<_R&>* __state_;
mutable mutex __mut_;
public:
atomic_future() : __state_(nullptr) {}
atomic_future(const atomic_future& __rhs) : __state_(__rhs.__state_)
{if (__state_) __state_->__add_shared();}
#ifdef _LIBCPP_MOVE
atomic_future(future<_R&>&& __f) : __state_(__f.__state_)
{__f.__state_ = nullptr;}
#endif // _LIBCPP_MOVE
~atomic_future();
atomic_future& operator=(const atomic_future& __rhs);
// retrieving the value
_R& get() const {return __state_->copy();}
void swap(atomic_future& __rhs);
// functions to check state
bool valid() const {return __state_ != nullptr;}
void wait() const {__state_->wait();}
template <class _Rep, class _Period>
future_status
wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
{return __state_->wait_for(__rel_time);}
template <class _Clock, class _Duration>
future_status
wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
{return __state_->wait_until(__abs_time);}
};
template <class _R>
atomic_future<_R&>::~atomic_future()
{
if (__state_)
__state_->__release_shared();
}
template <class _R>
atomic_future<_R&>&
atomic_future<_R&>::operator=(const atomic_future& __rhs)
{
if (this != &__rhs)
{
unique_lock<mutex> __this(__mut_, defer_lock);
unique_lock<mutex> __that(__rhs.__mut_, defer_lock);
_STD::lock(__this, __that);
if (__rhs.__state_)
__rhs.__state_->__add_shared();
if (__state_)
__state_->__release_shared();
__state_ = __rhs.__state_;
}
return *this;
}
template <class _R>
void
atomic_future<_R&>::swap(atomic_future& __rhs)
{
if (this != &__rhs)
{
unique_lock<mutex> __this(__mut_, defer_lock);
unique_lock<mutex> __that(__rhs.__mut_, defer_lock);
_STD::lock(__this, __that);
_STD::swap(__state_, __rhs.__state_);
}
}
template <>
class atomic_future<void>
{
__assoc_sub_state* __state_;
mutable mutex __mut_;
public:
atomic_future() : __state_(nullptr) {}
atomic_future(const atomic_future& __rhs) : __state_(__rhs.__state_)
{if (__state_) __state_->__add_shared();}
#ifdef _LIBCPP_MOVE
atomic_future(future<void>&& __f) : __state_(__f.__state_)
{__f.__state_ = nullptr;}
#endif // _LIBCPP_MOVE
~atomic_future();
atomic_future& operator=(const atomic_future& __rhs);
// retrieving the value
void get() const {__state_->copy();}
void swap(atomic_future& __rhs);
// functions to check state
bool valid() const {return __state_ != nullptr;}
void wait() const {__state_->wait();}
template <class _Rep, class _Period>
future_status
wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
{return __state_->wait_for(__rel_time);}
template <class _Clock, class _Duration>
future_status
wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
{return __state_->wait_until(__abs_time);}
};
template <class _R>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(atomic_future<_R>& __x, atomic_future<_R>& __y)
{
__x.swap(__y);
}
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_FUTURE