diff options
author | Mike Crowe <mac@mcrowe.com> | 2020-09-11 14:24:59 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2020-09-11 14:24:59 +0100 |
commit | f639343dc8c4fa65342a8c1fd43384999cf9f9d0 (patch) | |
tree | b2f5b46ed3bb3e1a1d2c4c7f7aae64b4207d5a36 | |
parent | 55bdee9af3cff04192c64a573fa1767b48918efa (diff) | |
download | gcc-f639343dc8c4fa65342a8c1fd43384999cf9f9d0.zip gcc-f639343dc8c4fa65342a8c1fd43384999cf9f9d0.tar.gz gcc-f639343dc8c4fa65342a8c1fd43384999cf9f9d0.tar.bz2 |
libstdc++: Improve std::async test
Add tests for waiting for the future using both chrono::steady_clock and
chrono::system_clock in preparation for dealing with those clocks
properly in futex.cc.
libstdc++-v3/ChangeLog:
* testsuite/30_threads/async/async.cc (test02): Test steady_clock
with std::future::wait_until.
(test03): Add new test templated on clock type waiting for future
associated with async to resolve.
(main): Call test03 to test both system_clock and steady_clock.
-rw-r--r-- | libstdc++-v3/testsuite/30_threads/async/async.cc | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/30_threads/async/async.cc b/libstdc++-v3/testsuite/30_threads/async/async.cc index 8c3a0c1..2089b1c 100644 --- a/libstdc++-v3/testsuite/30_threads/async/async.cc +++ b/libstdc++-v3/testsuite/30_threads/async/async.cc @@ -50,17 +50,50 @@ void test02() VERIFY( status == std::future_status::timeout ); status = f1.wait_until(std::chrono::system_clock::now()); VERIFY( status == std::future_status::timeout ); + status = f1.wait_until(std::chrono::steady_clock::now()); + VERIFY( status == std::future_status::timeout ); l.unlock(); // allow async thread to proceed f1.wait(); // wait for it to finish status = f1.wait_for(std::chrono::milliseconds(0)); VERIFY( status == std::future_status::ready ); status = f1.wait_until(std::chrono::system_clock::now()); VERIFY( status == std::future_status::ready ); + status = f1.wait_until(std::chrono::steady_clock::now()); + VERIFY( status == std::future_status::ready ); +} + +// This test is prone to failures if run on a loaded machine where the +// kernel decides not to schedule us for several seconds. It also +// assumes that no-one will warp CLOCK whilst the test is +// running when CLOCK is std::chrono::system_clock. +template<typename CLOCK> +void test03() +{ + auto const start = CLOCK::now(); + future<void> f1 = async(launch::async, []() { + std::this_thread::sleep_for(std::chrono::seconds(2)); + }); + std::future_status status; + + status = f1.wait_for(std::chrono::milliseconds(500)); + VERIFY( status == std::future_status::timeout ); + + status = f1.wait_until(start + std::chrono::seconds(1)); + VERIFY( status == std::future_status::timeout ); + + status = f1.wait_until(start + std::chrono::seconds(5)); + VERIFY( status == std::future_status::ready ); + + auto const elapsed = CLOCK::now() - start; + VERIFY( elapsed >= std::chrono::seconds(2) ); + VERIFY( elapsed < std::chrono::seconds(5) ); } int main() { test01(); test02(); + test03<std::chrono::system_clock>(); + test03<std::chrono::steady_clock>(); return 0; } |