libstdc++: Improve tests for try_lock_until members of mutex types
authorMike Crowe <mac@mcrowe.com>
Mon, 2 Dec 2019 16:22:53 +0000 (16:22 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 2 Dec 2019 16:22:53 +0000 (16:22 +0000)
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

libstdc++-v3/ChangeLog
libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc [new file with mode: 0644]
libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/3.cc [new file with mode: 0644]
libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc
libstdc++-v3/testsuite/30_threads/unique_lock/locking/4.cc

index 238e356..24a4a35 100644 (file)
@@ -1,3 +1,17 @@
+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>
 
        * 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 (file)
index 0000000..162d0f8
--- /dev/null
@@ -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
+// <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>();
+}
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 (file)
index 0000000..f0bf90f
--- /dev/null
@@ -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
+// <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>();
+}
index 12ee52f..6355d8f 100644 (file)
@@ -49,18 +49,26 @@ struct clock
 std::timed_mutex mx;
 bool locked = false;
 
+template <typename ClockType>
 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);
-  auto start = C::system_clock::now();
-  std::thread t(f);
+  auto start = ClockType::now();
+  std::thread t(f<ClockType>);
   t.join();
-  auto stop = C::system_clock::now();
+  auto stop = ClockType::now();
   VERIFY( (stop - start) < C::seconds(9) );
   VERIFY( !locked );
 }
+
+int main()
+{
+  test<C::system_clock>();
+  test<C::steady_clock>();
+}
index fe0935f..2c46a86 100644 (file)
 #include <system_error>
 #include <testsuite_hooks.h>
 
-int main()
+template <typename clock_type>
+void test()
 {
   typedef std::timed_mutex mutex_type;
   typedef std::unique_lock<mutex_type> 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<std::chrono::system_clock>();
+  test<std::chrono::steady_clock>();
   return 0;
 }