Make spin locks go faster

This commit is contained in:
Justine Tunney 2024-07-25 17:14:30 -07:00
parent a31d5ea399
commit c8e25d811c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
16 changed files with 150 additions and 123 deletions

View file

@ -51,6 +51,7 @@
#include "libc/sysv/consts/sicode.h"
#include "libc/sysv/consts/ss.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
#ifdef __x86_64__
/**
@ -64,6 +65,20 @@ struct SignalFrame {
ucontext_t ctx;
};
static pthread_mutex_t __sig_lock_obj;
static void __sig_wipe(void) {
pthread_mutex_init(&__sig_lock_obj, 0);
}
static void __sig_lock(void) {
pthread_mutex_lock(&__sig_lock_obj);
}
static void __sig_unlock(void) {
pthread_mutex_unlock(&__sig_lock_obj);
}
static textwindows bool __sig_ignored_by_default(int sig) {
return sig == SIGURG || //
sig == SIGCONT || //
@ -318,11 +333,10 @@ static textwindows int __sig_killer(struct PosixThread *pt, int sig, int sic) {
// take control of thread
// suspending the thread happens asynchronously
// however getting the context blocks until it's frozen
static pthread_spinlock_t killer_lock;
pthread_spin_lock(&killer_lock);
__sig_lock();
if (SuspendThread(th) == -1u) {
STRACE("SuspendThread failed w/ %d", GetLastError());
pthread_spin_unlock(&killer_lock);
__sig_unlock();
return ESRCH;
}
struct NtContext nc;
@ -330,10 +344,10 @@ static textwindows int __sig_killer(struct PosixThread *pt, int sig, int sic) {
if (!GetThreadContext(th, &nc)) {
STRACE("GetThreadContext failed w/ %d", GetLastError());
ResumeThread(th);
pthread_spin_unlock(&killer_lock);
__sig_unlock();
return ESRCH;
}
pthread_spin_unlock(&killer_lock);
__sig_unlock();
// we can't preempt threads that masked sig or are blocked
// we can't preempt threads that are running in win32 code
@ -612,6 +626,8 @@ __attribute__((__constructor__(10))) textstartup void __sig_init(void) {
return;
AddVectoredExceptionHandler(true, (void *)__sig_crash);
SetConsoleCtrlHandler((void *)__sig_console, true);
pthread_atfork(__sig_lock, __sig_unlock, __sig_wipe);
__sig_wipe();
}
#endif /* __x86_64__ */