From 99be8237db0c69ea05bb82bbb8fc8a2273c05743 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Fri, 3 Sep 2010 18:39:25 +0000 Subject: [PATCH] [futures.shared_future] git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@112990 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/future | 198 +++++++++++++++++- src/future.cpp | 17 ++ .../copy_assign.pass.cpp | 74 +++++++ .../futures.shared_future/copy_ctor.pass.cpp | 66 ++++++ .../ctor_future.pass.cpp | 66 ++++++ .../futures.shared_future/default.pass.cpp | 33 +++ .../futures.shared_future/dtor.pass.cpp | 66 ++++++ .../futures.shared_future/get.pass.cpp | 143 +++++++++++++ .../move_assign.pass.cpp | 74 +++++++ .../futures.shared_future/move_ctor.pass.cpp | 66 ++++++ .../futures.shared_future/wait.pass.cpp | 86 ++++++++ .../futures.shared_future/wait_for.pass.cpp | 95 +++++++++ .../futures.shared_future/wait_until.pass.cpp | 95 +++++++++ 13 files changed, 1077 insertions(+), 2 deletions(-) create mode 100644 test/thread/futures/futures.shared_future/copy_assign.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/copy_ctor.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/ctor_future.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/default.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/dtor.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/get.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/move_assign.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/move_ctor.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/wait.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/wait_for.pass.cpp create mode 100644 test/thread/futures/futures.shared_future/wait_until.pass.cpp diff --git a/include/future b/include/future index b75014ac..4189d881 100644 --- a/include/future +++ b/include/future @@ -257,7 +257,7 @@ class shared_future public: shared_future(); shared_future(const shared_future& rhs); - shared_future(future&&); + shared_future(future&&); shared_future(shared_future&& rhs); ~shared_future(); shared_future& operator=(const shared_future& rhs); @@ -284,7 +284,7 @@ class shared_future public: shared_future(); shared_future(const shared_future& rhs); - shared_future(future&&); + shared_future(future&&); shared_future(shared_future&& rhs); ~shared_future(); shared_future& operator=(const shared_future& rhs); @@ -919,6 +919,8 @@ __deferred_assoc_state::__execute() } template class promise; +template class shared_future; +template class atomic_future; // future @@ -940,6 +942,8 @@ class future explicit future(__assoc_state<_R>* __state); template friend class promise; + template friend class shared_future; + template friend class atomic_future; template #ifdef _LIBCPP_MOVE @@ -1027,6 +1031,8 @@ class future<_R&> explicit future(__assoc_state<_R&>* __state); template friend class promise; + template friend class shared_future; + template friend class atomic_future; template #ifdef _LIBCPP_MOVE @@ -1109,6 +1115,8 @@ class future explicit future(__assoc_sub_state* __state); template friend class promise; + template friend class shared_future; + template friend class atomic_future; template #ifdef _LIBCPP_MOVE @@ -1156,6 +1164,14 @@ public: {return __state_->wait_until(__abs_time);} }; +template +inline _LIBCPP_INLINE_VISIBILITY +void +swap(future<_R>& __x, future<_R>& __y) +{ + __x.swap(__y); +} + // promise template class packaged_task; @@ -2050,6 +2066,184 @@ async(_F&& __f, _Args&&... __args) #endif // _LIBCPP_HAS_NO_VARIADICS +template +class shared_future +{ + __assoc_state<_R>* __state_; + +public: + shared_future() : __state_(nullptr) {} + shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) + {if (__state_) __state_->__add_shared();} +#ifdef _LIBCPP_MOVE + shared_future(future<_R>&& __f) : __state_(__f.__state_) + {__f.__state_ = nullptr;} + shared_future(shared_future&& __rhs) : __state_(__rhs.__state_) + {__rhs.__state_ = nullptr;} +#endif // _LIBCPP_MOVE + ~shared_future(); + shared_future& operator=(const shared_future& __rhs); +#ifdef _LIBCPP_MOVE + shared_future& operator=(shared_future&& __rhs) + { + shared_future(std::move(__rhs)).swap(*this); + return *this; + } +#endif // _LIBCPP_MOVE + + // retrieving the value + const _R& get() const {return __state_->copy();} + + void swap(shared_future& __rhs) {_STD::swap(__state_, __rhs.__state_);} + + // functions to check state + bool valid() const {return __state_ != nullptr;} + + void wait() const {__state_->wait();} + template + future_status + wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const + {return __state_->wait_for(__rel_time);} + template + future_status + wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const + {return __state_->wait_until(__abs_time);} +}; + +template +shared_future<_R>::~shared_future() +{ + if (__state_) + __state_->__release_shared(); +} + +template +shared_future<_R>& +shared_future<_R>::operator=(const shared_future& __rhs) +{ + if (__rhs.__state_) + __rhs.__state_->__add_shared(); + if (__state_) + __state_->__release_shared(); + __state_ = __rhs.__state_; + return *this; +} + +template +class shared_future<_R&> +{ + __assoc_state<_R&>* __state_; + +public: + shared_future() : __state_(nullptr) {} + shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) + {if (__state_) __state_->__add_shared();} +#ifdef _LIBCPP_MOVE + shared_future(future<_R&>&& __f) : __state_(__f.__state_) + {__f.__state_ = nullptr;} + shared_future(shared_future&& __rhs) : __state_(__rhs.__state_) + {__rhs.__state_ = nullptr;} +#endif // _LIBCPP_MOVE + ~shared_future(); + shared_future& operator=(const shared_future& __rhs); +#ifdef _LIBCPP_MOVE + shared_future& operator=(shared_future&& __rhs) + { + shared_future(std::move(__rhs)).swap(*this); + return *this; + } +#endif // _LIBCPP_MOVE + + // retrieving the value + _R& get() const {return __state_->copy();} + + void swap(shared_future& __rhs) {_STD::swap(__state_, __rhs.__state_);} + + // functions to check state + bool valid() const {return __state_ != nullptr;} + + void wait() const {__state_->wait();} + template + future_status + wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const + {return __state_->wait_for(__rel_time);} + template + future_status + wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const + {return __state_->wait_until(__abs_time);} +}; + +template +shared_future<_R&>::~shared_future() +{ + if (__state_) + __state_->__release_shared(); +} + +template +shared_future<_R&>& +shared_future<_R&>::operator=(const shared_future& __rhs) +{ + if (__rhs.__state_) + __rhs.__state_->__add_shared(); + if (__state_) + __state_->__release_shared(); + __state_ = __rhs.__state_; + return *this; +} + +template <> +class shared_future +{ + __assoc_sub_state* __state_; + +public: + shared_future() : __state_(nullptr) {} + shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) + {if (__state_) __state_->__add_shared();} +#ifdef _LIBCPP_MOVE + shared_future(future&& __f) : __state_(__f.__state_) + {__f.__state_ = nullptr;} + shared_future(shared_future&& __rhs) : __state_(__rhs.__state_) + {__rhs.__state_ = nullptr;} +#endif // _LIBCPP_MOVE + ~shared_future(); + shared_future& operator=(const shared_future& __rhs); +#ifdef _LIBCPP_MOVE + shared_future& operator=(shared_future&& __rhs) + { + shared_future(std::move(__rhs)).swap(*this); + return *this; + } +#endif // _LIBCPP_MOVE + + // retrieving the value + void get() const {__state_->copy();} + + void swap(shared_future& __rhs) {_STD::swap(__state_, __rhs.__state_);} + + // functions to check state + bool valid() const {return __state_ != nullptr;} + + void wait() const {__state_->wait();} + template + future_status + wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const + {return __state_->wait_for(__rel_time);} + template + future_status + wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const + {return __state_->wait_until(__abs_time);} +}; + +template +inline _LIBCPP_INLINE_VISIBILITY +void +swap(shared_future<_R>& __x, shared_future<_R>& __y) +{ + __x.swap(__y); +} + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_FUTURE diff --git a/src/future.cpp b/src/future.cpp index a0082767..924a6846 100644 --- a/src/future.cpp +++ b/src/future.cpp @@ -240,4 +240,21 @@ promise::set_exception_at_thread_exit(exception_ptr __p) __state_->set_exception_at_thread_exit(__p); } +shared_future::~shared_future() +{ + if (__state_) + __state_->__release_shared(); +} + +shared_future& +shared_future::operator=(const shared_future& __rhs) +{ + if (__rhs.__state_) + __rhs.__state_->__add_shared(); + if (__state_) + __state_->__release_shared(); + __state_ = __rhs.__state_; + return *this; +} + _LIBCPP_END_NAMESPACE_STD diff --git a/test/thread/futures/futures.shared_future/copy_assign.pass.cpp b/test/thread/futures/futures.shared_future/copy_assign.pass.cpp new file mode 100644 index 00000000..04dc1f86 --- /dev/null +++ b/test/thread/futures/futures.shared_future/copy_assign.pass.cpp @@ -0,0 +1,74 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// shared_future& operator=(const shared_future& rhs); + +#include +#include + +int main() +{ +#ifdef _LIBCPP_MOVE + { + typedef int T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f; + f = f0; + assert(f0.valid()); + assert(f.valid()); + } + { + typedef int T; + std::shared_future f0; + std::shared_future f; + f = f0; + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef int& T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f; + f = f0; + assert(f0.valid()); + assert(f.valid()); + } + { + typedef int& T; + std::shared_future f0; + std::shared_future f; + f = f0; + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef void T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f; + f = f0; + assert(f0.valid()); + assert(f.valid()); + } + { + typedef void T; + std::shared_future f0; + std::shared_future f; + f = f0; + assert(!f0.valid()); + assert(!f.valid()); + } +#endif // _LIBCPP_MOVE +} diff --git a/test/thread/futures/futures.shared_future/copy_ctor.pass.cpp b/test/thread/futures/futures.shared_future/copy_ctor.pass.cpp new file mode 100644 index 00000000..ec17da04 --- /dev/null +++ b/test/thread/futures/futures.shared_future/copy_ctor.pass.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// shared_future(const shared_future& rhs); + +#include +#include + +int main() +{ + { + typedef int T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f = f0; + assert(f0.valid()); + assert(f.valid()); + } + { + typedef int T; + std::shared_future f0; + std::shared_future f = f0; + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef int& T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f = f0; + assert(f0.valid()); + assert(f.valid()); + } + { + typedef int& T; + std::shared_future f0; + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef void T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f = f0; + assert(f0.valid()); + assert(f.valid()); + } + { + typedef void T; + std::shared_future f0; + std::shared_future f = f0; + assert(!f0.valid()); + assert(!f.valid()); + } +} diff --git a/test/thread/futures/futures.shared_future/ctor_future.pass.cpp b/test/thread/futures/futures.shared_future/ctor_future.pass.cpp new file mode 100644 index 00000000..2fb83ff7 --- /dev/null +++ b/test/thread/futures/futures.shared_future/ctor_future.pass.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// shared_future(future&& rhs); + +#include +#include + +int main() +{ + { + typedef int T; + std::promise p; + std::future f0 = p.get_future(); + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(f.valid()); + } + { + typedef int T; + std::future f0; + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef int& T; + std::promise p; + std::future f0 = p.get_future(); + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(f.valid()); + } + { + typedef int& T; + std::future f0; + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef void T; + std::promise p; + std::future f0 = p.get_future(); + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(f.valid()); + } + { + typedef void T; + std::future f0; + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } +} diff --git a/test/thread/futures/futures.shared_future/default.pass.cpp b/test/thread/futures/futures.shared_future/default.pass.cpp new file mode 100644 index 00000000..07ec85a0 --- /dev/null +++ b/test/thread/futures/futures.shared_future/default.pass.cpp @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// shared_future(); + +#include +#include + +int main() +{ + { + std::shared_future f; + assert(!f.valid()); + } + { + std::shared_future f; + assert(!f.valid()); + } + { + std::shared_future f; + assert(!f.valid()); + } +} diff --git a/test/thread/futures/futures.shared_future/dtor.pass.cpp b/test/thread/futures/futures.shared_future/dtor.pass.cpp new file mode 100644 index 00000000..d773a80a --- /dev/null +++ b/test/thread/futures/futures.shared_future/dtor.pass.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// ~shared_future(); + +#include +#include + +#include "../test_allocator.h" + +int main() +{ + assert(test_alloc_base::count == 0); + { + typedef int T; + std::shared_future f; + { + std::promise p(std::allocator_arg, test_allocator()); + assert(test_alloc_base::count == 1); + f = p.get_future(); + assert(test_alloc_base::count == 1); + assert(f.valid()); + } + assert(test_alloc_base::count == 1); + assert(f.valid()); + } + assert(test_alloc_base::count == 0); + { + typedef int& T; + std::shared_future f; + { + std::promise p(std::allocator_arg, test_allocator()); + assert(test_alloc_base::count == 1); + f = p.get_future(); + assert(test_alloc_base::count == 1); + assert(f.valid()); + } + assert(test_alloc_base::count == 1); + assert(f.valid()); + } + assert(test_alloc_base::count == 0); + { + typedef void T; + std::shared_future f; + { + std::promise p(std::allocator_arg, test_allocator()); + assert(test_alloc_base::count == 1); + f = p.get_future(); + assert(test_alloc_base::count == 1); + assert(f.valid()); + } + assert(test_alloc_base::count == 1); + assert(f.valid()); + } + assert(test_alloc_base::count == 0); +} diff --git a/test/thread/futures/futures.shared_future/get.pass.cpp b/test/thread/futures/futures.shared_future/get.pass.cpp new file mode 100644 index 00000000..d3445134 --- /dev/null +++ b/test/thread/futures/futures.shared_future/get.pass.cpp @@ -0,0 +1,143 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// const R& shared_future::get(); +// R& shared_future::get(); +// void shared_future::get(); + +#include +#include + +void func1(std::promise& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p.set_value(3); +} + +void func2(std::promise& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p.set_exception(std::make_exception_ptr(3)); +} + +int j = 0; + +void func3(std::promise& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + j = 5; + p.set_value(j); +} + +void func4(std::promise& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p.set_exception(std::make_exception_ptr(3.5)); +} + +void func5(std::promise& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p.set_value(); +} + +void func6(std::promise& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p.set_exception(std::make_exception_ptr('c')); +} + +int main() +{ + { + typedef int T; + { + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func1, std::move(p)).detach(); + assert(f.valid()); + assert(f.get() == 3); + assert(f.valid()); + } + { + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func2, std::move(p)).detach(); + try + { + assert(f.valid()); + assert(f.get() == 3); + assert(false); + } + catch (int i) + { + assert(i == 3); + } + assert(f.valid()); + } + } + { + typedef int& T; + { + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func3, std::move(p)).detach(); + assert(f.valid()); + assert(f.get() == 5); + assert(f.valid()); + } + { + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func4, std::move(p)).detach(); + try + { + assert(f.valid()); + assert(f.get() == 3); + assert(false); + } + catch (double i) + { + assert(i == 3.5); + } + assert(f.valid()); + } + } + { + typedef void T; + { + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func5, std::move(p)).detach(); + assert(f.valid()); + f.get(); + assert(f.valid()); + } + { + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func6, std::move(p)).detach(); + try + { + assert(f.valid()); + f.get(); + assert(false); + } + catch (char i) + { + assert(i == 'c'); + } + assert(f.valid()); + } + } +} diff --git a/test/thread/futures/futures.shared_future/move_assign.pass.cpp b/test/thread/futures/futures.shared_future/move_assign.pass.cpp new file mode 100644 index 00000000..496bac5a --- /dev/null +++ b/test/thread/futures/futures.shared_future/move_assign.pass.cpp @@ -0,0 +1,74 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// shared_future& operator=(shared_future&& rhs); + +#include +#include + +int main() +{ +#ifdef _LIBCPP_MOVE + { + typedef int T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f; + f = std::move(f0); + assert(!f0.valid()); + assert(f.valid()); + } + { + typedef int T; + std::shared_future f0; + std::shared_future f; + f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef int& T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f; + f = std::move(f0); + assert(!f0.valid()); + assert(f.valid()); + } + { + typedef int& T; + std::shared_future f0; + std::shared_future f; + f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef void T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f; + f = std::move(f0); + assert(!f0.valid()); + assert(f.valid()); + } + { + typedef void T; + std::shared_future f0; + std::shared_future f; + f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } +#endif // _LIBCPP_MOVE +} diff --git a/test/thread/futures/futures.shared_future/move_ctor.pass.cpp b/test/thread/futures/futures.shared_future/move_ctor.pass.cpp new file mode 100644 index 00000000..2130071c --- /dev/null +++ b/test/thread/futures/futures.shared_future/move_ctor.pass.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// shared_future(shared_future&& rhs); + +#include +#include + +int main() +{ + { + typedef int T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(f.valid()); + } + { + typedef int T; + std::shared_future f0; + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef int& T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(f.valid()); + } + { + typedef int& T; + std::shared_future f0; + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } + { + typedef void T; + std::promise p; + std::shared_future f0 = p.get_future(); + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(f.valid()); + } + { + typedef void T; + std::shared_future f0; + std::shared_future f = std::move(f0); + assert(!f0.valid()); + assert(!f.valid()); + } +} diff --git a/test/thread/futures/futures.shared_future/wait.pass.cpp b/test/thread/futures/futures.shared_future/wait.pass.cpp new file mode 100644 index 00000000..1bcafb14 --- /dev/null +++ b/test/thread/futures/futures.shared_future/wait.pass.cpp @@ -0,0 +1,86 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// void wait() const; + +#include +#include + +void func1(std::promise& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p.set_value(3); +} + +int j = 0; + +void func3(std::promise& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + j = 5; + p.set_value(j); +} + +void func5(std::promise& p) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + p.set_value(); +} + +int main() +{ + typedef std::chrono::high_resolution_clock Clock; + typedef std::chrono::duration ms; + { + typedef int T; + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func1, std::move(p)).detach(); + assert(f.valid()); + f.wait(); + assert(f.valid()); + Clock::time_point t0 = Clock::now(); + f.wait(); + Clock::time_point t1 = Clock::now(); + assert(f.valid()); + assert(t1-t0 < ms(5)); + } + { + typedef int& T; + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func3, std::move(p)).detach(); + assert(f.valid()); + f.wait(); + assert(f.valid()); + Clock::time_point t0 = Clock::now(); + f.wait(); + Clock::time_point t1 = Clock::now(); + assert(f.valid()); + assert(t1-t0 < ms(5)); + } + { + typedef void T; + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func5, std::move(p)).detach(); + assert(f.valid()); + f.wait(); + assert(f.valid()); + Clock::time_point t0 = Clock::now(); + f.wait(); + Clock::time_point t1 = Clock::now(); + assert(f.valid()); + assert(t1-t0 < ms(5)); + } +} diff --git a/test/thread/futures/futures.shared_future/wait_for.pass.cpp b/test/thread/futures/futures.shared_future/wait_for.pass.cpp new file mode 100644 index 00000000..bb3bb79f --- /dev/null +++ b/test/thread/futures/futures.shared_future/wait_for.pass.cpp @@ -0,0 +1,95 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// template +// future_status +// wait_for(const chrono::duration& rel_time) const; + +#include +#include + +typedef std::chrono::milliseconds ms; + +void func1(std::promise& p) +{ + std::this_thread::sleep_for(ms(500)); + p.set_value(3); +} + +int j = 0; + +void func3(std::promise& p) +{ + std::this_thread::sleep_for(ms(500)); + j = 5; + p.set_value(j); +} + +void func5(std::promise& p) +{ + std::this_thread::sleep_for(ms(500)); + p.set_value(); +} + +int main() +{ + typedef std::chrono::high_resolution_clock Clock; + { + typedef int T; + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func1, std::move(p)).detach(); + assert(f.valid()); + assert(f.wait_for(ms(300)) == std::future_status::timeout); + assert(f.valid()); + assert(f.wait_for(ms(300)) == std::future_status::ready); + assert(f.valid()); + Clock::time_point t0 = Clock::now(); + f.wait(); + Clock::time_point t1 = Clock::now(); + assert(f.valid()); + assert(t1-t0 < ms(5)); + } + { + typedef int& T; + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func3, std::move(p)).detach(); + assert(f.valid()); + assert(f.wait_for(ms(300)) == std::future_status::timeout); + assert(f.valid()); + assert(f.wait_for(ms(300)) == std::future_status::ready); + assert(f.valid()); + Clock::time_point t0 = Clock::now(); + f.wait(); + Clock::time_point t1 = Clock::now(); + assert(f.valid()); + assert(t1-t0 < ms(5)); + } + { + typedef void T; + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func5, std::move(p)).detach(); + assert(f.valid()); + assert(f.wait_for(ms(300)) == std::future_status::timeout); + assert(f.valid()); + assert(f.wait_for(ms(300)) == std::future_status::ready); + assert(f.valid()); + Clock::time_point t0 = Clock::now(); + f.wait(); + Clock::time_point t1 = Clock::now(); + assert(f.valid()); + assert(t1-t0 < ms(5)); + } +} diff --git a/test/thread/futures/futures.shared_future/wait_until.pass.cpp b/test/thread/futures/futures.shared_future/wait_until.pass.cpp new file mode 100644 index 00000000..aac0ff2d --- /dev/null +++ b/test/thread/futures/futures.shared_future/wait_until.pass.cpp @@ -0,0 +1,95 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_future + +// template +// future_status +// wait_until(const chrono::time_point& abs_time) const; + +#include +#include + +typedef std::chrono::milliseconds ms; + +void func1(std::promise& p) +{ + std::this_thread::sleep_for(ms(500)); + p.set_value(3); +} + +int j = 0; + +void func3(std::promise& p) +{ + std::this_thread::sleep_for(ms(500)); + j = 5; + p.set_value(j); +} + +void func5(std::promise& p) +{ + std::this_thread::sleep_for(ms(500)); + p.set_value(); +} + +int main() +{ + typedef std::chrono::high_resolution_clock Clock; + { + typedef int T; + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func1, std::move(p)).detach(); + assert(f.valid()); + assert(f.wait_until(Clock::now() + ms(300)) == std::future_status::timeout); + assert(f.valid()); + assert(f.wait_until(Clock::now() + ms(300)) == std::future_status::ready); + assert(f.valid()); + Clock::time_point t0 = Clock::now(); + f.wait(); + Clock::time_point t1 = Clock::now(); + assert(f.valid()); + assert(t1-t0 < ms(5)); + } + { + typedef int& T; + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func3, std::move(p)).detach(); + assert(f.valid()); + assert(f.wait_until(Clock::now() + ms(300)) == std::future_status::timeout); + assert(f.valid()); + assert(f.wait_until(Clock::now() + ms(300)) == std::future_status::ready); + assert(f.valid()); + Clock::time_point t0 = Clock::now(); + f.wait(); + Clock::time_point t1 = Clock::now(); + assert(f.valid()); + assert(t1-t0 < ms(5)); + } + { + typedef void T; + std::promise p; + std::shared_future f = p.get_future(); + std::thread(func5, std::move(p)).detach(); + assert(f.valid()); + assert(f.wait_until(Clock::now() + ms(300)) == std::future_status::timeout); + assert(f.valid()); + assert(f.wait_until(Clock::now() + ms(300)) == std::future_status::ready); + assert(f.valid()); + Clock::time_point t0 = Clock::now(); + f.wait(); + Clock::time_point t1 = Clock::now(); + assert(f.valid()); + assert(t1-t0 < ms(5)); + } +}