mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-30 08:18:30 +00:00
Do some work on TurfWar
This commit is contained in:
parent
bc353f454b
commit
05197afca2
12 changed files with 499 additions and 181 deletions
|
@ -0,0 +1,57 @@
|
|||
/*-*- 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/calls/calls.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
_Thread_local intptr_t gotsig;
|
||||
|
||||
void OnSig(int sig) {
|
||||
gotsig = sig;
|
||||
}
|
||||
|
||||
void *Worker(void *arg) {
|
||||
sigset_t ss;
|
||||
sigemptyset(&ss);
|
||||
ASSERT_SYS(EINTR, -1, sigsuspend(&ss));
|
||||
return (void *)gotsig;
|
||||
}
|
||||
|
||||
TEST(tkill, test) {
|
||||
if (IsWindows()) return; // TODO(jart): fix me
|
||||
int i;
|
||||
void *res;
|
||||
pthread_t t;
|
||||
sigset_t ss, oldss;
|
||||
sighandler_t oldsig;
|
||||
sigemptyset(&ss);
|
||||
sigaddset(&ss, SIGUSR1);
|
||||
oldsig = signal(SIGUSR1, OnSig);
|
||||
ASSERT_SYS(0, 0, sigprocmask(SIG_BLOCK, &ss, &oldss));
|
||||
ASSERT_EQ(0, pthread_create(&t, 0, Worker, 0));
|
||||
ASSERT_SYS(0, 0, tkill(pthread_getunique_np(t), SIGUSR1));
|
||||
ASSERT_EQ(0, pthread_join(t, &res));
|
||||
ASSERT_EQ(SIGUSR1, (intptr_t)res);
|
||||
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, &oldss, 0));
|
||||
signal(SIGUSR1, oldsig);
|
||||
}
|
|
@ -16,27 +16,120 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "third_party/sqlite3/sqlite3.h"
|
||||
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
void SetUp(void) {
|
||||
void SetUpOnce(void) {
|
||||
if (IsWindows()) exit(0);
|
||||
sqlite3_initialize();
|
||||
}
|
||||
|
||||
// if we try to open a WAL database at the same time from multiple
|
||||
// threads then it's likely we'll get a SQLITE_BUSY conflict since
|
||||
// WAL mode does a complicated dance to initialize itself thus all
|
||||
// we need to do is wait a little bit, and use exponential backoff
|
||||
int DbOpen(const char *path, sqlite3 **db) {
|
||||
int i, rc;
|
||||
rc = sqlite3_open(path, db);
|
||||
if (rc != SQLITE_OK) return rc;
|
||||
for (i = 0; i < 12; ++i) {
|
||||
rc = sqlite3_exec(*db, "PRAGMA journal_mode=WAL", 0, 0, 0);
|
||||
if (rc == SQLITE_OK) break;
|
||||
if (rc != SQLITE_BUSY) return rc;
|
||||
usleep(1000L << i);
|
||||
}
|
||||
return sqlite3_exec(*db, "PRAGMA synchronous=NORMAL", 0, 0, 0);
|
||||
}
|
||||
|
||||
int DbStep(sqlite3_stmt *stmt) {
|
||||
int i, rc;
|
||||
for (i = 0; i < 12; ++i) {
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc == SQLITE_ROW) break;
|
||||
if (rc == SQLITE_DONE) break;
|
||||
if (rc != SQLITE_BUSY) return rc;
|
||||
usleep(1000L << i);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int DbExec(sqlite3 *db, const char *sql) {
|
||||
int i, rc;
|
||||
for (i = 0; i < 12; ++i) {
|
||||
rc = sqlite3_exec(db, sql, 0, 0, 0);
|
||||
if (rc == SQLITE_OK) break;
|
||||
if (rc != SQLITE_BUSY) return rc;
|
||||
usleep(1000L << i);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int DbPrepare(sqlite3 *db, sqlite3_stmt **stmt, const char *sql) {
|
||||
return sqlite3_prepare_v2(db, sql, -1, stmt, 0);
|
||||
}
|
||||
|
||||
void *Worker(void *arg) {
|
||||
int rc;
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt *stmt[2];
|
||||
static const char *const kNames[] = {"alice", "bob", "varu", "vader"};
|
||||
ASSERT_EQ(SQLITE_OK, DbOpen("foo.sqlite", &db));
|
||||
ASSERT_EQ(SQLITE_OK, DbPrepare(db, &stmt[0],
|
||||
"INSERT INTO t (id, name) VALUES (?1, ?2)"));
|
||||
ASSERT_EQ(SQLITE_OK, DbPrepare(db, &stmt[1],
|
||||
"SELECT name, COUNT(*) FROM t GROUP BY name"));
|
||||
for (int j = 0; j < 50; ++j) {
|
||||
ASSERT_EQ(SQLITE_OK, DbExec(db, "BEGIN TRANSACTION"));
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_bind_int64(stmt[0], 1, rand64()));
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_bind_text(
|
||||
stmt[0], 2, kNames[rand64() % ARRAYLEN(kNames)],
|
||||
-1, SQLITE_TRANSIENT));
|
||||
ASSERT_EQ(SQLITE_DONE, DbStep(stmt[0]));
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_reset(stmt[0]));
|
||||
}
|
||||
ASSERT_EQ(SQLITE_OK, DbExec(db, "COMMIT TRANSACTION"));
|
||||
ASSERT_EQ(SQLITE_OK, DbExec(db, "BEGIN TRANSACTION"));
|
||||
for (;;) {
|
||||
rc = DbStep(stmt[1]);
|
||||
if (rc == SQLITE_DONE) break;
|
||||
ASSERT_EQ(SQLITE_ROW, rc);
|
||||
}
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_reset(stmt[1]));
|
||||
ASSERT_EQ(SQLITE_OK, DbExec(db, "END TRANSACTION"));
|
||||
}
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_finalize(stmt[1]));
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_finalize(stmt[0]));
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_close(db));
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(sqlite, test) {
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt *stmt;
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_open("foo.sqlite", &db));
|
||||
ASSERT_EQ(SQLITE_OK,
|
||||
sqlite3_prepare_v2(db, "PRAGMA synchronous=0", -1, &stmt, 0));
|
||||
ASSERT_EQ(SQLITE_DONE, sqlite3_step(stmt));
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_finalize(stmt));
|
||||
ASSERT_EQ(SQLITE_OK,
|
||||
sqlite3_prepare_v2(db, "CREATE TABLE t (x INTEGER)", -1, &stmt, 0));
|
||||
ASSERT_EQ(SQLITE_DONE, sqlite3_step(stmt));
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_finalize(stmt));
|
||||
ASSERT_EQ(SQLITE_OK, DbOpen("foo.sqlite", &db));
|
||||
ASSERT_EQ(SQLITE_OK, DbExec(db, "PRAGMA synchronous=0"));
|
||||
ASSERT_EQ(SQLITE_OK, DbExec(db, "CREATE TABLE t (\n"
|
||||
" id INTEGER,\n"
|
||||
" name TEXT\n"
|
||||
")"));
|
||||
ASSERT_EQ(SQLITE_OK, sqlite3_close(db));
|
||||
int i, n = 4;
|
||||
pthread_t *t = _gc(malloc(sizeof(pthread_t) * n));
|
||||
for (i = 0; i < n; ++i) {
|
||||
ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0));
|
||||
}
|
||||
for (i = 0; i < n; ++i) {
|
||||
ASSERT_EQ(0, pthread_join(t[i], 0));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,10 +42,11 @@ TEST_TOOL_NET_DIRECTDEPS = \
|
|||
LIBC_STUBS \
|
||||
LIBC_SYSV \
|
||||
LIBC_TESTLIB \
|
||||
LIBC_THREAD \
|
||||
LIBC_X \
|
||||
LIBC_ZIPOS \
|
||||
THIRD_PARTY_REGEX \
|
||||
THIRD_PARTY_MBEDTLS \
|
||||
THIRD_PARTY_REGEX \
|
||||
THIRD_PARTY_SQLITE3
|
||||
|
||||
TEST_TOOL_NET_DEPS := \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue