1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
16 // template <class Clock, class Duration>
18 // wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
25 #include "make_test_thread.h"
26 #include "test_macros.h"
28 enum class WorkerThreadState
{ Uninitialized
, AllowedToRun
, Exiting
};
29 typedef std::chrono::milliseconds ms
;
31 std::atomic
<WorkerThreadState
> thread_state(WorkerThreadState::Uninitialized
);
33 void set_worker_thread_state(WorkerThreadState state
)
35 thread_state
.store(state
, std::memory_order_relaxed
);
38 void wait_for_worker_thread_state(WorkerThreadState state
)
40 while (thread_state
.load(std::memory_order_relaxed
) != state
)
41 std::this_thread::yield();
44 void func1(std::promise
<int> p
)
46 wait_for_worker_thread_state(WorkerThreadState::AllowedToRun
);
48 set_worker_thread_state(WorkerThreadState::Exiting
);
53 void func3(std::promise
<int&> p
)
55 wait_for_worker_thread_state(WorkerThreadState::AllowedToRun
);
58 set_worker_thread_state(WorkerThreadState::Exiting
);
61 void func5(std::promise
<void> p
)
63 wait_for_worker_thread_state(WorkerThreadState::AllowedToRun
);
65 set_worker_thread_state(WorkerThreadState::Exiting
);
70 typedef std::chrono::high_resolution_clock Clock
;
74 std::future
<T
> f
= p
.get_future();
75 support::make_test_thread(func1
, std::move(p
)).detach();
77 assert(f
.wait_until(Clock::now() + ms(10)) == std::future_status::timeout
);
80 // allow the worker thread to produce the result and wait until the worker is done
81 set_worker_thread_state(WorkerThreadState::AllowedToRun
);
82 wait_for_worker_thread_state(WorkerThreadState::Exiting
);
84 assert(f
.wait_until(Clock::now() + ms(10)) == std::future_status::ready
);
92 std::future
<T
> f
= p
.get_future();
93 support::make_test_thread(func3
, std::move(p
)).detach();
95 assert(f
.wait_until(Clock::now() + ms(10)) == std::future_status::timeout
);
98 // allow the worker thread to produce the result and wait until the worker is done
99 set_worker_thread_state(WorkerThreadState::AllowedToRun
);
100 wait_for_worker_thread_state(WorkerThreadState::Exiting
);
102 assert(f
.wait_until(Clock::now() + ms(10)) == std::future_status::ready
);
110 std::future
<T
> f
= p
.get_future();
111 support::make_test_thread(func5
, std::move(p
)).detach();
113 assert(f
.wait_until(Clock::now() + ms(10)) == std::future_status::timeout
);
116 // allow the worker thread to produce the result and wait until the worker is done
117 set_worker_thread_state(WorkerThreadState::AllowedToRun
);
118 wait_for_worker_thread_state(WorkerThreadState::Exiting
);
120 assert(f
.wait_until(Clock::now() + ms(10)) == std::future_status::ready
);