Skip to content

Commit 9d58dab

Browse files
committed
[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.
1 parent 6b67e22 commit 9d58dab

File tree

2 files changed

+77
-33
lines changed

2 files changed

+77
-33
lines changed

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

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// UNSUPPORTED: libcpp-has-no-threads
108

11-
// This test uses the POSIX header <sys/time.h> which Windows doesn't provide
12-
// UNSUPPORTED: windows
9+
// UNSUPPORTED: libcpp-has-no-threads
1310

14-
// This test depends on signal behaviour until r210210, so some system libs
15-
// don't pass.
16-
//
11+
// Until 58a0a70fb2f1, this_thread::sleep_for could get interrupted by
12+
// signals and this test would fail. Disable the test on the corresponding
13+
// system libraries.
1714
// XFAIL: with_system_cxx_lib=macosx10.11
1815
// XFAIL: with_system_cxx_lib=macosx10.10
1916
// XFAIL: with_system_cxx_lib=macosx10.9
@@ -24,36 +21,11 @@
2421
// void sleep_for(const chrono::duration<Rep, Period>& rel_time);
2522

2623
#include <thread>
27-
#include <cstdlib>
2824
#include <cassert>
29-
#include <cstring>
30-
#include <signal.h>
31-
#include <sys/time.h>
32-
33-
#include "test_macros.h"
34-
35-
void sig_action(int) {}
25+
#include <chrono>
3626

3727
int main(int, char**)
3828
{
39-
int ec;
40-
struct sigaction action;
41-
action.sa_handler = &sig_action;
42-
sigemptyset(&action.sa_mask);
43-
action.sa_flags = 0;
44-
45-
ec = sigaction(SIGALRM, &action, nullptr);
46-
assert(!ec);
47-
48-
struct itimerval it;
49-
std::memset(&it, 0, sizeof(itimerval));
50-
it.it_value.tv_sec = 0;
51-
it.it_value.tv_usec = 250000;
52-
// This will result in a SIGALRM getting fired resulting in the nanosleep
53-
// inside sleep_for getting EINTR.
54-
ec = setitimer(ITIMER_REAL, &it, nullptr);
55-
assert(!ec);
56-
5729
typedef std::chrono::system_clock Clock;
5830
typedef Clock::time_point time_point;
5931
std::chrono::milliseconds ms(500);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: libcpp-has-no-threads
10+
11+
// This test uses the POSIX header <sys/time.h> which Windows doesn't provide
12+
// UNSUPPORTED: windows
13+
14+
// Until 58a0a70fb2f1, this_thread::sleep_for could get interrupted by
15+
// signals and this test would fail. Disable the test on the corresponding
16+
// system libraries.
17+
// XFAIL: with_system_cxx_lib=macosx10.11
18+
// XFAIL: with_system_cxx_lib=macosx10.10
19+
// XFAIL: with_system_cxx_lib=macosx10.9
20+
21+
// <thread>
22+
23+
// template <class Rep, class Period>
24+
// void sleep_for(const chrono::duration<Rep, Period>& rel_time);
25+
26+
// This test ensures that we sleep for the right amount of time even when
27+
// we get interrupted by a signal, as fixed in 58a0a70fb2f1.
28+
29+
#include <thread>
30+
#include <cassert>
31+
#include <chrono>
32+
#include <cstring> // for std::memset
33+
34+
#include <signal.h>
35+
#include <sys/time.h>
36+
37+
#include "test_macros.h"
38+
39+
void sig_action(int) {}
40+
41+
int main(int, char**)
42+
{
43+
int ec;
44+
struct sigaction action;
45+
action.sa_handler = &sig_action;
46+
sigemptyset(&action.sa_mask);
47+
action.sa_flags = 0;
48+
49+
ec = sigaction(SIGALRM, &action, nullptr);
50+
assert(!ec);
51+
52+
struct itimerval it;
53+
std::memset(&it, 0, sizeof(itimerval));
54+
it.it_value.tv_sec = 0;
55+
it.it_value.tv_usec = 250000;
56+
// This will result in a SIGALRM getting fired resulting in the nanosleep
57+
// inside sleep_for getting EINTR.
58+
ec = setitimer(ITIMER_REAL, &it, nullptr);
59+
assert(!ec);
60+
61+
typedef std::chrono::system_clock Clock;
62+
typedef Clock::time_point time_point;
63+
std::chrono::milliseconds ms(500);
64+
time_point t0 = Clock::now();
65+
std::this_thread::sleep_for(ms);
66+
time_point t1 = Clock::now();
67+
// NOTE: Operating systems are (by default) best effort and therefore we may
68+
// have slept longer, perhaps much longer than we requested.
69+
assert(t1 - t0 >= ms);
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)