Improve system calls

- Wrap clock_getres()
- Wrap sched_setscheduler()
- Make sleep() api conformant
- Polyfill sleep() using select()
- Improve clock_gettime() polyfill
- Make nanosleep() POSIX conformant
- Slightly improve some DNS functions
- Further strengthen pledge() sandboxing
- Improve rounding of timeval / timespec
- Allow layering of pledge() calls on Linux
- Polyfill sched_yield() using select() on XNU
- Delete more system constants we probably don't need
This commit is contained in:
Justine Tunney 2022-07-08 06:29:24 -07:00
parent 5df3e4e7a8
commit 853b6c3864
330 changed files with 1971 additions and 1223 deletions

View file

@ -16,51 +16,22 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/likely.h"
#include "libc/calls/clock_gettime.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/timespec.h"
#include "libc/fmt/conv.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/spinlock.h"
#include "libc/nexgen32e/rdtsc.h"
#include "libc/nexgen32e/threaded.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/nt/struct/filetime.h"
#include "libc/nt/synchronization.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/errfuns.h"
#include "libc/time/clockstonanos.internal.h"
textwindows int sys_clock_gettime_nt(int clockid, struct timespec *ts) {
uint64_t nanos;
static bool once;
static char lock;
struct timespec res;
static uint64_t base;
textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
struct NtFileTime ft;
static struct timespec mono;
if (!ts) return efault();
if (clockid == CLOCK_REALTIME) {
if (clock == CLOCK_REALTIME) {
GetSystemTimeAsFileTime(&ft);
*ts = FileTimeToTimeSpec(ft);
return 0;
} else if ((clockid == CLOCK_MONOTONIC || clockid == CLOCK_MONOTONIC_RAW) &&
X86_HAVE(INVTSC)) {
// this routine stops being monotonic after 194 years of uptime
if (__threaded) _spinlock(&lock);
if (UNLIKELY(!once)) {
GetSystemTimeAsFileTime(&ft);
mono = FileTimeToTimeSpec(ft);
base = rdtsc();
once = true;
}
nanos = ClocksToNanos(rdtsc(), base);
res = mono;
res.tv_sec += nanos / 1000000000;
res.tv_nsec += nanos % 1000000000;
_spunlock(&lock);
*ts = res;
return 0;
} else if (clock == CLOCK_MONOTONIC) {
return sys_clock_gettime_mono(ts);
} else {
return einval();
}