mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-26 22:38:30 +00:00
Improve Windows sleep accuracy from 15ms to 15µs
This commit is contained in:
parent
b40140e6c5
commit
b490e23d63
16 changed files with 189 additions and 67 deletions
|
@ -19,65 +19,96 @@
|
|||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/nt/enum/wait.h"
|
||||
#include "libc/nt/events.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/sicode.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
|
||||
#ifdef __x86_64__
|
||||
|
||||
// returns 0 on timeout or spurious wakeup
|
||||
// returns 0 if deadline is reached
|
||||
// raises EINTR if a signal delivery interrupted wait operation
|
||||
// raises ECANCELED if this POSIX thread was canceled in masked mode
|
||||
textwindows static int _park_thread(uint32_t msdelay, sigset_t waitmask,
|
||||
textwindows static int _park_thread(struct timespec deadline, sigset_t waitmask,
|
||||
bool restartable) {
|
||||
struct PosixThread *pt = _pthread_self();
|
||||
for (;;) {
|
||||
uint32_t handl = 0;
|
||||
intptr_t hands[2];
|
||||
|
||||
// perform the wait operation
|
||||
intptr_t sigev;
|
||||
if (!(sigev = CreateEvent(0, 0, 0, 0)))
|
||||
return __winerr();
|
||||
pt->pt_event = sigev;
|
||||
pt->pt_blkmask = waitmask;
|
||||
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_EVENT,
|
||||
memory_order_release);
|
||||
//!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!//
|
||||
int sig = 0;
|
||||
uint32_t ws = 0;
|
||||
if (!_is_canceled() &&
|
||||
!(_weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))))
|
||||
ws = WaitForSingleObject(sigev, msdelay);
|
||||
//!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!//
|
||||
atomic_store_explicit(&pt->pt_blocker, 0, memory_order_release);
|
||||
CloseHandle(sigev);
|
||||
// create event object
|
||||
intptr_t sigev;
|
||||
if (!(sigev = CreateEvent(0, 0, 0, 0)))
|
||||
return __winerr();
|
||||
hands[handl++] = sigev;
|
||||
|
||||
// recursion is now safe
|
||||
if (ws == -1u)
|
||||
return __winerr();
|
||||
int handler_was_called = 0;
|
||||
if (sig)
|
||||
handler_was_called = _weaken(__sig_relay)(sig, SI_KERNEL, waitmask);
|
||||
if (_check_cancel())
|
||||
return -1;
|
||||
if (handler_was_called & SIG_HANDLED_NO_RESTART)
|
||||
return eintr();
|
||||
if (handler_was_called & SIG_HANDLED_SA_RESTART)
|
||||
if (!restartable)
|
||||
// create high precision timer if needed
|
||||
if (memcmp(&deadline, ×pec_max, sizeof(struct timespec))) {
|
||||
intptr_t hTimer;
|
||||
if ((hTimer = CreateWaitableTimer(NULL, true, NULL))) {
|
||||
int64_t due = TimeSpecToWindowsTime(deadline);
|
||||
if (SetWaitableTimer(hTimer, &due, 0, NULL, NULL, false)) {
|
||||
hands[handl++] = hTimer;
|
||||
} else {
|
||||
CloseHandle(hTimer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// perform wait operation
|
||||
struct PosixThread *pt = _pthread_self();
|
||||
pt->pt_event = sigev;
|
||||
pt->pt_blkmask = waitmask;
|
||||
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_EVENT,
|
||||
memory_order_release);
|
||||
//!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!//
|
||||
int sig = 0;
|
||||
uint32_t wi = 0;
|
||||
if (!_is_canceled() &&
|
||||
!(_weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))))
|
||||
wi = WaitForMultipleObjects(handl, hands, false, -1u);
|
||||
//!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!/!//
|
||||
atomic_store_explicit(&pt->pt_blocker, 0, memory_order_release);
|
||||
for (int i = 0; i < handl; ++i)
|
||||
CloseHandle(hands[i]);
|
||||
|
||||
// recursion is now safe
|
||||
if (wi == 1)
|
||||
return 0;
|
||||
if (wi == -1u)
|
||||
return __winerr();
|
||||
int handler_was_called = 0;
|
||||
if (!sig) {
|
||||
if (_check_cancel())
|
||||
return -1;
|
||||
if (_weaken(__sig_get))
|
||||
sig = _weaken(__sig_get)(waitmask);
|
||||
}
|
||||
if (sig)
|
||||
handler_was_called = _weaken(__sig_relay)(sig, SI_KERNEL, waitmask);
|
||||
if (_check_cancel())
|
||||
return -1;
|
||||
if (handler_was_called & SIG_HANDLED_NO_RESTART)
|
||||
return eintr();
|
||||
return 0;
|
||||
if (handler_was_called & SIG_HANDLED_SA_RESTART)
|
||||
if (!restartable)
|
||||
return eintr();
|
||||
}
|
||||
}
|
||||
|
||||
textwindows int _park_norestart(uint32_t msdelay, sigset_t waitmask) {
|
||||
return _park_thread(msdelay, waitmask, false);
|
||||
textwindows int _park_norestart(struct timespec deadline, sigset_t waitmask) {
|
||||
return _park_thread(deadline, waitmask, false);
|
||||
}
|
||||
|
||||
textwindows int _park_restartable(uint32_t msdelay, sigset_t waitmask) {
|
||||
return _park_thread(msdelay, waitmask, true);
|
||||
textwindows int _park_restartable(struct timespec deadline, sigset_t waitmask) {
|
||||
return _park_thread(deadline, waitmask, true);
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue