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

@ -16,10 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/lockcmpxchg.h"
#include "libc/log/check.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/kmalloc.h"
#include "libc/runtime/internal.h"
#include "libc/x/x.h"
#include "third_party/zlib/zlib.h"
@ -27,43 +26,30 @@
* Inflates data once atomically, e.g.
*
* void *GetData(void) {
* static char once;
* static void *ptr;
* static _Atomic(void *) ptr;
* static const unsigned char rodata[] = {...};
* if (once) return ptr;
* return xload(&once, &ptr, rodata, 112, 1024);
* return xload(&ptr, rodata, 112, 1024);
* }
*
* The above is an example of how this helper may be used to have lazy
* loading of big infrequently accessed image data.
*
* @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
* @return pointer to inflated data
* @threadsafe
*/
void *xload(bool *o, void **t, const void *p, size_t n, size_t m) {
void *q;
z_stream zs;
q = malloc(m);
zs.zfree = 0;
zs.zalloc = 0;
zs.next_in = p;
zs.avail_in = n;
zs.total_in = n;
zs.avail_out = m;
zs.total_out = m;
zs.next_out = (void *)q;
inflateInit2(&zs, -MAX_WBITS);
inflate(&zs, Z_NO_FLUSH);
if (_lockcmpxchg(t, 0, q)) {
__cxa_atexit(free, q, 0);
} else {
free(q);
void *xload(_Atomic(void *) * a, const void *p, size_t n, size_t m) {
void *r, *z;
if ((r = atomic_load_explicit(a, memory_order_acquire))) return r;
if (!(r = kmalloc(m))) return 0;
if (__inflate(r, m, p, n)) return 0;
z = 0;
if (!atomic_compare_exchange_strong_explicit(a, &z, r, memory_order_release,
memory_order_relaxed)) {
r = z;
}
*o = true;
return *t;
return r;
}