15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier //
9*a7f9895cSLouis Dionne // UNSUPPORTED: no-threads
1031cbe0f2SLouis Dionne // UNSUPPORTED: c++03
115a83710eSEric Fiselier 
125a83710eSEric Fiselier // <future>
135a83710eSEric Fiselier 
145a83710eSEric Fiselier // class shared_future<R>
155a83710eSEric Fiselier 
165a83710eSEric Fiselier // void wait() const;
175a83710eSEric Fiselier 
185a83710eSEric Fiselier #include <cassert>
19489637e6SNikolas Klauser #include <chrono>
20489637e6SNikolas Klauser #include <future>
215a83710eSEric Fiselier 
2256462801SLouis Dionne #include "make_test_thread.h"
237fc6a556SMarshall Clow #include "test_macros.h"
247fc6a556SMarshall Clow 
func1(std::promise<int> p)255a83710eSEric Fiselier void func1(std::promise<int> p)
265a83710eSEric Fiselier {
275a83710eSEric Fiselier     std::this_thread::sleep_for(std::chrono::milliseconds(500));
285a83710eSEric Fiselier     p.set_value(3);
295a83710eSEric Fiselier }
305a83710eSEric Fiselier 
315a83710eSEric Fiselier int j = 0;
325a83710eSEric Fiselier 
func3(std::promise<int &> p)335a83710eSEric Fiselier void func3(std::promise<int&> p)
345a83710eSEric Fiselier {
355a83710eSEric Fiselier     std::this_thread::sleep_for(std::chrono::milliseconds(500));
365a83710eSEric Fiselier     j = 5;
375a83710eSEric Fiselier     p.set_value(j);
385a83710eSEric Fiselier }
395a83710eSEric Fiselier 
func5(std::promise<void> p)405a83710eSEric Fiselier void func5(std::promise<void> p)
415a83710eSEric Fiselier {
425a83710eSEric Fiselier     std::this_thread::sleep_for(std::chrono::milliseconds(500));
435a83710eSEric Fiselier     p.set_value();
445a83710eSEric Fiselier }
455a83710eSEric Fiselier 
main(int,char **)462df59c50SJF Bastien int main(int, char**)
475a83710eSEric Fiselier {
485a83710eSEric Fiselier     typedef std::chrono::high_resolution_clock Clock;
495a83710eSEric Fiselier     typedef std::chrono::duration<double, std::milli> ms;
505a83710eSEric Fiselier     {
515a83710eSEric Fiselier         typedef int T;
525a83710eSEric Fiselier         std::promise<T> p;
535a83710eSEric Fiselier         std::shared_future<T> f = p.get_future();
5456462801SLouis Dionne         support::make_test_thread(func1, std::move(p)).detach();
555a83710eSEric Fiselier         assert(f.valid());
565a83710eSEric Fiselier         f.wait();
575a83710eSEric Fiselier         assert(f.valid());
585a83710eSEric Fiselier         Clock::time_point t0 = Clock::now();
595a83710eSEric Fiselier         f.wait();
605a83710eSEric Fiselier         Clock::time_point t1 = Clock::now();
615a83710eSEric Fiselier         assert(f.valid());
625a83710eSEric Fiselier         assert(t1-t0 < ms(5));
635a83710eSEric Fiselier     }
645a83710eSEric Fiselier     {
655a83710eSEric Fiselier         typedef int& T;
665a83710eSEric Fiselier         std::promise<T> p;
675a83710eSEric Fiselier         std::shared_future<T> f = p.get_future();
6856462801SLouis Dionne         support::make_test_thread(func3, std::move(p)).detach();
695a83710eSEric Fiselier         assert(f.valid());
705a83710eSEric Fiselier         f.wait();
715a83710eSEric Fiselier         assert(f.valid());
725a83710eSEric Fiselier         Clock::time_point t0 = Clock::now();
735a83710eSEric Fiselier         f.wait();
745a83710eSEric Fiselier         Clock::time_point t1 = Clock::now();
755a83710eSEric Fiselier         assert(f.valid());
765a83710eSEric Fiselier         assert(t1-t0 < ms(5));
775a83710eSEric Fiselier     }
785a83710eSEric Fiselier     {
795a83710eSEric Fiselier         typedef void T;
805a83710eSEric Fiselier         std::promise<T> p;
815a83710eSEric Fiselier         std::shared_future<T> f = p.get_future();
8256462801SLouis Dionne         support::make_test_thread(func5, std::move(p)).detach();
835a83710eSEric Fiselier         assert(f.valid());
845a83710eSEric Fiselier         f.wait();
855a83710eSEric Fiselier         assert(f.valid());
865a83710eSEric Fiselier         Clock::time_point t0 = Clock::now();
875a83710eSEric Fiselier         f.wait();
885a83710eSEric Fiselier         Clock::time_point t1 = Clock::now();
895a83710eSEric Fiselier         assert(f.valid());
905a83710eSEric Fiselier         assert(t1-t0 < ms(5));
915a83710eSEric Fiselier     }
922df59c50SJF Bastien 
932df59c50SJF Bastien   return 0;
945a83710eSEric Fiselier }
95