mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-30 16:28:30 +00:00
Make garbage collection thread safe
- You can now use _gc(malloc()) in multithreaded programs - This change fixes a bug where fork() on NT disabled TLS - Fixed TLS code morphing on XNU/NT, for R8-R15 registers
This commit is contained in:
parent
571c2c3c69
commit
0e2b1bfeed
37 changed files with 310 additions and 189 deletions
|
@ -230,7 +230,7 @@ privileged void __enable_tls(void) {
|
|||
|
||||
// we're checking for the following expression:
|
||||
// 0144 == p[0] && // %fs
|
||||
// 0110 == p[1] && // rex.w (64-bit operand size)
|
||||
// 0110 == (p[1] & 0373) && // rex.w (and ignore rex.r)
|
||||
// (0213 == p[2] || // mov reg/mem → reg (word-sized)
|
||||
// 0003 == p[2]) && // add reg/mem → reg (word-sized)
|
||||
// 0004 == (p[3] & 0307) && // mod/rm (4,reg,0) means sib → reg
|
||||
|
@ -239,7 +239,7 @@ privileged void __enable_tls(void) {
|
|||
// 0000 == p[6] && // displacement
|
||||
// 0000 == p[7] && // displacement
|
||||
// 0000 == p[8] // displacement
|
||||
w = READ64LE(p) & READ64LE("\377\377\377\307\377\377\377\377");
|
||||
w = READ64LE(p) & READ64LE("\377\373\377\307\377\377\377\377");
|
||||
if ((w == READ64LE("\144\110\213\004\045\000\000\000") ||
|
||||
w == READ64LE("\144\110\003\004\045\000\000\000")) &&
|
||||
!p[8]) {
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include "libc/mem/alloca.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/nt2sysv.h"
|
||||
#include "libc/nexgen32e/threaded.h"
|
||||
#include "libc/nt/console.h"
|
||||
#include "libc/nt/createfile.h"
|
||||
#include "libc/nt/enum/accessmask.h"
|
||||
|
@ -88,7 +89,6 @@ static inline textwindows ssize_t ForkIo(int64_t h, char *p, size_t n,
|
|||
static dontinline textwindows bool ForkIo2(int64_t h, void *buf, size_t n,
|
||||
bool32 (*fn)(), const char *sf) {
|
||||
ssize_t rc = ForkIo(h, buf, n, fn);
|
||||
__tls_enabled = false; // prevent tls crash in kprintf
|
||||
NTTRACE("%s(%ld, %p, %'zu) → %'zd% m", sf, h, buf, n, rc);
|
||||
return rc != -1;
|
||||
}
|
||||
|
@ -98,6 +98,7 @@ static dontinline textwindows bool WriteAll(int64_t h, void *buf, size_t n) {
|
|||
}
|
||||
|
||||
static textwindows dontinline void ReadOrDie(int64_t h, void *buf, size_t n) {
|
||||
__tls_enabled = false; // prevent tls crash in kprintf
|
||||
if (!ForkIo2(h, buf, n, ReadFile, "ReadFile")) {
|
||||
AbortFork("ReadFile");
|
||||
}
|
||||
|
|
|
@ -1,27 +1,24 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_RUNTIME_GC_H_
|
||||
#define COSMOPOLITAN_LIBC_RUNTIME_GC_H_
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/nexgen32e/stackframe.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
void *_gc(void *) hidden;
|
||||
void *_defer(void *, void *) hidden;
|
||||
void __defer(struct StackFrame *, void *, void *) hidden;
|
||||
void __deferer(struct StackFrame *, void *, void *) hidden;
|
||||
void _gclongjmp(jmp_buf, int) dontthrow wontreturn;
|
||||
void *_gc(void *);
|
||||
void *_defer(void *, void *);
|
||||
void __defer(void *, void *, void *);
|
||||
void _gclongjmp(void *, int) dontthrow wontreturn;
|
||||
void _gc_free(void *);
|
||||
|
||||
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
||||
#define _gc(THING) _defer((void *)_weakfree, (void *)(THING))
|
||||
#define _defer(FN, ARG) \
|
||||
({ \
|
||||
autotype(ARG) Arg = (ARG); \
|
||||
/* prevent weird opts like tail call */ \
|
||||
asm volatile("" : "+g"(Arg) : : "memory"); \
|
||||
__defer((struct StackFrame *)__builtin_frame_address(0), FN, Arg); \
|
||||
asm volatile("" : "+g"(Arg) : : "memory"); \
|
||||
Arg; \
|
||||
#define _gc(THING) _defer((void *)_gc_free, (void *)(THING))
|
||||
#define _defer(FN, ARG) \
|
||||
({ \
|
||||
autotype(ARG) Arg = (ARG); \
|
||||
/* prevent weird opts like tail call */ \
|
||||
asm volatile("" : "+g"(Arg) : : "memory"); \
|
||||
__defer(__builtin_frame_address(0), FN, Arg); \
|
||||
asm volatile("" : "+g"(Arg) : : "memory"); \
|
||||
Arg; \
|
||||
})
|
||||
#endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue