Get threads working on all platforms

We now have a high-quality clone() implementation for creating
lightweight threads on Linux/Windows/FreeBSD/NetBSD/OpenBSD.
This commit is contained in:
Justine Tunney 2022-05-12 17:52:13 -07:00
parent 4e62cefa6e
commit fec396037a
43 changed files with 850 additions and 532 deletions

View file

@ -19,18 +19,21 @@
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/intrin/tls.h"
#include "libc/intrin/winthread.internal.h"
#include "libc/nt/thread.h"
/**
* Returns current thread id.
* @asyncsignalsafe
*/
int gettid(void) {
privileged int gettid(void) {
int rc;
int64_t wut;
struct WinThread *wt;
if (IsWindows()) {
return GetCurrentThreadId();
}
if (IsLinux()) {
asm("syscall"
: "=a"(rc) // man says always succeeds
@ -59,7 +62,7 @@ int gettid(void) {
asm("syscall"
: "=a"(rc) // man says always succeeds
: "0"(311) // _lwp_self()
: "rcx", "r11", "memory", "cc");
: "rcx", "rdx", "r11", "memory", "cc");
return rc;
}
@ -73,13 +76,5 @@ int gettid(void) {
return wut; // narrowing intentional
}
if (IsWindows()) {
if ((wt = GetWinThread())) {
return wt->pid;
} else {
return GetCurrentThreadId();
}
}
return getpid();
}