Make more threading improvements

- ASAN memory morgue is now lockless
- Make C11 atomics header more portable
- Rewrote pthread keys support to be lockless
- Simplify Python's unicode table unpacking code
- Make crash report write(2) closer to being atomic
- Make it possible to strace/ftrace a single thread
- ASAN now checks nul-terminated strings fast and properly
- Windows fork() now restores TLS memory of calling thread
This commit is contained in:
Justine Tunney 2022-11-01 22:36:03 -07:00
parent d7b88734cd
commit e522aa3a07
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
189 changed files with 1363 additions and 1217 deletions

View file

@ -18,7 +18,8 @@
*/
#include "libc/assert.h"
#include "libc/fmt/leb128.h"
#include "libc/intrin/lockcmpxchg.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/kmalloc.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/runtime/internal.h"
@ -29,8 +30,7 @@
/**
* Loads δzd encoded data once atomically.
*
* @param o points to your static init guard
* @param t points to your static pointer holder
* @param a points to your static pointer holder
* @param p is read-only data compressed using raw deflate
* @param n is byte length of deflated data
* @param m is byte length of inflated data
@ -40,17 +40,23 @@
* @return pointer to decoded data
* @threadsafe
*/
void *xloadzd(bool *o, void **t, const void *p, size_t n, size_t m, size_t c,
void *xloadzd(_Atomic(void *) * a, const void *p, size_t n, size_t m, size_t c,
size_t z, uint32_t s) {
void *r;
size_t i;
z_stream zs;
char *q, *b;
void *r, *g;
int64_t x, y;
if ((r = atomic_load_explicit(a, memory_order_acquire))) return r;
_unassert(z == 2 || z == 4);
b = q = malloc(m);
__inflate(q, m, p, n);
r = memalign(z, c * z);
if (!(b = q = malloc(m))) return 0;
if (__inflate(q, m, p, n)) {
free(q);
return 0;
}
if (!(r = kmalloc(c * z))) {
free(q);
return 0;
}
for (x = i = 0; i < c; ++i) {
b += unzleb64(b, 10, &y);
x += y;
@ -61,12 +67,11 @@ void *xloadzd(bool *o, void **t, const void *p, size_t n, size_t m, size_t c,
}
}
free(q);
_npassert(crc32_z(0, r, c * z) == s);
if (_lockcmpxchg(t, 0, r)) {
__cxa_atexit(free, r, 0);
} else {
free(q);
assert(crc32_z(0, r, c * z) == s);
g = 0;
if (!atomic_compare_exchange_strong_explicit(a, &g, r, memory_order_relaxed,
memory_order_relaxed)) {
r = g;
}
*o = true;
return *t;
return r;
}