2020-10-11 04:18:53 +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│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
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-10-11 04:18:53 +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-10-11 04:18:53 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-03-25 14:11:44 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2020-10-11 04:18:53 +00:00
|
|
|
#include "libc/calls/struct/itimerval.h"
|
2023-06-15 20:50:42 +00:00
|
|
|
#include "libc/calls/struct/timeval.h"
|
2020-10-11 04:18:53 +00:00
|
|
|
#include "libc/sysv/consts/itimer.h"
|
2022-03-25 14:11:44 +00:00
|
|
|
#include "libc/sysv/consts/sicode.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
2020-10-11 04:18:53 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2023-05-09 08:56:56 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2023-06-15 20:50:42 +00:00
|
|
|
static struct itimerval g_setitimer;
|
2020-10-11 04:18:53 +00:00
|
|
|
|
2022-03-25 14:11:44 +00:00
|
|
|
textwindows void _check_sigalrm(void) {
|
2023-06-15 20:50:42 +00:00
|
|
|
struct timeval now;
|
|
|
|
if (timeval_iszero(g_setitimer.it_value)) return;
|
|
|
|
now = timeval_real();
|
|
|
|
if (timeval_cmp(now, g_setitimer.it_value) < 0) return;
|
|
|
|
if (timeval_iszero(g_setitimer.it_interval)) {
|
|
|
|
g_setitimer.it_value = timeval_zero;
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
g_setitimer.it_value =
|
|
|
|
timeval_add(g_setitimer.it_value, g_setitimer.it_interval);
|
|
|
|
} while (timeval_cmp(now, g_setitimer.it_value) > 0);
|
2022-03-25 14:11:44 +00:00
|
|
|
}
|
2023-06-15 20:50:42 +00:00
|
|
|
__sig_add(0, SIGALRM, SI_TIMER);
|
2020-10-11 04:18:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 20:50:42 +00:00
|
|
|
textwindows int sys_setitimer_nt(int which, const struct itimerval *neu,
|
|
|
|
struct itimerval *old) {
|
|
|
|
if (which != ITIMER_REAL || (neu && (!timeval_isvalid(neu->it_value) ||
|
|
|
|
!timeval_isvalid(neu->it_interval)))) {
|
2022-05-23 17:15:53 +00:00
|
|
|
return einval();
|
|
|
|
}
|
2023-06-15 20:50:42 +00:00
|
|
|
if (old) {
|
|
|
|
old->it_interval = g_setitimer.it_interval;
|
|
|
|
old->it_value = timeval_subz(g_setitimer.it_value, timeval_real());
|
2020-10-11 04:18:53 +00:00
|
|
|
}
|
2023-06-15 20:50:42 +00:00
|
|
|
if (neu) {
|
|
|
|
g_setitimer.it_interval = neu->it_interval;
|
|
|
|
g_setitimer.it_value = timeval_iszero(neu->it_value)
|
|
|
|
? timeval_zero
|
|
|
|
: timeval_add(timeval_real(), neu->it_value);
|
2020-10-11 04:18:53 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-09 08:56:56 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|