mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-18 18:42:28 +00:00
Improve multithreading
This commit is contained in:
parent
d3167126aa
commit
30afd6ddbb
38 changed files with 752 additions and 174 deletions
|
@ -27,6 +27,7 @@ const char *DescribeItimer(char[12], int) libcesque;
|
|||
const char *DescribeMapFlags(char[64], int) libcesque;
|
||||
const char *DescribeMapping(char[8], int, int) libcesque;
|
||||
const char *DescribeMremapFlags(char[30], int) libcesque;
|
||||
const char *DescribeMsyncFlags(char[48], int) libcesque;
|
||||
const char *DescribeNtConsoleInFlags(char[256], uint32_t) libcesque;
|
||||
const char *DescribeNtConsoleOutFlags(char[128], uint32_t) libcesque;
|
||||
const char *DescribeNtCreationDisposition(uint32_t) libcesque;
|
||||
|
@ -54,6 +55,7 @@ const char *DescribeRlimitName(char[20], int) libcesque;
|
|||
const char *DescribeSchedPolicy(char[48], int) libcesque;
|
||||
const char *DescribeSeccompOperation(int) libcesque;
|
||||
const char *DescribeSiCode(char[20], int, int) libcesque;
|
||||
const char *DescribeSigaltstackFlags(char[22], int) libcesque;
|
||||
const char *DescribeSleepFlags(char[16], int) libcesque;
|
||||
const char *DescribeSockLevel(char[12], int) libcesque;
|
||||
const char *DescribeSockOptname(char[32], int, int) libcesque;
|
||||
|
@ -82,6 +84,7 @@ const char *DescribeWhichPrio(char[12], int) libcesque;
|
|||
#define DescribeMapFlags(x) DescribeMapFlags(alloca(64), x)
|
||||
#define DescribeMapping(x, y) DescribeMapping(alloca(8), x, y)
|
||||
#define DescribeMremapFlags(x) DescribeMremapFlags(alloca(30), x)
|
||||
#define DescribeMsyncFlags(x) DescribeMsyncFlags(alloca(48), x)
|
||||
#define DescribeNtConsoleInFlags(x) DescribeNtConsoleInFlags(alloca(256), x)
|
||||
#define DescribeNtConsoleOutFlags(x) DescribeNtConsoleOutFlags(alloca(128), x)
|
||||
#define DescribeNtFileAccessFlags(x) DescribeNtFileAccessFlags(alloca(512), x)
|
||||
|
@ -107,6 +110,7 @@ const char *DescribeWhichPrio(char[12], int) libcesque;
|
|||
#define DescribeRlimitName(rl) DescribeRlimitName(alloca(20), rl)
|
||||
#define DescribeSchedPolicy(x) DescribeSchedPolicy(alloca(48), x)
|
||||
#define DescribeSiCode(x, y) DescribeSiCode(alloca(20), x, y)
|
||||
#define DescribeSigaltstackFlags(x) DescribeSigaltstackFlags(alloca(22), x)
|
||||
#define DescribeSleepFlags(x) DescribeSleepFlags(alloca(16), x)
|
||||
#define DescribeSockLevel(x) DescribeSockLevel(alloca(12), x)
|
||||
#define DescribeSockOptname(x, y) DescribeSockOptname(alloca(32), x, y)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
||||
│ 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 │
|
||||
|
@ -16,20 +16,15 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/intrin/describeflags.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/sysv/consts/msync.h"
|
||||
|
||||
static bool _pthread_deref(struct PosixThread *pt) {
|
||||
int refs = atomic_load_explicit(&pt->pt_refs, memory_order_acquire);
|
||||
return !refs || !atomic_fetch_sub(&pt->pt_refs, 1);
|
||||
}
|
||||
|
||||
void _pthread_unref(struct PosixThread *pt) {
|
||||
if (_pthread_deref(pt)) {
|
||||
unassert(_weaken(_pthread_free));
|
||||
_weaken(_pthread_free)(pt, false);
|
||||
_weaken(_pthread_decimate)();
|
||||
}
|
||||
const char *(DescribeMsyncFlags)(char buf[48], int x) {
|
||||
const struct DescribeFlags kMsyncFlags[] = {
|
||||
{MS_SYNC, "SYNC"}, //
|
||||
{MS_ASYNC, "ASYNC"}, //
|
||||
{MS_INVALIDATE, "INVALIDATE"}, //
|
||||
};
|
||||
return DescribeFlags(buf, 48, kMsyncFlags, ARRAYLEN(kMsyncFlags), "MS_", x);
|
||||
}
|
|
@ -21,8 +21,8 @@
|
|||
#include "libc/intrin/describeflags.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
|
||||
const char *(DescribeSigaltstk)(char buf[128], int rc,
|
||||
const struct sigaltstack *ss) {
|
||||
const char *(DescribeSigaltstack)(char buf[128], int rc,
|
||||
const struct sigaltstack *ss) {
|
||||
if (rc == -1)
|
||||
return "n/a";
|
||||
if (!ss)
|
||||
|
@ -30,8 +30,8 @@ const char *(DescribeSigaltstk)(char buf[128], int rc,
|
|||
if (kisdangerous(ss)) {
|
||||
ksnprintf(buf, 128, "%p", ss);
|
||||
} else {
|
||||
ksnprintf(buf, 128, "{.ss_sp=%p, .ss_flags=%#lx, .ss_size=%'zu}", ss->ss_sp,
|
||||
ss->ss_flags, ss->ss_size);
|
||||
ksnprintf(buf, 128, "{.ss_sp=%p, .ss_flags=%s, .ss_size=%'zu}", ss->ss_sp,
|
||||
DescribeSigaltstackFlags(ss->ss_flags), ss->ss_size);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
|
30
libc/intrin/describesigaltstackflags.c
Normal file
30
libc/intrin/describesigaltstackflags.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et 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/intrin/describeflags.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/sysv/consts/ss.h"
|
||||
|
||||
const char *(DescribeSigaltstackFlags)(char buf[22], int x) {
|
||||
const struct DescribeFlags kSigaltstackFlags[] = {
|
||||
{SS_ONSTACK, "ONSTACK"}, //
|
||||
{SS_DISABLE, "DISABLE"}, //
|
||||
};
|
||||
return DescribeFlags(buf, 48, kSigaltstackFlags, ARRAYLEN(kSigaltstackFlags),
|
||||
"SS_", x);
|
||||
}
|
|
@ -91,12 +91,21 @@ privileged bool __maps_lock(void) {
|
|||
tib = __get_tls_privileged();
|
||||
if (atomic_fetch_add_explicit(&tib->tib_relock_maps, 1, memory_order_relaxed))
|
||||
return true;
|
||||
int backoff = 0;
|
||||
while (atomic_exchange_explicit(&__maps.lock, 1, memory_order_acquire)) {
|
||||
if (backoff < 7) {
|
||||
volatile int i;
|
||||
for (i = 0; i != 1 << backoff; i++) {
|
||||
}
|
||||
backoff++;
|
||||
} else {
|
||||
// STRACE("pthread_delay_np(__maps)");
|
||||
#if defined(__GNUC__) && defined(__aarch64__)
|
||||
__asm__ volatile("yield");
|
||||
__asm__ volatile("yield");
|
||||
#elif defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
|
||||
__asm__ volatile("pause");
|
||||
__asm__ volatile("pause");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -834,7 +834,8 @@ void *mremap(void *old_addr, size_t old_size, size_t new_size, int flags, ...) {
|
|||
*/
|
||||
int munmap(void *addr, size_t size) {
|
||||
int rc = __munmap(addr, size);
|
||||
STRACE("munmap(%p, %'zu) → %d% m", addr, size, rc);
|
||||
STRACE("munmap(%p, %'zu) → %d% m (%'zu bytes total)", addr, size, rc,
|
||||
__maps.pages * __pagesize);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "libc/calls/syscall-nt.internal.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/describeflags.h"
|
||||
#include "libc/intrin/strace.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
@ -93,6 +94,7 @@ int msync(void *addr, size_t size, int flags) {
|
|||
END_CANCELATION_POINT;
|
||||
|
||||
Finished:
|
||||
STRACE("msync(%p, %'zu, %#x) → %d% m", addr, size, flags, rc);
|
||||
STRACE("msync(%p, %'zu, %s) → %d% m", addr, size, DescribeMsyncFlags(flags),
|
||||
rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue