Skip to content

Commit 5bec78e

Browse files
committed
sqlite: set threading mode to multithreaded
1 parent ddec6fb commit 5bec78e

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

deps/sqlite/sqlite.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
'SQLITE_ENABLE_RBU',
2626
'SQLITE_ENABLE_RTREE',
2727
'SQLITE_ENABLE_SESSION',
28+
'SQLITE_THREADSAFE=2',
2829
],
2930
'include_dirs': ['.'],
3031
'sources': [

deps/sqlite/unofficial.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ template("sqlite_gn_build") {
1919
"SQLITE_ENABLE_RBU",
2020
"SQLITE_ENABLE_RTREE",
2121
"SQLITE_ENABLE_SESSION",
22+
"SQLITE_THREADSAFE=2",
2223
]
2324
}
2425

src/node_sqlite.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ class SQLiteAsyncTask : public ThreadPoolWork {
474474
after_(result_, resolver);
475475
Finalize();
476476
}
477+
db_->ProcessNextAsyncTask();
477478
}
478479

479480
void Finalize() { db_->RemoveAsyncTask(this); }
@@ -745,6 +746,25 @@ void Database::RemoveAsyncTask(ThreadPoolWork* async_task) {
745746
async_tasks_.erase(async_task);
746747
}
747748

749+
void Database::ScheduleAsyncTask(ThreadPoolWork* work) {
750+
if (has_running_task_) {
751+
task_queue_.push(work);
752+
} else {
753+
has_running_task_ = true;
754+
work->ScheduleWork();
755+
}
756+
}
757+
758+
void Database::ProcessNextAsyncTask() {
759+
if (task_queue_.empty()) {
760+
has_running_task_ = false;
761+
} else {
762+
ThreadPoolWork* next_work = task_queue_.front();
763+
task_queue_.pop();
764+
next_work->ScheduleWork();
765+
}
766+
}
767+
748768
void Database::DeleteSessions() {
749769
// all attached sessions need to be deleted before the database is closed
750770
// https://www.sqlite.org/session/sqlite3session_create.html
@@ -1234,8 +1254,8 @@ Local<Promise::Resolver> MakeSQLiteAsyncWork(
12341254
}
12351255

12361256
auto* work = new SQLiteAsyncTask<T>(env, db, resolver, task, after);
1237-
work->ScheduleWork();
12381257
db->AddAsyncTask(work);
1258+
db->ScheduleAsyncTask(work);
12391259
return resolver;
12401260
}
12411261

src/node_sqlite.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <list>
1414
#include <map>
15+
#include <queue>
1516
#include <unordered_set>
1617

1718
namespace node {
@@ -194,6 +195,8 @@ class Database : public BaseObject {
194195
void AddBackup(BackupJob* backup);
195196
void AddAsyncTask(ThreadPoolWork* async_task);
196197
void RemoveAsyncTask(ThreadPoolWork* async_task);
198+
void ScheduleAsyncTask(ThreadPoolWork* async_task);
199+
void ProcessNextAsyncTask();
197200
void FinalizeBackups();
198201
void UntrackStatement(Statement* statement);
199202
bool IsOpen();
@@ -230,6 +233,8 @@ class Database : public BaseObject {
230233
bool ignore_next_sqlite_error_;
231234

232235
std::set<ThreadPoolWork*> async_tasks_;
236+
std::queue<ThreadPoolWork*> task_queue_;
237+
bool has_running_task_ = false;
233238
std::set<BackupJob*> backups_;
234239
std::set<sqlite3_session*> sessions_;
235240
std::unordered_set<Statement*> statements_;

0 commit comments

Comments
 (0)