mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Implement swapcontext() and makecontext()
This change introduces support for Linux-style uc_context manipulation that's fast and works well on all supported OSes and architectures. It also integrates with the Cosmpolitan runtime which can show backtraces comprised of multiple stacks and fibers. See the test and example code for further details. This will be used by Mold once it's been vendored
This commit is contained in:
parent
7ec84655b4
commit
197aa0d465
28 changed files with 617 additions and 355 deletions
|
@ -83,3 +83,19 @@ void SetGetContext(void) {
|
|||
BENCH(getcontext, bench) {
|
||||
EZBENCH2("get/setcontext", donothing, SetGetContext());
|
||||
}
|
||||
|
||||
BENCH(swapcontext, bench) {
|
||||
ucontext_t main, loop;
|
||||
volatile bool ready = false;
|
||||
getcontext(&main);
|
||||
if (ready) {
|
||||
for (;;) {
|
||||
swapcontext(&main, &loop);
|
||||
// kprintf("boom\n");
|
||||
}
|
||||
} else {
|
||||
ready = true;
|
||||
EZBENCH2("x2 swapcontext", donothing, swapcontext(&loop, &main));
|
||||
// kprintf("dollar\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ void TestContendedLock(const char *name, int kind) {
|
|||
pthread_mutexattr_destroy(&attr);
|
||||
atomic_store(&ready, 0);
|
||||
atomic_store(&success, 0);
|
||||
stk = _mapstack();
|
||||
stk = NewCosmoStack();
|
||||
rc = clone(Worker, stk, GetStackSize() - 16 /* openbsd:stackbound */,
|
||||
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
|
||||
CLONE_SYSVSEM | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
|
||||
|
@ -146,7 +146,7 @@ void TestContendedLock(const char *name, int kind) {
|
|||
while (tib.tib_tid) donothing;
|
||||
ASSERT_EQ(1, atomic_load(&success));
|
||||
ASSERT_EQ(0, atomic_load(&counter));
|
||||
_freestack(stk);
|
||||
FreeCosmoStack(stk);
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
||||
ns = time2dbl(timespec_sub(t2, t1)) / n;
|
||||
kprintf("%s contended took %s\n", name, time2str(ns));
|
||||
|
|
68
test/libc/thread/makecontext_test.c
Normal file
68
test/libc/thread/makecontext_test.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*-*- 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 2023 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/calls/calls.h"
|
||||
#include "libc/calls/ucontext.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/mem/gc.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/subprocess.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "third_party/libcxx/math.h"
|
||||
|
||||
ucontext_t uc;
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
void itsatrap(int x, int y) {
|
||||
*(int *)(intptr_t)x = scalbn(x, y);
|
||||
}
|
||||
|
||||
TEST(makecontext, test) {
|
||||
SPAWN(fork);
|
||||
getcontext(&uc);
|
||||
uc.uc_link = 0;
|
||||
uc.uc_stack.ss_sp = NewCosmoStack();
|
||||
uc.uc_stack.ss_size = GetStackSize();
|
||||
makecontext(&uc, exit, 1, 42);
|
||||
setcontext(&uc);
|
||||
EXITS(42);
|
||||
}
|
||||
|
||||
TEST(makecontext, backtrace) {
|
||||
SPAWN(fork);
|
||||
ASSERT_SYS(0, 0, close(2));
|
||||
ASSERT_SYS(0, 2, creat("log", 0644));
|
||||
getcontext(&uc);
|
||||
uc.uc_link = 0;
|
||||
uc.uc_stack.ss_sp = NewCosmoStack();
|
||||
uc.uc_stack.ss_size = GetStackSize();
|
||||
makecontext(&uc, itsatrap, 2, 123, 456);
|
||||
setcontext(&uc);
|
||||
EXITS(128 + SIGSEGV);
|
||||
if (!GetSymbolTable()) return;
|
||||
char *log = gc(xslurp("log", 0));
|
||||
EXPECT_NE(0, strstr(log, "itsatrap"));
|
||||
EXPECT_NE(0, strstr(log, "runcontext"));
|
||||
EXPECT_NE(0, strstr(log, "makecontext_backtrace"));
|
||||
}
|
|
@ -35,6 +35,7 @@ TEST_LIBC_THREAD_DIRECTDEPS = \
|
|||
LIBC_TESTLIB \
|
||||
LIBC_THREAD \
|
||||
LIBC_TIME \
|
||||
LIBC_X \
|
||||
THIRD_PARTY_NSYNC \
|
||||
THIRD_PARTY_NSYNC_MEM
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue