2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2022-03-16 20:33:13 +00:00
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-07-08 13:29:24 +00:00
|
|
|
#include "libc/assert.h"
|
2020-10-11 04:18:53 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-03-25 14:11:44 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/struct/timespec.internal.h"
|
|
|
|
#include "libc/limits.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-07-08 13:29:24 +00:00
|
|
|
#include "libc/nexgen32e/rdtsc.h"
|
2020-10-11 04:18:53 +00:00
|
|
|
#include "libc/nt/errors.h"
|
|
|
|
#include "libc/nt/synchronization.h"
|
2022-07-08 13:29:24 +00:00
|
|
|
#include "libc/sysv/consts/clock.h"
|
2022-03-25 14:11:44 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2022-07-08 13:29:24 +00:00
|
|
|
#include "libc/time/clockstonanos.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-07-08 13:29:24 +00:00
|
|
|
textwindows int sys_nanosleep_nt(const struct timespec *req,
|
|
|
|
struct timespec *rem) {
|
2022-04-16 17:40:23 +00:00
|
|
|
bool alertable;
|
|
|
|
uint32_t slice;
|
2022-07-08 13:29:24 +00:00
|
|
|
uint64_t begin;
|
|
|
|
int64_t ms, toto, nanos;
|
|
|
|
struct timespec elapsed;
|
|
|
|
|
|
|
|
// check req is legal timespec
|
|
|
|
if (!(0 <= req->tv_nsec && req->tv_nsec < 1000000000)) {
|
|
|
|
return einval();
|
|
|
|
}
|
|
|
|
|
|
|
|
// save beginning timestamp
|
|
|
|
if (!__time_critical && rem) {
|
|
|
|
begin = rdtsc();
|
|
|
|
} else {
|
|
|
|
begin = 0; // to prevent uninitialized warning
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert timespec to milliseconds
|
2022-03-25 14:11:44 +00:00
|
|
|
if (__builtin_mul_overflow(req->tv_sec, 1000, &ms) ||
|
2022-07-08 13:29:24 +00:00
|
|
|
__builtin_add_overflow(ms, (req->tv_nsec + 999999) / 1000000, &ms)) {
|
2022-04-24 16:59:22 +00:00
|
|
|
ms = INT64_MAX;
|
2022-03-25 14:11:44 +00:00
|
|
|
}
|
2022-07-08 13:29:24 +00:00
|
|
|
|
|
|
|
for (toto = ms;;) {
|
|
|
|
|
|
|
|
// check if signal was delivered
|
2022-03-25 14:11:44 +00:00
|
|
|
if (!__time_critical && _check_interrupts(false, g_fds.p)) {
|
2022-07-08 13:29:24 +00:00
|
|
|
if (rem) {
|
|
|
|
nanos = ClocksToNanos(rdtsc(), begin);
|
|
|
|
elapsed.tv_sec = nanos / 1000000000;
|
|
|
|
elapsed.tv_nsec = nanos % 1000000000;
|
|
|
|
*rem = _timespec_sub(*req, elapsed);
|
|
|
|
if (rem->tv_sec < 0) {
|
|
|
|
rem->tv_sec = 0;
|
|
|
|
rem->tv_nsec = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return eintr();
|
2022-03-25 14:11:44 +00:00
|
|
|
}
|
2022-07-08 13:29:24 +00:00
|
|
|
|
|
|
|
// configure the sleep
|
2022-04-16 17:40:23 +00:00
|
|
|
slice = MIN(__SIG_POLLING_INTERVAL_MS, ms);
|
|
|
|
if (__time_critical) {
|
|
|
|
alertable = false;
|
2022-03-25 14:11:44 +00:00
|
|
|
} else {
|
2022-04-16 17:40:23 +00:00
|
|
|
alertable = true;
|
2022-07-08 13:29:24 +00:00
|
|
|
POLLTRACE("... sleeping %'ldms of %'ld", toto - ms, toto);
|
2022-03-25 14:11:44 +00:00
|
|
|
}
|
2022-07-08 13:29:24 +00:00
|
|
|
|
|
|
|
// perform the sleep
|
2022-04-16 17:40:23 +00:00
|
|
|
if (SleepEx(slice, alertable) == kNtWaitIoCompletion) {
|
2022-07-08 13:29:24 +00:00
|
|
|
POLLTRACE("IOCP EINTR"); // in case we ever figure it out
|
2022-04-16 17:40:23 +00:00
|
|
|
continue;
|
2022-03-25 14:11:44 +00:00
|
|
|
}
|
2022-07-08 13:29:24 +00:00
|
|
|
|
|
|
|
// check if full duration has elapsed
|
|
|
|
if ((ms -= slice) <= 0) {
|
|
|
|
if (rem) {
|
|
|
|
rem->tv_sec = 0;
|
|
|
|
rem->tv_nsec = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
2022-03-25 14:11:44 +00:00
|
|
|
}
|
2022-03-16 20:33:13 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|