[libcxx-commits] [libcxx] 9d58dab - [libc++] Split off part of a test that require signals into a separate test

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 4 08:20:54 PST 2020


Author: Louis Dionne
Date: 2020-11-04T11:20:47-05:00
New Revision: 9d58dab6f6587f759c7fe6f30008097d8a17559d

URL: https://github.com/llvm/llvm-project/commit/9d58dab6f6587f759c7fe6f30008097d8a17559d
DIFF: https://github.com/llvm/llvm-project/commit/9d58dab6f6587f759c7fe6f30008097d8a17559d.diff

LOG: [libc++] Split off part of a test that require signals into a separate test

This will allow running the basic test on all platforms, and the part that
requires signals on platforms that support them only.

Added: 
    libcxx/test/libcxx/thread/thread.threads/thread.thread.this/sleep_for.signals.pass.cpp

Modified: 
    libcxx/test/libcxx/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/libcxx/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp b/libcxx/test/libcxx/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp
index 3ff983f21c40..593d9a4bf4fd 100644
--- a/libcxx/test/libcxx/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp
+++ b/libcxx/test/libcxx/thread/thread.threads/thread.thread.this/sleep_for.pass.cpp
@@ -5,15 +5,12 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-//
-// UNSUPPORTED: libcpp-has-no-threads
 
-// This test uses the POSIX header <sys/time.h> which Windows doesn't provide
-// UNSUPPORTED: windows
+// UNSUPPORTED: libcpp-has-no-threads
 
-// This test depends on signal behaviour until r210210, so some system libs
-// don't pass.
-//
+// Until 58a0a70fb2f1, this_thread::sleep_for could get interrupted by
+// signals and this test would fail. Disable the test on the corresponding
+// system libraries.
 // XFAIL: with_system_cxx_lib=macosx10.11
 // XFAIL: with_system_cxx_lib=macosx10.10
 // XFAIL: with_system_cxx_lib=macosx10.9
@@ -24,36 +21,11 @@
 //   void sleep_for(const chrono::duration<Rep, Period>& rel_time);
 
 #include <thread>
-#include <cstdlib>
 #include <cassert>
-#include <cstring>
-#include <signal.h>
-#include <sys/time.h>
-
-#include "test_macros.h"
-
-void sig_action(int) {}
+#include <chrono>
 
 int main(int, char**)
 {
-  int ec;
-  struct sigaction action;
-  action.sa_handler = &sig_action;
-  sigemptyset(&action.sa_mask);
-  action.sa_flags = 0;
-
-  ec = sigaction(SIGALRM, &action, nullptr);
-  assert(!ec);
-
-  struct itimerval it;
-  std::memset(&it, 0, sizeof(itimerval));
-  it.it_value.tv_sec = 0;
-  it.it_value.tv_usec = 250000;
-  // This will result in a SIGALRM getting fired resulting in the nanosleep
-  // inside sleep_for getting EINTR.
-  ec = setitimer(ITIMER_REAL, &it, nullptr);
-  assert(!ec);
-
   typedef std::chrono::system_clock Clock;
   typedef Clock::time_point time_point;
   std::chrono::milliseconds ms(500);

diff  --git a/libcxx/test/libcxx/thread/thread.threads/thread.thread.this/sleep_for.signals.pass.cpp b/libcxx/test/libcxx/thread/thread.threads/thread.thread.this/sleep_for.signals.pass.cpp
new file mode 100644
index 000000000000..469dfc40919e
--- /dev/null
+++ b/libcxx/test/libcxx/thread/thread.threads/thread.thread.this/sleep_for.signals.pass.cpp
@@ -0,0 +1,72 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: libcpp-has-no-threads
+
+// This test uses the POSIX header <sys/time.h> which Windows doesn't provide
+// UNSUPPORTED: windows
+
+// Until 58a0a70fb2f1, this_thread::sleep_for could get interrupted by
+// signals and this test would fail. Disable the test on the corresponding
+// system libraries.
+// XFAIL: with_system_cxx_lib=macosx10.11
+// XFAIL: with_system_cxx_lib=macosx10.10
+// XFAIL: with_system_cxx_lib=macosx10.9
+
+// <thread>
+
+// template <class Rep, class Period>
+//   void sleep_for(const chrono::duration<Rep, Period>& rel_time);
+
+// This test ensures that we sleep for the right amount of time even when
+// we get interrupted by a signal, as fixed in 58a0a70fb2f1.
+
+#include <thread>
+#include <cassert>
+#include <chrono>
+#include <cstring> // for std::memset
+
+#include <signal.h>
+#include <sys/time.h>
+
+#include "test_macros.h"
+
+void sig_action(int) {}
+
+int main(int, char**)
+{
+  int ec;
+  struct sigaction action;
+  action.sa_handler = &sig_action;
+  sigemptyset(&action.sa_mask);
+  action.sa_flags = 0;
+
+  ec = sigaction(SIGALRM, &action, nullptr);
+  assert(!ec);
+
+  struct itimerval it;
+  std::memset(&it, 0, sizeof(itimerval));
+  it.it_value.tv_sec = 0;
+  it.it_value.tv_usec = 250000;
+  // This will result in a SIGALRM getting fired resulting in the nanosleep
+  // inside sleep_for getting EINTR.
+  ec = setitimer(ITIMER_REAL, &it, nullptr);
+  assert(!ec);
+
+  typedef std::chrono::system_clock Clock;
+  typedef Clock::time_point time_point;
+  std::chrono::milliseconds ms(500);
+  time_point t0 = Clock::now();
+  std::this_thread::sleep_for(ms);
+  time_point t1 = Clock::now();
+  // NOTE: Operating systems are (by default) best effort and therefore we may
+  // have slept longer, perhaps much longer than we requested.
+  assert(t1 - t0 >= ms);
+
+  return 0;
+}


        


More information about the libcxx-commits mailing list