From 17c5842c8615fec8b36ea35ed204da2aba27d4ec Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Wed, 23 Nov 2022 19:03:39 -0800 Subject: [PATCH] Add mutex support to SQLite (reapply 05197afc) --- third_party/sqlite3/fts3_write.c | 1 + third_party/sqlite3/mutex.internal.h | 41 ++++++++++++++++++++++++++++ third_party/sqlite3/pcache1.c | 3 ++ third_party/sqlite3/sqlite3.h | 2 -- 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 third_party/sqlite3/mutex.internal.h diff --git a/third_party/sqlite3/fts3_write.c b/third_party/sqlite3/fts3_write.c index 1b6b371cd..42cb96624 100644 --- a/third_party/sqlite3/fts3_write.c +++ b/third_party/sqlite3/fts3_write.c @@ -21,6 +21,7 @@ #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) #include "libc/assert.h" +#include "libc/fmt/conv.h" #include "libc/mem/alg.h" #include "libc/mem/mem.h" #include "libc/stdio/stdio.h" diff --git a/third_party/sqlite3/mutex.internal.h b/third_party/sqlite3/mutex.internal.h new file mode 100644 index 000000000..08a731d5c --- /dev/null +++ b/third_party/sqlite3/mutex.internal.h @@ -0,0 +1,41 @@ +#ifndef COSMOPOLITAN_THIRD_PARTY_SQLITE3_MUTEX_INTERNAL_H_ +#define COSMOPOLITAN_THIRD_PARTY_SQLITE3_MUTEX_INTERNAL_H_ +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ + +#if !SQLITE_THREADSAFE +#define SQLITE_MUTEX_OMIT +#endif +#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP) +#if SQLITE_OS_UNIX +#define SQLITE_MUTEX_PTHREADS +#elif SQLITE_OS_WIN +#define SQLITE_MUTEX_W32 +#else +#define SQLITE_MUTEX_NOOP +#endif +#endif + +#ifdef SQLITE_MUTEX_OMIT +/* +** If this is a no-op implementation, implement everything as macros. +*/ +#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) +#define sqlite3_mutex_free(X) +#define sqlite3_mutex_enter(X) +#define sqlite3_mutex_try(X) SQLITE_OK +#define sqlite3_mutex_leave(X) +#define sqlite3_mutex_held(X) ((void)(X), 1) +#define sqlite3_mutex_notheld(X) ((void)(X), 1) +#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8) +#define sqlite3MutexInit() SQLITE_OK +#define sqlite3MutexEnd() +#define MUTEX_LOGIC(X) +#else +#define MUTEX_LOGIC(X) X +int sqlite3_mutex_held(sqlite3_mutex*); +#endif /* defined(SQLITE_MUTEX_OMIT) */ + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* COSMOPOLITAN_THIRD_PARTY_SQLITE3_MUTEX_INTERNAL_H_ */ diff --git a/third_party/sqlite3/pcache1.c b/third_party/sqlite3/pcache1.c index e99cf1baa..0c9d308d7 100644 --- a/third_party/sqlite3/pcache1.c +++ b/third_party/sqlite3/pcache1.c @@ -81,7 +81,10 @@ ** show that method (3) with N==100 provides about a 5% performance boost for ** common workloads. */ +#include "libc/assert.h" +#include "third_party/sqlite3/sqlite3.h" #include "third_party/sqlite3/sqliteInt.h" +/* clang-format off */ typedef struct PCache1 PCache1; typedef struct PgHdr1 PgHdr1; diff --git a/third_party/sqlite3/sqlite3.h b/third_party/sqlite3/sqlite3.h index c8f8503b9..1a4d313b4 100644 --- a/third_party/sqlite3/sqlite3.h +++ b/third_party/sqlite3/sqlite3.h @@ -7916,10 +7916,8 @@ struct sqlite3_mutex_methods { ** the appropriate thing to do. The sqlite3_mutex_notheld() ** interface should also return 1 when given a NULL pointer. */ -#ifndef NDEBUG SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); -#endif /* ** CAPI3REF: Mutex Types