mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:48:11 +00:00
sleep: Support fractional sleep lengths
This commit is contained in:
parent
c5df0532c0
commit
fd29bed656
1 changed files with 21 additions and 4 deletions
|
@ -28,6 +28,7 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static volatile bool g_interrupted;
|
static volatile bool g_interrupted;
|
||||||
|
@ -38,7 +39,7 @@ static void handle_sigint(int)
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
int secs;
|
double secs;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_positional_argument(secs, "Number of seconds to sleep for", "num-seconds");
|
args_parser.add_positional_argument(secs, "Number of seconds to sleep for", "num-seconds");
|
||||||
|
@ -54,9 +55,25 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned remaining = sleep(secs);
|
double whole_seconds = static_cast<time_t>(secs);
|
||||||
if (remaining) {
|
double fraction = secs - whole_seconds;
|
||||||
printf("Sleep interrupted with %u seconds remaining.\n", remaining);
|
|
||||||
|
timespec requested_sleep {
|
||||||
|
.tv_sec = static_cast<time_t>(whole_seconds),
|
||||||
|
.tv_nsec = static_cast<long>(fraction * (double)1000000000),
|
||||||
|
};
|
||||||
|
|
||||||
|
timespec remaining_sleep {};
|
||||||
|
|
||||||
|
if (clock_nanosleep(CLOCK_MONOTONIC, 0, &requested_sleep, &remaining_sleep) < 0) {
|
||||||
|
if (errno != EINTR) {
|
||||||
|
perror("clock_nanosleep");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remaining_sleep.tv_sec || remaining_sleep.tv_nsec) {
|
||||||
|
outln("Sleep interrupted with {}.{} seconds remaining.", remaining_sleep.tv_sec, remaining_sleep.tv_nsec);
|
||||||
}
|
}
|
||||||
|
|
||||||
signal(SIGINT, SIG_DFL);
|
signal(SIGINT, SIG_DFL);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue