[cfe-commits] [libcxx] r113017 - in /libcxx/trunk: include/ src/ test/thread/futures/futures.atomic_future/ test/thread/thread.condition/ test/thread/thread.condition/thread.condition.condvarany/

Howard Hinnant hhinnant at apple.com
Fri Sep 3 14:46:37 PDT 2010


Author: hhinnant
Date: Fri Sep  3 16:46:37 2010
New Revision: 113017

URL: http://llvm.org/viewvc/llvm-project?rev=113017&view=rev
Log:
[futures.atomic_future] and notify_all_at_thread_exit.  This completes the header <future> and all of Chapter 30 (for C++0x enabled compilers).

Added:
    libcxx/trunk/test/thread/futures/futures.atomic_future/copy_assign.pass.cpp
    libcxx/trunk/test/thread/futures/futures.atomic_future/copy_ctor.pass.cpp
    libcxx/trunk/test/thread/futures/futures.atomic_future/ctor_future.pass.cpp
    libcxx/trunk/test/thread/futures/futures.atomic_future/default.pass.cpp
    libcxx/trunk/test/thread/futures/futures.atomic_future/dtor.pass.cpp
    libcxx/trunk/test/thread/futures/futures.atomic_future/get.pass.cpp
    libcxx/trunk/test/thread/futures/futures.atomic_future/wait.pass.cpp
    libcxx/trunk/test/thread/futures/futures.atomic_future/wait_for.pass.cpp
    libcxx/trunk/test/thread/futures/futures.atomic_future/wait_until.pass.cpp
Modified:
    libcxx/trunk/include/condition_variable
    libcxx/trunk/include/future
    libcxx/trunk/include/thread
    libcxx/trunk/src/condition_variable.cpp
    libcxx/trunk/src/future.cpp
    libcxx/trunk/src/thread.cpp
    libcxx/trunk/test/thread/thread.condition/notify_all_at_thread_exit.pass.cpp
    libcxx/trunk/test/thread/thread.condition/thread.condition.condvarany/wait_for_pred.pass.cpp

Modified: libcxx/trunk/include/condition_variable
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/condition_variable?rev=113017&r1=113016&r2=113017&view=diff
==============================================================================
--- libcxx/trunk/include/condition_variable (original)
+++ libcxx/trunk/include/condition_variable Fri Sep  3 16:46:37 2010
@@ -246,6 +246,8 @@
                       _STD::move(__pred));
 }
 
+void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_CONDITION_VARIABLE

Modified: libcxx/trunk/include/future
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/future?rev=113017&r1=113016&r2=113017&view=diff
==============================================================================
--- libcxx/trunk/include/future (original)
+++ libcxx/trunk/include/future Fri Sep  3 16:46:37 2010
@@ -439,7 +439,7 @@
 #include <memory>
 #include <chrono>
 #include <exception>
-#include <__mutex_base>
+#include <mutex>
 #include <thread>
 
 #pragma GCC system_header
@@ -2066,6 +2066,8 @@
 
 #endif  // _LIBCPP_HAS_NO_VARIADICS
 
+// shared_future
+
 template <class _R>
 class shared_future
 {
@@ -2244,6 +2246,200 @@
     __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

Modified: libcxx/trunk/include/thread
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/thread?rev=113017&r1=113016&r2=113017&view=diff
==============================================================================
--- libcxx/trunk/include/thread (original)
+++ libcxx/trunk/include/thread Fri Sep  3 16:46:37 2010
@@ -295,6 +295,7 @@
     __thread_struct();
     ~__thread_struct();
 
+    void notify_all_at_thread_exit(condition_variable*, mutex*);
     void __make_ready_at_thread_exit(__assoc_sub_state*);
 };
 

