mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 22:02:27 +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
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue