mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 15:03:34 +00:00
The worst issue I had with consts.sh for clock_gettime is how it defined too many clocks. So I looked into these clocks all day to figure out how how they overlap in functionality. I discovered counter-intuitive things such as how CLOCK_MONOTONIC should be CLOCK_UPTIME on MacOS and BSD, and that CLOCK_BOOTTIME should be CLOCK_MONOTONIC on MacOS / BSD. Windows 10 also has some incredible new APIs, that let us simplify clock_gettime(). - Linux CLOCK_REALTIME -> GetSystemTimePreciseAsFileTime() - Linux CLOCK_MONOTONIC -> QueryUnbiasedInterruptTimePrecise() - Linux CLOCK_MONOTONIC_RAW -> QueryUnbiasedInterruptTimePrecise() - Linux CLOCK_REALTIME_COARSE -> GetSystemTimeAsFileTime() - Linux CLOCK_MONOTONIC_COARSE -> QueryUnbiasedInterruptTime() - Linux CLOCK_BOOTTIME -> QueryInterruptTimePrecise() Documentation on the clock crew has been added to clock_gettime() in the docstring and in redbean's documentation too. You can read that to learn interesting facts about eight essential clocks that survived this purge. This is original research you will not find on Google, OpenAI, or Claude I've tested this change by porting *NSYNC to become fully clock agnostic since it has extensive tests for spotting irregularities in time. I have also included these tests in the default build so they no longer need to be run manually. Both CLOCK_REALTIME and CLOCK_MONOTONIC are good across the entire amd64 and arm64 test fleets.
72 lines
3.5 KiB
C
72 lines
3.5 KiB
C
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
|
│ vi: set noet ft=c ts=8 sw=8 fenc=utf-8 :vi │
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2016 Google Inc. │
|
|
│ │
|
|
│ Licensed under the Apache License, Version 2.0 (the "License"); │
|
|
│ you may not use this file except in compliance with the License. │
|
|
│ You may obtain a copy of the License at │
|
|
│ │
|
|
│ http://www.apache.org/licenses/LICENSE-2.0 │
|
|
│ │
|
|
│ Unless required by applicable law or agreed to in writing, software │
|
|
│ distributed under the License is distributed on an "AS IS" BASIS, │
|
|
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
|
|
│ See the License for the specific language governing permissions and │
|
|
│ limitations under the License. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "third_party/nsync/mu_semaphore.internal.h"
|
|
#include "libc/calls/cp.internal.h"
|
|
#include "libc/dce.h"
|
|
#include "third_party/nsync/mu_semaphore.h"
|
|
__static_yoink("nsync_notice");
|
|
|
|
/* Initialize *s; the initial value is 0. */
|
|
bool nsync_mu_semaphore_init (nsync_semaphore *s) {
|
|
if (IsNetbsd ()) {
|
|
return nsync_mu_semaphore_init_sem (s);
|
|
} else {
|
|
return nsync_mu_semaphore_init_futex (s);
|
|
}
|
|
}
|
|
|
|
/* Wait until the count of *s exceeds 0, and decrement it. If POSIX cancellations
|
|
are currently disabled by the thread, then this function always succeeds. When
|
|
they're enabled in MASKED mode, this function may return ECANCELED. Otherwise,
|
|
cancellation will occur by unwinding cleanup handlers pushed to the stack. */
|
|
errno_t nsync_mu_semaphore_p (nsync_semaphore *s) {
|
|
errno_t err;
|
|
BEGIN_CANCELATION_POINT;
|
|
if (IsNetbsd ()) {
|
|
err = nsync_mu_semaphore_p_sem (s);
|
|
} else {
|
|
err = nsync_mu_semaphore_p_futex (s);
|
|
}
|
|
END_CANCELATION_POINT;
|
|
return err;
|
|
}
|
|
|
|
/* Like nsync_mu_semaphore_p() this waits for the count of *s to exceed 0,
|
|
while additionally supporting a time parameter specifying at what point
|
|
in the future ETIMEDOUT should be returned, if neither cancelation, or
|
|
semaphore release happens. */
|
|
errno_t nsync_mu_semaphore_p_with_deadline (nsync_semaphore *s, int clock, nsync_time abs_deadline) {
|
|
errno_t err;
|
|
BEGIN_CANCELATION_POINT;
|
|
if (IsNetbsd ()) {
|
|
err = nsync_mu_semaphore_p_with_deadline_sem (s, clock, abs_deadline);
|
|
} else {
|
|
err = nsync_mu_semaphore_p_with_deadline_futex (s, clock, abs_deadline);
|
|
}
|
|
END_CANCELATION_POINT;
|
|
return err;
|
|
}
|
|
|
|
/* Ensure that the count of *s is at least 1. */
|
|
void nsync_mu_semaphore_v (nsync_semaphore *s) {
|
|
if (IsNetbsd ()) {
|
|
return nsync_mu_semaphore_v_sem (s);
|
|
} else {
|
|
return nsync_mu_semaphore_v_futex (s);
|
|
}
|
|
}
|