Modified: libcxx/trunk/src/condition_variable.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/condition_variable.cpp?rev=113017&r1=113016&r2=113017&view=diff
==============================================================================
--- libcxx/trunk/src/condition_variable.cpp (original)
+++ libcxx/trunk/src/condition_variable.cpp Fri Sep  3 16:46:37 2010
@@ -61,4 +61,10 @@
         __throw_system_error(ec, "condition_variable timed_wait failed");
 }
 
+void
+notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
+{
+    __thread_local_data->notify_all_at_thread_exit(&cond, lk.release());
+}
+
 _LIBCPP_END_NAMESPACE_STD

Modified: libcxx/trunk/src/future.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/future.cpp?rev=113017&r1=113016&r2=113017&view=diff
==============================================================================
--- libcxx/trunk/src/future.cpp (original)
+++ libcxx/trunk/src/future.cpp Fri Sep  3 16:46:37 2010
@@ -257,4 +257,39 @@
     return *this;
 }
 
+atomic_future<void>::~atomic_future()
+{
+    if (__state_)
+        __state_->__release_shared();
+}
+
+atomic_future<void>&
+atomic_future<void>::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;
+}
+
+void
+atomic_future<void>::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_);
+    }
+}
+
 _LIBCPP_END_NAMESPACE_STD

