68 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: no-threads10 11// This test uses the POSIX header <sys/time.h> which Windows doesn't provide12// UNSUPPORTED: windows13 14// ALLOW_RETRIES: 315 16// <thread>17 18// template <class Rep, class Period>19// void sleep_for(const chrono::duration<Rep, Period>& rel_time);20 21// This test ensures that we sleep for the right amount of time even when22// we get interrupted by a signal, as fixed in 58a0a70fb2f1.23 24#include <thread>25#include <cassert>26#include <chrono>27#include <cstring> // for std::memset28 29#include <signal.h>30#include <sys/time.h>31 32#include "test_macros.h"33 34void sig_action(int) {}35 36int main(int, char**)37{38 int ec;39 struct sigaction action;40 action.sa_handler = &sig_action;41 sigemptyset(&action.sa_mask);42 action.sa_flags = 0;43 44 ec = sigaction(SIGALRM, &action, nullptr);45 assert(!ec);46 47 struct itimerval it;48 std::memset(&it, 0, sizeof(itimerval));49 it.it_value.tv_sec = 0;50 it.it_value.tv_usec = 250000;51 // This will result in a SIGALRM getting fired resulting in the nanosleep52 // inside sleep_for getting EINTR.53 ec = setitimer(ITIMER_REAL, &it, nullptr);54 assert(!ec);55 56 typedef std::chrono::system_clock Clock;57 typedef Clock::time_point time_point;58 std::chrono::milliseconds ms(500);59 time_point t0 = Clock::now();60 std::this_thread::sleep_for(ms);61 time_point t1 = Clock::now();62 // NOTE: Operating systems are (by default) best effort and therefore we may63 // have slept longer, perhaps much longer than we requested.64 assert(t1 - t0 >= ms);65 66 return 0;67}68