mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 16:52:28 +00:00
Make some more fixups
This commit is contained in:
parent
6070a53e89
commit
4ddfc47d6e
18 changed files with 81 additions and 259 deletions
|
@ -19,8 +19,6 @@
|
|||
#include "libc/assert.h"
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/intrin/spinlock.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/bsr.h"
|
||||
|
@ -30,8 +28,6 @@
|
|||
|
||||
STATIC_YOINK("__cxa_finalize");
|
||||
|
||||
static pthread_mutex_t __cxa_lock;
|
||||
|
||||
/**
|
||||
* Adds global destructor.
|
||||
*
|
||||
|
@ -51,7 +47,7 @@ noasan int __cxa_atexit(void *fp, void *arg, void *pred) {
|
|||
unsigned i;
|
||||
struct CxaAtexitBlock *b, *b2;
|
||||
_Static_assert(ATEXIT_MAX == CHAR_BIT * sizeof(b->mask), "");
|
||||
pthread_mutex_lock(&__cxa_lock);
|
||||
__cxa_lock();
|
||||
b = __cxa_blocks.p;
|
||||
if (!b) b = __cxa_blocks.p = &__cxa_blocks.root;
|
||||
if (!~b->mask) {
|
||||
|
@ -60,7 +56,7 @@ noasan int __cxa_atexit(void *fp, void *arg, void *pred) {
|
|||
b2->next = b;
|
||||
__cxa_blocks.p = b = b2;
|
||||
} else {
|
||||
pthread_mutex_unlock(&__cxa_lock);
|
||||
__cxa_unlock();
|
||||
return enomem();
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +66,6 @@ noasan int __cxa_atexit(void *fp, void *arg, void *pred) {
|
|||
b->p[i].fp = fp;
|
||||
b->p[i].arg = arg;
|
||||
b->p[i].pred = pred;
|
||||
pthread_mutex_unlock(&__cxa_lock);
|
||||
__cxa_unlock();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -34,9 +34,12 @@
|
|||
* @param pred can be null to match all
|
||||
*/
|
||||
void __cxa_finalize(void *pred) {
|
||||
void *fp, *arg;
|
||||
unsigned i, mask;
|
||||
struct CxaAtexitBlock *b, *b2;
|
||||
StartOver:
|
||||
__cxa_lock();
|
||||
StartOverLocked:
|
||||
if ((b = __cxa_blocks.p)) {
|
||||
for (;;) {
|
||||
mask = b->mask;
|
||||
|
@ -45,9 +48,11 @@ StartOver:
|
|||
mask &= ~(1u << i);
|
||||
if (!pred || pred == b->p[i].pred) {
|
||||
b->mask &= ~(1u << i);
|
||||
if (b->p[i].fp) {
|
||||
STRACE("__cxa_finalize(%t, %p)", b->p[i].fp, b->p[i].arg);
|
||||
((void (*)(void *))b->p[i].fp)(b->p[i].arg);
|
||||
if ((fp = b->p[i].fp)) {
|
||||
arg = b->p[i].arg;
|
||||
__cxa_unlock();
|
||||
STRACE("__cxa_finalize(%t, %p)", fp, arg);
|
||||
((void (*)(void *))fp)(arg);
|
||||
goto StartOver;
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +66,7 @@ StartOver:
|
|||
}
|
||||
}
|
||||
__cxa_blocks.p = b2;
|
||||
goto StartOver;
|
||||
goto StartOverLocked;
|
||||
} else {
|
||||
if (b->next) {
|
||||
b = b->next;
|
||||
|
@ -71,4 +76,5 @@ StartOver:
|
|||
}
|
||||
}
|
||||
}
|
||||
__cxa_unlock();
|
||||
}
|
||||
|
|
21
libc/intrin/cxalock.c
Normal file
21
libc/intrin/cxalock.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/runtime/cxaatexit.internal.h"
|
||||
|
||||
pthread_mutex_t __cxa_lock_obj;
|
|
@ -42,10 +42,6 @@ $(LIBC_INTRIN_A).pkg: \
|
|||
$(LIBC_INTRIN_A_OBJS) \
|
||||
$(foreach x,$(LIBC_INTRIN_A_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
$(LIBC_INTRIN_A_OBJS): \
|
||||
OVERRIDE_CFLAGS += \
|
||||
-foptimize-sibling-calls
|
||||
|
||||
# we can't use asan and ubsan because:
|
||||
# this is asan and ubsan
|
||||
o/$(MODE)/libc/intrin/asan.o \
|
||||
|
@ -76,20 +72,6 @@ o/$(MODE)/libc/intrin/kprintf.greg.o: \
|
|||
-fno-sanitize=all \
|
||||
-fno-stack-protector
|
||||
|
||||
# we can't use compiler magic because:
|
||||
# spinlocks are called very early in initialization
|
||||
# e.g. __cxa_atexit()
|
||||
o/$(MODE)/libc/intrin/gettid.greg.o \
|
||||
o/$(MODE)/libc/intrin/_trylock_debug_4.o \
|
||||
o/$(MODE)/libc/intrin/_spinlock_debug_4.o: \
|
||||
OVERRIDE_CFLAGS += \
|
||||
-fwrapv \
|
||||
-x-no-pg \
|
||||
-mno-fentry \
|
||||
-ffreestanding \
|
||||
-fno-sanitize=all \
|
||||
-fno-stack-protector
|
||||
|
||||
o/$(MODE)/libc/intrin/tls.greg.o \
|
||||
o/$(MODE)/libc/intrin/exit.greg.o \
|
||||
o/$(MODE)/libc/intrin/exit1.greg.o \
|
||||
|
|
|
@ -55,36 +55,6 @@
|
|||
|
||||
extern hidden struct SymbolTable *__symtab;
|
||||
|
||||
struct Timestamps {
|
||||
unsigned long long birth;
|
||||
unsigned long long start;
|
||||
};
|
||||
|
||||
unsigned long long __kbirth; // see fork-nt.c
|
||||
|
||||
privileged static struct Timestamps kenter(void) {
|
||||
struct Timestamps ts;
|
||||
ts.start = rdtsc();
|
||||
ts.birth = __kbirth;
|
||||
if (!ts.birth) {
|
||||
ts.birth = kStartTsc;
|
||||
if (!ts.birth) ts.birth = 1;
|
||||
_lockcmpxchg(&__kbirth, 0, ts.birth);
|
||||
}
|
||||
return ts;
|
||||
}
|
||||
|
||||
privileged static void kleave(struct Timestamps ts) {
|
||||
uint64_t finish, elapse, adjust;
|
||||
finish = rdtsc();
|
||||
elapse = unsignedsubtract(finish, ts.start);
|
||||
adjust = ts.birth + elapse;
|
||||
if (!adjust) adjust = 1;
|
||||
if (__kbirth == ts.birth) {
|
||||
_lockcmpxchg(&__kbirth, ts.birth, adjust); // ignore overlapping intervals
|
||||
}
|
||||
}
|
||||
|
||||
privileged static inline char *kadvance(char *p, char *e, long n) {
|
||||
intptr_t t = (intptr_t)p;
|
||||
if (__builtin_add_overflow(t, n, &t)) t = (intptr_t)e;
|
||||
|
@ -213,8 +183,8 @@ privileged static void klog(const char *b, size_t n) {
|
|||
}
|
||||
}
|
||||
|
||||
privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
||||
struct Timestamps ts) {
|
||||
privileged static size_t kformat(char *b, size_t n, const char *fmt,
|
||||
va_list va) {
|
||||
int si;
|
||||
wint_t t, u;
|
||||
const char *abet;
|
||||
|
@ -333,7 +303,7 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
|||
continue;
|
||||
|
||||
case 'T':
|
||||
x = ClocksToNanos(ts.start, ts.birth) % 86400000000000;
|
||||
x = ClocksToNanos(rdtsc(), kStartTsc) % 86400000000000;
|
||||
goto FormatUnsigned;
|
||||
|
||||
case 'P':
|
||||
|
@ -766,37 +736,8 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
|
|||
privileged size_t ksnprintf(char *b, size_t n, const char *fmt, ...) {
|
||||
size_t m;
|
||||
va_list v;
|
||||
struct Timestamps t;
|
||||
t = kenter();
|
||||
va_start(v, fmt);
|
||||
m = kformat(b, n, fmt, v, t);
|
||||
va_end(v);
|
||||
kleave(t);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Privileged snprintf() w/o timestamp feature.
|
||||
*
|
||||
* This provides a marginal performance boost, but it means %T can no
|
||||
* longer be used.
|
||||
*
|
||||
* snprintf(".") l: 25𝑐 8𝑛𝑠
|
||||
* kusnprintf(".") l: 22𝑐 7𝑛𝑠
|
||||
* ksnprintf(".") l: 54𝑐 17𝑛𝑠
|
||||
*
|
||||
* @param b is buffer, and guaranteed a NUL-terminator if `n>0`
|
||||
* @param n is number of bytes available in buffer
|
||||
* @return length of output excluding NUL, which may exceed `n`
|
||||
* @see kprintf() for documentation
|
||||
* @asyncsignalsafe
|
||||
* @vforksafe
|
||||
*/
|
||||
privileged size_t kusnprintf(char *b, size_t n, const char *fmt, ...) {
|
||||
size_t m;
|
||||
va_list v;
|
||||
va_start(v, fmt);
|
||||
m = kformat(b, n, fmt, v, (struct Timestamps){0});
|
||||
m = kformat(b, n, fmt, v);
|
||||
va_end(v);
|
||||
return m;
|
||||
}
|
||||
|
@ -812,12 +753,7 @@ privileged size_t kusnprintf(char *b, size_t n, const char *fmt, ...) {
|
|||
* @vforksafe
|
||||
*/
|
||||
privileged size_t kvsnprintf(char *b, size_t n, const char *fmt, va_list v) {
|
||||
size_t m;
|
||||
struct Timestamps t;
|
||||
t = kenter();
|
||||
m = kformat(b, n, fmt, v, t);
|
||||
kleave(t);
|
||||
return m;
|
||||
return kformat(b, n, fmt, v);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -830,12 +766,9 @@ privileged size_t kvsnprintf(char *b, size_t n, const char *fmt, va_list v) {
|
|||
privileged void kvprintf(const char *fmt, va_list v) {
|
||||
size_t n;
|
||||
char b[4000];
|
||||
struct Timestamps t;
|
||||
if (!v) return;
|
||||
t = kenter();
|
||||
n = kformat(b, sizeof(b), fmt, v, t);
|
||||
n = kformat(b, sizeof(b), fmt, v);
|
||||
klog(b, MIN(n, sizeof(b) - 1));
|
||||
kleave(t);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,11 +3,8 @@
|
|||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
extern unsigned long long __kbirth;
|
||||
|
||||
void kprintf(const char *, ...);
|
||||
size_t ksnprintf(char *, size_t, const char *, ...);
|
||||
size_t kusnprintf(char *, size_t, const char *, ...);
|
||||
void kvprintf(const char *, va_list);
|
||||
size_t kvsnprintf(char *, size_t, const char *, va_list);
|
||||
bool kisdangerous(const void *);
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
/**
|
||||
* Acquires mutex.
|
||||
*/
|
||||
int pthread_mutex_lock(pthread_mutex_t *mutex) {
|
||||
noasan noubsan int pthread_mutex_lock(pthread_mutex_t *mutex) {
|
||||
int me, owner;
|
||||
unsigned tries;
|
||||
if (__threaded) {
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
/**
|
||||
* Releases mutex.
|
||||
*/
|
||||
int pthread_mutex_unlock(pthread_mutex_t *mutex) {
|
||||
noasan noubsan int pthread_mutex_unlock(pthread_mutex_t *mutex) {
|
||||
int owner;
|
||||
bool shouldunlock;
|
||||
assert(mutex->reent > 0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue