From 49638674a46e58ff8bfb41c5346386dca9569375 Mon Sep 17 00:00:00 2001 From: Mike Crowe Date: Mon, 2 Dec 2019 16:22:53 +0000 Subject: [PATCH] libstdc++: Improve tests for try_lock_until members of mutex types 2019-12-02 Mike Crowe * testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc: New test. Ensure that timed_mutex::try_lock_until actually times out after the specified time when using both system_clock and steady_clock. * testsuite/30_threads/timed_mutex/try_lock_until/3.cc: New test. Likewise but for recursive_timed_mutex. * testsuite/30_threads/timed_mutex/try_lock_until/57641.cc: Template test functions and use them to test both steady_clock and system_clock. * testsuite/30_threads/unique_lock/locking/4.cc: Likewise. Wrap call to timed_mutex::try_lock_until in VERIFY macro to check its return value. From-SVN: r278900 --- libstdc++-v3/ChangeLog | 14 ++++ .../recursive_timed_mutex/try_lock_until/3.cc | 74 +++++++++++++++++++ .../timed_mutex/try_lock_until/3.cc | 74 +++++++++++++++++++ .../timed_mutex/try_lock_until/57641.cc | 18 +++-- .../30_threads/unique_lock/locking/4.cc | 14 +++- 5 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc create mode 100644 libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/3.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 238e356c843..24a4a35d173 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,17 @@ +2019-12-02 Mike Crowe + + * testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc: + New test. Ensure that timed_mutex::try_lock_until actually times out + after the specified time when using both system_clock and + steady_clock. + * testsuite/30_threads/timed_mutex/try_lock_until/3.cc: New test. + Likewise but for recursive_timed_mutex. + * testsuite/30_threads/timed_mutex/try_lock_until/57641.cc: Template + test functions and use them to test both steady_clock and system_clock. + * testsuite/30_threads/unique_lock/locking/4.cc: Likewise. Wrap call + to timed_mutex::try_lock_until in VERIFY macro to check its return + value. + 2019-11-30 Jonathan Wakely * acinclude.m4 (GLIBCXX_ENABLE_FILESYSTEM_TS): Enable by default for diff --git a/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc b/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc new file mode 100644 index 00000000000..162d0f8542b --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc @@ -0,0 +1,74 @@ +// { dg-do run } +// { dg-options "-pthread" } +// { dg-require-effective-target c++14 } +// { dg-require-effective-target pthread } +// { dg-require-gthreads "" } + +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + + +#include +#include +#include +#include + +template +void test() +{ + typedef std::recursive_timed_mutex mutex_type; + + try + { + mutex_type m; + m.lock(); + bool b; + + std::thread t([&] { + try + { + using namespace std::chrono; + const auto timeout = 100ms; + const auto start = clock_type::now(); + const auto b = m.try_lock_until(start + timeout); + const auto t = clock_type::now() - start; + VERIFY( !b ); + VERIFY( t >= timeout ); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + }); + t.join(); + m.unlock(); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + catch (...) + { + VERIFY( false ); + } +} + +int main() +{ + test(); + test(); +} diff --git a/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/3.cc b/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/3.cc new file mode 100644 index 00000000000..f0bf90f9b9e --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/3.cc @@ -0,0 +1,74 @@ +// { dg-do run } +// { dg-options "-pthread" } +// { dg-require-effective-target c++14 } +// { dg-require-effective-target pthread } +// { dg-require-gthreads "" } + +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + + +#include +#include +#include +#include + +template +void test() +{ + typedef std::timed_mutex mutex_type; + + try + { + mutex_type m; + m.lock(); + bool b; + + std::thread t([&] { + try + { + using namespace std::chrono; + const auto timeout = 100ms; + const auto start = clock_type::now(); + const auto b = m.try_lock_until(start + timeout); + const auto t = clock_type::now() - start; + VERIFY( !b ); + VERIFY( t >= timeout ); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + }); + t.join(); + m.unlock(); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + catch (...) + { + VERIFY( false ); + } +} + +int main() +{ + test(); + test(); +} diff --git a/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc b/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc index 12ee52fa064..6355d8f327a 100644 --- a/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc +++ b/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc @@ -49,18 +49,26 @@ struct clock std::timed_mutex mx; bool locked = false; +template void f() { - locked = mx.try_lock_until(clock::now() + C::milliseconds(1)); + locked = mx.try_lock_until(ClockType::now() + C::milliseconds(1)); } -int main() +template +void test() { std::lock_guard l(mx); - auto start = C::system_clock::now(); - std::thread t(f); + auto start = ClockType::now(); + std::thread t(f); t.join(); - auto stop = C::system_clock::now(); + auto stop = ClockType::now(); VERIFY( (stop - start) < C::seconds(9) ); VERIFY( !locked ); } + +int main() +{ + test(); + test(); +} diff --git a/libstdc++-v3/testsuite/30_threads/unique_lock/locking/4.cc b/libstdc++-v3/testsuite/30_threads/unique_lock/locking/4.cc index fe0935f94c7..2c46a867822 100644 --- a/libstdc++-v3/testsuite/30_threads/unique_lock/locking/4.cc +++ b/libstdc++-v3/testsuite/30_threads/unique_lock/locking/4.cc @@ -26,21 +26,22 @@ #include #include -int main() +template +void test() { typedef std::timed_mutex mutex_type; typedef std::unique_lock lock_type; - typedef std::chrono::system_clock clock_type; try { mutex_type m; lock_type l(m, std::defer_lock); - clock_type::time_point t = clock_type::now() + std::chrono::seconds(1); + const typename clock_type::time_point t = clock_type::now() + + std::chrono::seconds(1); try { - l.try_lock_until(t); + VERIFY( l.try_lock_until(t) ); } catch(const std::system_error&) { @@ -61,6 +62,11 @@ int main() { VERIFY( false ); } +} +int main() +{ + test(); + test(); return 0; } -- 2.30.2