Modified: libcxx/trunk/src/thread.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/thread.cpp?rev=113017&r1=113016&r2=113017&view=diff
==============================================================================
--- libcxx/trunk/src/thread.cpp (original)
+++ libcxx/trunk/src/thread.cpp Fri Sep  3 16:46:37 2010
@@ -90,7 +90,10 @@
 class __thread_struct_imp
 {
     typedef vector<__assoc_sub_state*> _AsyncStates;
+    typedef vector<pair<condition_variable*, mutex*> > _Notify;
+
     _AsyncStates async_states_;
+    _Notify notify_;
 
     __thread_struct_imp(const __thread_struct_imp&);
     __thread_struct_imp& operator=(const __thread_struct_imp&);
@@ -98,11 +101,18 @@
     __thread_struct_imp() {}
     ~__thread_struct_imp();
 
+    void notify_all_at_thread_exit(condition_variable* cv, mutex* m);
     void __make_ready_at_thread_exit(__assoc_sub_state* __s);
 };
 
 __thread_struct_imp::~__thread_struct_imp()
 {
+    for (_Notify::iterator i = notify_.begin(), e = notify_.end();
+            i != e; ++i)
+    {
+        i->second->unlock();
+        i->first->notify_all();
+    }
     for (_AsyncStates::iterator i = async_states_.begin(), e = async_states_.end();
             i != e; ++i)
     {
@@ -112,6 +122,12 @@
 }
 
 void
+__thread_struct_imp::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
+{
+    notify_.push_back(pair<condition_variable*, mutex*>(cv, m));
+}
+
+void
 __thread_struct_imp::__make_ready_at_thread_exit(__assoc_sub_state* __s)
 {
     async_states_.push_back(__s);
@@ -131,6 +147,12 @@
 }
 
 void
+__thread_struct::notify_all_at_thread_exit(condition_variable* cv, mutex* m)
+{
+    __p_->notify_all_at_thread_exit(cv, m);
+}
+
+void
 __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
 {
     __p_->__make_ready_at_thread_exit(__s);

Added: libcxx/trunk/test/thread/futures/futures.atomic_future/copy_assign.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/futures/futures.atomic_future/copy_assign.pass.cpp?rev=113017&view=auto
==============================================================================
--- libcxx/trunk/test/thread/futures/futures.atomic_future/copy_assign.pass.cpp (added)
+++ libcxx/trunk/test/thread/futures/futures.atomic_future/copy_assign.pass.cpp Fri Sep  3 16:46:37 2010
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <future>
+
+// class atomic_future<R>
+
+// atomic_future& operator=(const atomic_future& rhs);
+
+#include <future>
+#include <cassert>
+
+int main()
+{
+#ifdef _LIBCPP_MOVE
+    {
+        typedef int T;
+        std::promise<T> p;
+        std::atomic_future<T> f0 = p.get_future();
+        std::atomic_future<T> f;
+        f = f0;
+        assert(f0.valid());
+        assert(f.valid());
+    }
+    {
+        typedef int T;
+        std::atomic_future<T> f0;
+        std::atomic_future<T> f;
+        f = f0;
+        assert(!f0.valid());
+        assert(!f.valid());
+    }
+    {
+        typedef int& T;
+        std::promise<T> p;
+        std::atomic_future<T> f0 = p.get_future();
+        std::atomic_future<T> f;
+        f = f0;
+        assert(f0.valid());
+        assert(f.valid());
+    }
+    {
+        typedef int& T;
+        std::atomic_future<T> f0;
+        std::atomic_future<T> f;
+        f = f0;
+        assert(!f0.valid());
+        assert(!f.valid());
+    }
+    {
+        typedef void T;
+        std::promise<T> p;
+        std::atomic_future<T> f0 = p.get_future();
+        std::atomic_future<T> f;
+        f = f0;
+        assert(f0.valid());
+        assert(f.valid());
+    }
+    {
+        typedef void T;
+        std::atomic_future<T> f0;
+        std::atomic_future<T> f;
+        f = f0;
+        assert(!f0.valid());
+        assert(!f.valid());
+    }
+#endif  // _LIBCPP_MOVE
+}

Added: libcxx/trunk/test/thread/futures/futures.atomic_future/copy_ctor.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/futures/futures.atomic_future/copy_ctor.pass.cpp?rev=113017&view=auto
==============================================================================
--- libcxx/trunk/test/thread/futures/futures.atomic_future/copy_ctor.pass.cpp (added)
+++ libcxx/trunk/test/thread/futures/futures.atomic_future/copy_ctor.pass.cpp Fri Sep  3 16:46:37 2010
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <future>
+
+// class atomic_future<R>
+
+// atomic_future(const atomic_future& rhs);
+
+#include <future>
+#include <cassert>
+
+int main()
+{
+    {
+        typedef int T;
+        std::promise<T> p;
+        std::atomic_future<T> f0 = p.get_future();
+        std::atomic_future<T> f = f0;
+        assert(f0.valid());
+        assert(f.valid());
+    }
+    {
+        typedef int T;
+        std::atomic_future<T> f0;
+        std::atomic_future<T> f = f0;
+        assert(!f0.valid());
+        assert(!f.valid());
+    }
+    {
+        typedef int& T;
+        std::promise<T> p;
+        std::atomic_future<T> f0 = p.get_future();
+        std::atomic_future<T> f = f0;
+        assert(f0.valid());
+        assert(f.valid());
+    }
+    {
+        typedef int& T;
+        std::atomic_future<T> f0;
+        std::atomic_future<T> f = std::move(f0);
+        assert(!f0.valid());
+        assert(!f.valid());
+    }
+    {
+        typedef void T;
+        std::promise<T> p;
+        std::atomic_future<T> f0 = p.get_future();
+        std::atomic_future<T> f = f0;
+        assert(f0.valid());
+        assert(f.valid());
+    }
+    {
+        typedef void T;
+        std::atomic_future<T> f0;
+        std::atomic_future<T> f = f0;
+        assert(!f0.valid());
+        assert(!f.valid());
+    }
+}

Added: libcxx/trunk/test/thread/futures/futures.atomic_future/ctor_future.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/futures/futures.atomic_future/ctor_future.pass.cpp?rev=113017&view=auto
==============================================================================
--- libcxx/trunk/test/thread/futures/futures.atomic_future/ctor_future.pass.cpp (added)
+++ libcxx/trunk/test/thread/futures/futures.atomic_future/ctor_future.pass.cpp Fri Sep  3 16:46:37 2010
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <future>
+
+// class atomic_future<R>
+
+// atomic_future(future<R>&& rhs);
+
+#include <future>
+#include <cassert>
+
+int main()
+{
+    {
+        typedef int T;
+        std::promise<T> p;
+        std::future<T> f0 = p.get_future();
+        std::atomic_future<T> f = std::move(f0);
+        assert(!f0.valid());
+        assert(f.valid());
+    }
+    {
+        typedef int T;
+        std::future<T> f0;
+        std::atomic_future<T> f = std::move(f0);
+        assert(!f0.valid());
+        assert(!f.valid());
+    }
+    {
+        typedef int& T;
+        std::promise<T> p;
+        std::future<T> f0 = p.get_future();
+        std::atomic_future<T> f = std::move(f0);
+        assert(!f0.valid());
+        assert(f.valid());
+    }
+    {
+        typedef int& T;
+        std::future<T> f0;
+        std::atomic_future<T> f = std::move(f0);
+        assert(!f0.valid());
+        assert(!f.valid());
+    }
+    {
+        typedef void T;
+        std::promise<T> p;
+        std::future<T> f0 = p.get_future();
+        std::atomic_future<T> f = std::move(f0);
+        assert(!f0.valid());
+        assert(f.valid());
+    }
+    {
+        typedef void T;
+        std::future<T> f0;
+        std::atomic_future<T> f = std::move(f0);
+        assert(!f0.valid());
+        assert(!f.valid());
+    }
+}

Added: libcxx/trunk/test/thread/futures/futures.atomic_future/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/futures/futures.atomic_future/default.pass.cpp?rev=113017&view=auto
==============================================================================
--- libcxx/trunk/test/thread/futures/futures.atomic_future/default.pass.cpp (added)
+++ libcxx/trunk/test/thread/futures/futures.atomic_future/default.pass.cpp Fri Sep  3 16:46:37 2010
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <future>
+
+// class atomic_future<R>
+
+// atomic_future();
+
+#include <future>
+#include <cassert>
+
+int main()
+{
+    {
+        std::atomic_future<int> f;
+        assert(!f.valid());
+    }
+    {
+        std::atomic_future<int&> f;
+        assert(!f.valid());
+    }
+    {
+        std::atomic_future<void> f;
+        assert(!f.valid());
+    }
+}

Added: libcxx/trunk/test/thread/futures/futures.atomic_future/dtor.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/futures/futures.atomic_future/dtor.pass.cpp?rev=113017&view=auto
==============================================================================
--- libcxx/trunk/test/thread/futures/futures.atomic_future/dtor.pass.cpp (added)
+++ libcxx/trunk/test/thread/futures/futures.atomic_future/dtor.pass.cpp Fri Sep  3 16:46:37 2010
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <future>
+
+// class atomic_future<R>
+
+// ~atomic_future();
+
+#include <future>
+#include <cassert>
+
+#include "../test_allocator.h"
+
+int main()
+{
+    assert(test_alloc_base::count == 0);
+    {
+        typedef int T;
+        std::atomic_future<T> f;
+        {
+            std::promise<T> p(std::allocator_arg, test_allocator<T>());
+            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::atomic_future<T> f;
+        {
+            std::promise<T> p(std::allocator_arg, test_allocator<int>());
+            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::atomic_future<T> f;
+        {
+            std::promise<T> p(std::allocator_arg, test_allocator<T>());
+            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);
+}

Added: libcxx/trunk/test/thread/futures/futures.atomic_future/get.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/futures/futures.atomic_future/get.pass.cpp?rev=113017&view=auto
==============================================================================
--- libcxx/trunk/test/thread/futures/futures.atomic_future/get.pass.cpp (added)
+++ libcxx/trunk/test/thread/futures/futures.atomic_future/get.pass.cpp Fri Sep  3 16:46:37 2010
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <future>
+
+// class atomic_future<R>
+
+// const R& atomic_future::get();
+// R& atomic_future<R&>::get();
+// void atomic_future<void>::get();
+
+#include <future>
+#include <cassert>
+
+void func1(std::promise<int>& p)
+{
+    std::this_thread::sleep_for(std::chrono::milliseconds(500));
+    p.set_value(3);
+}
+
+void func2(std::promise<int>& 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<int&>& p)
+{
+    std::this_thread::sleep_for(std::chrono::milliseconds(500));
+    j = 5;
+    p.set_value(j);
+}
+
+void func4(std::promise<int&>& p)
+{
+    std::this_thread::sleep_for(std::chrono::milliseconds(500));
+    p.set_exception(std::make_exception_ptr(3.5));
+}
+
+void func5(std::promise<void>& p)
+{
+    std::this_thread::sleep_for(std::chrono::milliseconds(500));
+    p.set_value();
+}
+
+void func6(std::promise<void>& 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<T> p;
+            std::atomic_future<T> f = p.get_future();
+            std::thread(func1, std::move(p)).detach();
+            assert(f.valid());
+            assert(f.get() == 3);
+            assert(f.valid());
+        }
+        {
+            std::promise<T> p;
+            std::atomic_future<T> 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<T> p;
+            std::atomic_future<T> f = p.get_future();
+            std::thread(func3, std::move(p)).detach();
+            assert(f.valid());
+            assert(f.get() == 5);
+            assert(f.valid());
+        }
+        {
+            std::promise<T> p;
+            std::atomic_future<T> 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<T> p;
+            std::atomic_future<T> f = p.get_future();
+            std::thread(func5, std::move(p)).detach();
+            assert(f.valid());
+            f.get();
+            assert(f.valid());
+        }
+        {
+            std::promise<T> p;
+            std::atomic_future<T> 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());
+        }
+    }
+}

Added: libcxx/trunk/test/thread/futures/futures.atomic_future/wait.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/futures/futures.atomic_future/wait.pass.cpp?rev=113017&view=auto
==============================================================================
--- libcxx/trunk/test/thread/futures/futures.atomic_future/wait.pass.cpp (added)
+++ libcxx/trunk/test/thread/futures/futures.atomic_future/wait.pass.cpp Fri Sep  3 16:46:37 2010
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <future>
+
+// class atomic_future<R>
+
+// void wait() const;
+
+#include <future>
+#include <cassert>
+
+void func1(std::promise<int>& p)
+{
+    std::this_thread::sleep_for(std::chrono::milliseconds(500));
+    p.set_value(3);
+}
+
+int j = 0;
+
+void func3(std::promise<int&>& p)
+{
+    std::this_thread::sleep_for(std::chrono::milliseconds(500));
+    j = 5;
+    p.set_value(j);
+}
+
+void func5(std::promise<void>& 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<double, std::milli> ms;
+    {
+        typedef int T;
+        std::promise<T> p;
+        std::atomic_future<T> 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<T> p;
+        std::atomic_future<T> 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<T> p;
+        std::atomic_future<T> 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));
+    }
+}

