|
| 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