mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 15:03:34 +00:00
This is one of the few POSIX APIs that was missing. It lets you choose a monotonic clock for your condition variables. This might improve perf on some platforms. It might also grant more flexibility with NTP configs. I know Qt is one project that believes it needs this. To introduce this, I needed to change some the *NSYNC APIs, to support passing a clock param. There's also new benchmarks, demonstrating Cosmopolitan's supremacy over many libc implementations when it comes to mutex performance. Cygwin has an alarmingly bad pthread_mutex_t implementation. It is so bad that they would have been significantly better off if they'd used naive spinlocks.
121 lines
4.8 KiB
C
121 lines
4.8 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
│ vi: set et ft=c ts=8 sts=2 sw=2 fenc=utf-8 :vi │
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
│ │
|
|
│ 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. │
|
|
│ │
|
|
│ 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. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/calls/struct/timespec.h"
|
|
#include "libc/calls/struct/timespec.internal.h"
|
|
#include "libc/calls/syscall_support-sysv.internal.h"
|
|
#include "libc/dce.h"
|
|
#include "libc/errno.h"
|
|
#include "libc/intrin/describeflags.h"
|
|
#include "libc/intrin/strace.h"
|
|
#include "libc/runtime/syslib.internal.h"
|
|
#include "libc/sysv/consts/clock.h"
|
|
|
|
#ifdef __aarch64__
|
|
#define CGT_VDSO __vdsosym("LINUX_2.6.39", "__kernel_clock_gettime")
|
|
#else
|
|
#define CGT_VDSO __vdsosym("LINUX_2.6", "__vdso_clock_gettime")
|
|
#endif
|
|
|
|
typedef int clock_gettime_f(int, struct timespec *);
|
|
|
|
static clock_gettime_f *__clock_gettime_get(void) {
|
|
clock_gettime_f *cgt;
|
|
if (IsLinux() && (cgt = CGT_VDSO)) {
|
|
return cgt;
|
|
} else if (__syslib) {
|
|
return (void *)__syslib->__clock_gettime;
|
|
} else if (IsWindows()) {
|
|
return sys_clock_gettime_nt;
|
|
#ifdef __x86_64__
|
|
} else if (IsXnu()) {
|
|
return sys_clock_gettime_xnu;
|
|
#endif
|
|
} else {
|
|
return sys_clock_gettime;
|
|
}
|
|
}
|
|
|
|
static int __clock_gettime_init(int, struct timespec *);
|
|
static clock_gettime_f *__clock_gettime = __clock_gettime_init;
|
|
static int __clock_gettime_init(int clockid, struct timespec *ts) {
|
|
clock_gettime_f *cgt;
|
|
__clock_gettime = cgt = __clock_gettime_get();
|
|
return cgt(clockid, ts);
|
|
}
|
|
|
|
static int clock_gettime_impl(int clock, struct timespec *ts) {
|
|
int rc;
|
|
if (!IsLinux())
|
|
return __clock_gettime(clock, ts);
|
|
TryAgain:
|
|
|
|
// Ensure fallback for old Linux sticks.
|
|
if (clock == 4 /* CLOCK_MONOTONIC_RAW */)
|
|
clock = CLOCK_MONOTONIC_RAW;
|
|
|
|
// Call appropriate implementation.
|
|
rc = __clock_gettime(clock, ts);
|
|
|
|
// CLOCK_MONOTONIC_RAW is Linux 2.6.28+ so not available on RHEL5
|
|
if (rc == -EINVAL && clock == 4 /* CLOCK_MONOTONIC_RAW */) {
|
|
CLOCK_MONOTONIC_RAW = CLOCK_MONOTONIC;
|
|
CLOCK_MONOTONIC_RAW_APPROX = CLOCK_MONOTONIC;
|
|
goto TryAgain;
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
/**
|
|
* Returns nanosecond time.
|
|
*
|
|
* @param clock supports the following values across OSes:
|
|
* - `CLOCK_REALTIME`
|
|
* - `CLOCK_MONOTONIC`
|
|
* - `CLOCK_MONOTONIC_RAW`
|
|
* - `CLOCK_MONOTONIC_RAW_APPROX`
|
|
* - `CLOCK_REALTIME_FAST`
|
|
* - `CLOCK_REALTIME_COARSE`
|
|
* - `CLOCK_REALTIME_PRECISE`
|
|
* - `CLOCK_MONOTONIC_FAST`
|
|
* - `CLOCK_MONOTONIC_COARSE`
|
|
* - `CLOCK_MONOTONIC_PRECISE`
|
|
* - `CLOCK_THREAD_CPUTIME_ID`
|
|
* - `CLOCK_PROCESS_CPUTIME_ID`
|
|
* @param ts is where the result is stored (or null to do clock check)
|
|
* @return 0 on success, or -1 w/ errno
|
|
* @raise EFAULT if `ts` points to invalid memory
|
|
* @error EINVAL if `clock` isn't supported on this system
|
|
* @error EPERM if pledge() is in play without stdio promise
|
|
* @error ESRCH on NetBSD if PID/TID OR'd into `clock` wasn't found
|
|
* @see strftime(), gettimeofday()
|
|
* @asyncsignalsafe
|
|
* @vforksafe
|
|
*/
|
|
int clock_gettime(int clock, struct timespec *ts) {
|
|
// threads on win32 stacks call this so we can't asan check *ts
|
|
int rc = clock_gettime_impl(clock, ts);
|
|
if (rc) {
|
|
errno = -rc;
|
|
rc = -1;
|
|
}
|
|
TIMETRACE("clock_gettime(%s, [%s]) → %d% m", DescribeClockName(clock),
|
|
DescribeTimespec(rc, ts), rc);
|
|
return rc;
|
|
}
|