Added: libcxx/trunk/test/thread/futures/futures.atomic_future/wait_for.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/futures/futures.atomic_future/wait_for.pass.cpp?rev=113017&view=auto
==============================================================================
--- libcxx/trunk/test/thread/futures/futures.atomic_future/wait_for.pass.cpp (added)
+++ libcxx/trunk/test/thread/futures/futures.atomic_future/wait_for.pass.cpp Fri Sep  3 16:46:37 2010
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <future>
+
+// class atomic_future<R>
+
+// template <class Rep, class Period>
+//   future_status
+//   wait_for(const chrono::duration<Rep, Period>& rel_time) const;
+
+#include <future>
+#include <cassert>
+
+typedef std::chrono::milliseconds ms;
+
+void func1(std::promise<int>& p)
+{
+    std::this_thread::sleep_for(ms(500));
+    p.set_value(3);
+}
+
+int j = 0;
+
+void func3(std::promise<int&>& p)
+{
+    std::this_thread::sleep_for(ms(500));
+    j = 5;
+    p.set_value(j);
+}
+
+void func5(std::promise<void>& 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<T> p;
+        std::atomic_future<T> 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<T> p;
+        std::atomic_future<T> 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<T> p;
+        std::atomic_future<T> 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));
+    }
+}

Added: libcxx/trunk/test/thread/futures/futures.atomic_future/wait_until.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/futures/futures.atomic_future/wait_until.pass.cpp?rev=113017&view=auto
==============================================================================
--- libcxx/trunk/test/thread/futures/futures.atomic_future/wait_until.pass.cpp (added)
+++ libcxx/trunk/test/thread/futures/futures.atomic_future/wait_until.pass.cpp Fri Sep  3 16:46:37 2010
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <future>
+
+// class atomic_future<R>
+
+// template <class Clock, class Duration>
+//   future_status
+//   wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
+
+#include <future>
+#include <cassert>
+
+typedef std::chrono::milliseconds ms;
+
+void func1(std::promise<int>& p)
+{
+    std::this_thread::sleep_for(ms(500));
+    p.set_value(3);
+}
+
+int j = 0;
+
+void func3(std::promise<int&>& p)
+{
+    std::this_thread::sleep_for(ms(500));
+    j = 5;
+    p.set_value(j);
+}
+
+void func5(std::promise<void>& 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<T> p;
+        std::atomic_future<T> 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<T> p;
+        std::atomic_future<T> 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<T> p;
+        std::atomic_future<T> 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));
+    }
+}

