Allow the DB writer to be used from a worker thread. Not well tested, just still trying to charge forward on moving all blocking work off the main thread.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4325 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-06-15 00:27:49 +00:00
parent 51b317233a
commit 7d562ce85c
11 changed files with 163 additions and 92 deletions

View File

@ -180,9 +180,10 @@ typedef struct _tf_ssb_t
tf_trace_t* trace;
const char* db_path;
sqlite3* db;
uv_mutex_t db_readers_lock;
uv_mutex_t db_writer_lock;
sqlite3* db_writer;
sqlite3** db_readers;
int db_readers_count;
@ -2099,6 +2100,7 @@ tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, const char* db_path
}
uv_mutex_init(&ssb->db_readers_lock);
uv_mutex_init(&ssb->db_writer_lock);
JS_NewClassID(&_connection_class_id);
JSClassDef def =
@ -2109,7 +2111,7 @@ tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, const char* db_path
JS_NewClass(JS_GetRuntime(ssb->context), _connection_class_id, &def);
ssb->db_path = tf_strdup(db_path);
sqlite3_open(db_path, &ssb->db);
sqlite3_open_v2(db_path, &ssb->db_writer, SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI, NULL);
tf_ssb_db_init(ssb);
if (loop)
@ -2142,11 +2144,6 @@ tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, const char* db_path
return ssb;
}
sqlite3* tf_ssb_get_db(tf_ssb_t* ssb)
{
return ssb->db;
}
sqlite3* tf_ssb_acquire_db_reader(tf_ssb_t* ssb)
{
sqlite3* db = NULL;
@ -2157,7 +2154,7 @@ sqlite3* tf_ssb_acquire_db_reader(tf_ssb_t* ssb)
}
else
{
sqlite3_open_v2(ssb->db_path, &db, SQLITE_OPEN_READONLY, NULL);
sqlite3_open_v2(ssb->db_path, &db, SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, NULL);
tf_ssb_db_init_reader(db);
tf_trace_sqlite(ssb->trace, db);
}
@ -2173,6 +2170,22 @@ void tf_ssb_release_db_reader(tf_ssb_t* ssb, sqlite3* db)
uv_mutex_unlock(&ssb->db_readers_lock);
}
sqlite3* tf_ssb_acquire_db_writer(tf_ssb_t* ssb)
{
uv_mutex_lock(&ssb->db_writer_lock);
sqlite3* writer = ssb->db_writer;
assert(writer);
ssb->db_writer = NULL;
return writer;
}
void tf_ssb_release_db_writer(tf_ssb_t* ssb, sqlite3* db)
{
assert(ssb->db_writer == NULL);
ssb->db_writer = db;
uv_mutex_unlock(&ssb->db_writer_lock);
}
uv_loop_t* tf_ssb_get_loop(tf_ssb_t* ssb)
{
return ssb->loop;
@ -2205,10 +2218,12 @@ void tf_ssb_get_private_key(tf_ssb_t* ssb, uint8_t* out_private, size_t private_
void tf_ssb_set_trace(tf_ssb_t* ssb, tf_trace_t* trace)
{
ssb->trace = trace;
if (trace && ssb->db)
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
if (trace && db)
{
tf_trace_sqlite(trace, ssb->db);
tf_trace_sqlite(trace, db);
}
tf_ssb_release_db_writer(ssb, db);
}
tf_trace_t* tf_ssb_get_trace(tf_ssb_t* ssb)
@ -2336,7 +2351,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
JS_FreeContext(ssb->context);
JS_FreeRuntime(ssb->runtime);
}
sqlite3_close(ssb->db);
sqlite3_close(ssb->db_writer);
while (ssb->broadcasts)
{
tf_ssb_broadcast_t* broadcast = ssb->broadcasts;
@ -2357,6 +2372,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
}
tf_free(ssb->db_readers);
uv_mutex_destroy(&ssb->db_readers_lock);
uv_mutex_destroy(&ssb->db_writer_lock);
tf_free((void*)ssb->db_path);
tf_free(ssb);
}