Tidy up the threading implementation

The organization of the source files is now much more rational.
Old experiments that didn't work out are now deleted. Naming of
things like files is now more intuitive.
This commit is contained in:
Justine Tunney 2022-09-10 02:56:25 -07:00
parent e9272f03fb
commit 155b378a39
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
199 changed files with 526 additions and 685 deletions

View file

@ -26,14 +26,13 @@
#include "libc/log/libfatal.internal.h"
#include "libc/macros.internal.h"
#include "libc/nexgen32e/msr.h"
#include "libc/nexgen32e/threaded.h"
#include "libc/nt/thread.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdalign.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/nrlinux.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#include "third_party/xed/x86.h"
#define __NR_sysarch 0x000000a5 // freebsd+netbsd
@ -48,7 +47,7 @@
#define _TLSZ ((intptr_t)_tls_size)
#define _TLDZ ((intptr_t)_tdata_size)
#define _TIBZ sizeof(struct cthread_descriptor_t)
#define _TIBZ sizeof(struct CosmoTib)
int sys_enable_tls();
@ -97,14 +96,14 @@ _Alignas(long) static char __static_tls[5008];
*/
privileged void __enable_tls(void) {
size_t siz;
cthread_t tib;
struct CosmoTib *tib;
char *mem, *tls;
siz = ROUNDUP(_TLSZ + _TIBZ, alignof(__static_tls));
if (siz <= sizeof(__static_tls)) {
// if tls requirement is small then use the static tls block
// which helps avoid a system call for appes with little tls
// this is crucial to keeping life.com 16 kilobytes in size!
_Static_assert(alignof(__static_tls) >= alignof(cthread_t));
_Static_assert(alignof(__static_tls) >= alignof(struct CosmoTib));
mem = __static_tls;
} else {
// if this binary needs a hefty tls block then we'll bank on
@ -116,17 +115,17 @@ privileged void __enable_tls(void) {
mem = weaken(_mapanon)(siz);
assert(mem);
}
tib = (cthread_t)(mem + siz - _TIBZ);
tib = (struct CosmoTib *)(mem + siz - _TIBZ);
tls = mem + siz - _TIBZ - _TLSZ;
tib->self = tib;
tib->self2 = tib;
tib->err = __errno;
tib->tib_self = tib;
tib->tib_self2 = tib;
tib->tib_errno = __errno;
if (IsLinux()) {
// gnu/systemd guarantees pid==tid for the main thread so we can
// avoid issuing a superfluous system call at startup in program
tib->tid = __pid;
tib->tib_tid = __pid;
} else {
tib->tid = sys_gettid();
tib->tib_tid = sys_gettid();
}
__repmovsb(tls, _tdata_start, _TLDZ);