Modified: libcxx/trunk/test/thread/thread.condition/notify_all_at_thread_exit.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/thread.condition/notify_all_at_thread_exit.pass.cpp?rev=113017&r1=113016&r2=113017&view=diff
==============================================================================
--- libcxx/trunk/test/thread/thread.condition/notify_all_at_thread_exit.pass.cpp (original)
+++ libcxx/trunk/test/thread/thread.condition/notify_all_at_thread_exit.pass.cpp Fri Sep  3 16:46:37 2010
@@ -13,9 +13,30 @@
 //   notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
 
 #include <condition_variable>
+#include <mutex>
+#include <thread>
+#include <chrono>
 #include <cassert>
 
+std::condition_variable cv;
+std::mutex mut;
+
+typedef std::chrono::milliseconds ms;
+typedef std::chrono::high_resolution_clock Clock;
+
+void func()
+{
+    std::unique_lock<std::mutex> lk(mut);
+    std::notify_all_at_thread_exit(cv, std::move(lk));
+    std::this_thread::sleep_for(ms(300));
+}
+
 int main()
 {
-#error notify_all_at_thread_exit not implemented
+    std::unique_lock<std::mutex> lk(mut);
+    std::thread(func).detach();
+    Clock::time_point t0 = Clock::now();
+    cv.wait(lk);
+    Clock::time_point t1 = Clock::now();
+    assert(t1-t0 > ms(250));
 }

Modified: libcxx/trunk/test/thread/thread.condition/thread.condition.condvarany/wait_for_pred.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/thread/thread.condition/thread.condition.condvarany/wait_for_pred.pass.cpp?rev=113017&r1=113016&r2=113017&view=diff
==============================================================================
--- libcxx/trunk/test/thread/thread.condition/thread.condition.condvarany/wait_for_pred.pass.cpp (original)
+++ libcxx/trunk/test/thread/thread.condition/thread.condition.condvarany/wait_for_pred.pass.cpp Fri Sep  3 16:46:37 2010
@@ -61,7 +61,7 @@
     }
     else
     {
-        assert(t1 - t0 - milliseconds(250) < milliseconds(2));
+        assert(t1 - t0 - milliseconds(250) < milliseconds(5));
         assert(test2 == 0);
     }
     ++runs;





More information about the cfe-commits mailing list