blob: d5865b9b9dcf34f9139387f714b123addb33ac27 [file] [log] [blame]
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // UNSUPPORTED: libcpp-has-no-threads
Howard Hinnant7158e5c2010-08-29 14:20:30 +000011
Dan Albert1d4a1ed2016-05-25 22:36:09 -070012 // <future>
Howard Hinnant7158e5c2010-08-29 14:20:30 +000013
Dan Albert1d4a1ed2016-05-25 22:36:09 -070014 // class future<R>
Howard Hinnant7158e5c2010-08-29 14:20:30 +000015
Dan Albert1d4a1ed2016-05-25 22:36:09 -070016 // template <class Clock, class Duration>
17 // future_status
18 // wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
Howard Hinnant7158e5c2010-08-29 14:20:30 +000019
Dan Albert1d4a1ed2016-05-25 22:36:09 -070020 #include <future>
21 #include <atomic>
22 #include <cassert>
Howard Hinnant7158e5c2010-08-29 14:20:30 +000023
Dan Albert1d4a1ed2016-05-25 22:36:09 -070024 enum class WorkerThreadState { Uninitialized, AllowedToRun, Exiting };
25 typedef std::chrono::milliseconds ms;
Howard Hinnant7158e5c2010-08-29 14:20:30 +000026
Dan Albert1d4a1ed2016-05-25 22:36:09 -070027 std::atomic<WorkerThreadState> thread_state(WorkerThreadState::Uninitialized);
Howard Hinnant7158e5c2010-08-29 14:20:30 +000028
Dan Albert1d4a1ed2016-05-25 22:36:09 -070029 void set_worker_thread_state(WorkerThreadState state)
30 {
31 thread_state.store(state, std::memory_order_relaxed);
32 }
Howard Hinnant7158e5c2010-08-29 14:20:30 +000033
Dan Albert1d4a1ed2016-05-25 22:36:09 -070034 void wait_for_worker_thread_state(WorkerThreadState state)
35 {
36 while (thread_state.load(std::memory_order_relaxed) != state);
37 }
Howard Hinnant7158e5c2010-08-29 14:20:30 +000038
Dan Albert1d4a1ed2016-05-25 22:36:09 -070039 void func1(std::promise<int> p)
40 {
41 wait_for_worker_thread_state(WorkerThreadState::AllowedToRun);
42 p.set_value(3);
43 set_worker_thread_state(WorkerThreadState::Exiting);
44 }
Howard Hinnant7158e5c2010-08-29 14:20:30 +000045
Dan Albert1d4a1ed2016-05-25 22:36:09 -070046 int j = 0;
Eric Fiselier578c9e82015-02-11 01:25:57 +000047
Dan Albert1d4a1ed2016-05-25 22:36:09 -070048 void func3(std::promise<int&> p)
49 {
50 wait_for_worker_thread_state(WorkerThreadState::AllowedToRun);
51 j = 5;
52 p.set_value(j);
53 set_worker_thread_state(WorkerThreadState::Exiting);
54 }
Eric Fiselier578c9e82015-02-11 01:25:57 +000055
Dan Albert1d4a1ed2016-05-25 22:36:09 -070056 void func5(std::promise<void> p)
57 {
58 wait_for_worker_thread_state(WorkerThreadState::AllowedToRun);
59 p.set_value();
60 set_worker_thread_state(WorkerThreadState::Exiting);
61 }
Eric Fiselier578c9e82015-02-11 01:25:57 +000062
Dan Albert1d4a1ed2016-05-25 22:36:09 -070063 int main()
64 {
65 typedef std::chrono::high_resolution_clock Clock;
66 {
67 typedef int T;
68 std::promise<T> p;
69 std::future<T> f = p.get_future();
70 std::thread(func1, std::move(p)).detach();
71 assert(f.valid());
72 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout);
73 assert(f.valid());
Eric Fiselier578c9e82015-02-11 01:25:57 +000074
Dan Albert1d4a1ed2016-05-25 22:36:09 -070075 // allow the worker thread to produce the result and wait until the worker is done
76 set_worker_thread_state(WorkerThreadState::AllowedToRun);
77 wait_for_worker_thread_state(WorkerThreadState::Exiting);
Eric Fiselier578c9e82015-02-11 01:25:57 +000078
Dan Albert1d4a1ed2016-05-25 22:36:09 -070079 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready);
80 assert(f.valid());
81 Clock::time_point t0 = Clock::now();
82 f.wait();
83 Clock::time_point t1 = Clock::now();
84 assert(f.valid());
85 assert(t1-t0 < ms(5));
86 }
87 {
88 typedef int& T;
89 std::promise<T> p;
90 std::future<T> f = p.get_future();
91 std::thread(func3, std::move(p)).detach();
92 assert(f.valid());
93 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout);
94 assert(f.valid());
Eric Fiselier578c9e82015-02-11 01:25:57 +000095
Dan Albert1d4a1ed2016-05-25 22:36:09 -070096 // allow the worker thread to produce the result and wait until the worker is done
97 set_worker_thread_state(WorkerThreadState::AllowedToRun);
98 wait_for_worker_thread_state(WorkerThreadState::Exiting);
Eric Fiselier578c9e82015-02-11 01:25:57 +000099
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700100 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready);
101 assert(f.valid());
102 Clock::time_point t0 = Clock::now();
103 f.wait();
104 Clock::time_point t1 = Clock::now();
105 assert(f.valid());
106 assert(t1-t0 < ms(5));
107 }
108 {
109 typedef void T;
110 std::promise<T> p;
111 std::future<T> f = p.get_future();
112 std::thread(func5, std::move(p)).detach();
113 assert(f.valid());
114 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout);
115 assert(f.valid());
Eric Fiselier578c9e82015-02-11 01:25:57 +0000116
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700117 // allow the worker thread to produce the result and wait until the worker is done
118 set_worker_thread_state(WorkerThreadState::AllowedToRun);
119 wait_for_worker_thread_state(WorkerThreadState::Exiting);
Eric Fiselier578c9e82015-02-11 01:25:57 +0000120
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700121 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready);
122 assert(f.valid());
123 Clock::time_point t0 = Clock::now();
124 f.wait();
125 Clock::time_point t1 = Clock::now();
126 assert(f.valid());
127 assert(t1-t0 < ms(5));
128 }
129 }