Commit 49638674 by Mike Crowe Committed by Jonathan Wakely

libstdc++: Improve tests for try_lock_until members of mutex types

2019-12-02  Mike Crowe  <mac@mcrowe.com>

	* 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
parent 74fee042
2019-12-02 Mike Crowe <mac@mcrowe.com>
* 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 <jwakely@redhat.com> 2019-11-30 Jonathan Wakely <jwakely@redhat.com>
* acinclude.m4 (GLIBCXX_ENABLE_FILESYSTEM_TS): Enable by default for * acinclude.m4 (GLIBCXX_ENABLE_FILESYSTEM_TS): Enable by default for
......
// { 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
// <http://www.gnu.org/licenses/>.
#include <mutex>
#include <thread>
#include <system_error>
#include <testsuite_hooks.h>
template <typename clock_type>
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<std::chrono::system_clock>();
test<std::chrono::steady_clock>();
}
// { 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
// <http://www.gnu.org/licenses/>.
#include <mutex>
#include <thread>
#include <system_error>
#include <testsuite_hooks.h>
template <typename clock_type>
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<std::chrono::system_clock>();
test<std::chrono::steady_clock>();
}
...@@ -49,18 +49,26 @@ struct clock ...@@ -49,18 +49,26 @@ struct clock
std::timed_mutex mx; std::timed_mutex mx;
bool locked = false; bool locked = false;
template <typename ClockType>
void f() 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 <typename ClockType>
void test()
{ {
std::lock_guard<std::timed_mutex> l(mx); std::lock_guard<std::timed_mutex> l(mx);
auto start = C::system_clock::now(); auto start = ClockType::now();
std::thread t(f); std::thread t(f<ClockType>);
t.join(); t.join();
auto stop = C::system_clock::now(); auto stop = ClockType::now();
VERIFY( (stop - start) < C::seconds(9) ); VERIFY( (stop - start) < C::seconds(9) );
VERIFY( !locked ); VERIFY( !locked );
} }
int main()
{
test<C::system_clock>();
test<C::steady_clock>();
}
...@@ -26,21 +26,22 @@ ...@@ -26,21 +26,22 @@
#include <system_error> #include <system_error>
#include <testsuite_hooks.h> #include <testsuite_hooks.h>
int main() template <typename clock_type>
void test()
{ {
typedef std::timed_mutex mutex_type; typedef std::timed_mutex mutex_type;
typedef std::unique_lock<mutex_type> lock_type; typedef std::unique_lock<mutex_type> lock_type;
typedef std::chrono::system_clock clock_type;
try try
{ {
mutex_type m; mutex_type m;
lock_type l(m, std::defer_lock); 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 try
{ {
l.try_lock_until(t); VERIFY( l.try_lock_until(t) );
} }
catch(const std::system_error&) catch(const std::system_error&)
{ {
...@@ -61,6 +62,11 @@ int main() ...@@ -61,6 +62,11 @@ int main()
{ {
VERIFY( false ); VERIFY( false );
} }
}
int main()
{
test<std::chrono::system_clock>();
test<std::chrono::steady_clock>();
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment