Skip to content

Commit 36ec8cf

Browse files
authored
Merge pull request #1007 from rpetrich/experimental-worker
Add support for running inside a node 10.5 worker_thread
2 parents 5278b2b + f91b4fe commit 36ec8cf

File tree

8 files changed

+38
-15
lines changed

8 files changed

+38
-15
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"prepublishOnly": "npm ls",
5151
"install": "node-pre-gyp install --fallback-to-build",
5252
"pretest": "node test/support/createdb.js",
53-
"test": "mocha -R spec --timeout 480000"
53+
"test": "mocha -R spec --timeout 480000",
54+
"test:worker": "node --experimental-worker scripts/mocha-as-worker.js -R spec --timeout 480000"
5455
},
5556
"license": "BSD-3-Clause",
5657
"keywords": [

scripts/mocha-as-worker.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Run the mocha tests in a worker
2+
// Not a clean approach, but is sufficient to verify correctness
3+
const worker_threads = require("worker_threads");
4+
const path = require("path");
5+
6+
if (worker_threads.isMainThread) {
7+
const worker = new worker_threads.Worker(__filename, { workerData: { windowSize: process.stdout.getWindowSize() } });
8+
worker.on("error", console.error);
9+
} else {
10+
process.stdout.getWindowSize = () => worker_threads.workerData.windowSize;
11+
const mochaPath = path.resolve(require.resolve("mocha"), "../bin/_mocha");
12+
require(mochaPath);
13+
}

src/async.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ template <class Item, class Parent> class Async {
2222
Parent* parent;
2323

2424
public:
25-
Async(Parent* parent_, Callback cb_)
25+
Async(uv_loop_t* loop_, Parent* parent_, Callback cb_)
2626
: callback(cb_), parent(parent_) {
2727
watcher.data = this;
2828
NODE_SQLITE3_MUTEX_INIT
29-
uv_async_init(uv_default_loop(), &watcher, reinterpret_cast<uv_async_cb>(listener));
29+
uv_async_init(loop_, &watcher, reinterpret_cast<uv_async_cb>(listener));
3030
}
3131

3232
static void listener(uv_async_t* handle, int status) {

src/database.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ NAN_METHOD(Database::New) {
127127
callback = Local<Function>::Cast(info[pos++]);
128128
}
129129

130-
Database* db = new Database();
130+
#if NODE_MODULE_VERSION > NODE_9_0_MODULE_VERSION
131+
uv_loop_t* loop = node::GetCurrentEventLoop(info.GetIsolate());
132+
#else
133+
uv_loop_t* loop = uv_default_loop();
134+
#endif
135+
Database* db = new Database(loop);
131136
db->Wrap(info.This());
132137

133138
Nan::ForceSet(info.This(), Nan::New("filename").ToLocalChecked(), info[0].As<String>(), ReadOnly);
@@ -141,7 +146,7 @@ NAN_METHOD(Database::New) {
141146
}
142147

143148
void Database::Work_BeginOpen(Baton* baton) {
144-
int status = uv_queue_work(uv_default_loop(),
149+
int status = uv_queue_work(baton->db->loop,
145150
&baton->request, Work_Open, (uv_after_work_cb)Work_AfterOpen);
146151
assert(status == 0);
147152
}
@@ -227,7 +232,7 @@ void Database::Work_BeginClose(Baton* baton) {
227232
baton->db->RemoveCallbacks();
228233
baton->db->closing = true;
229234

230-
int status = uv_queue_work(uv_default_loop(),
235+
int status = uv_queue_work(baton->db->loop,
231236
&baton->request, Work_Close, (uv_after_work_cb)Work_AfterClose);
232237
assert(status == 0);
233238
}
@@ -388,7 +393,7 @@ void Database::RegisterTraceCallback(Baton* baton) {
388393

389394
if (db->debug_trace == NULL) {
390395
// Add it.
391-
db->debug_trace = new AsyncTrace(db, TraceCallback);
396+
db->debug_trace = new AsyncTrace(db->loop, db, TraceCallback);
392397
sqlite3_trace(db->_handle, TraceCallback, db);
393398
}
394399
else {
@@ -426,7 +431,7 @@ void Database::RegisterProfileCallback(Baton* baton) {
426431

427432
if (db->debug_profile == NULL) {
428433
// Add it.
429-
db->debug_profile = new AsyncProfile(db, ProfileCallback);
434+
db->debug_profile = new AsyncProfile(db->loop, db, ProfileCallback);
430435
sqlite3_profile(db->_handle, ProfileCallback, db);
431436
}
432437
else {
@@ -467,7 +472,7 @@ void Database::RegisterUpdateCallback(Baton* baton) {
467472

468473
if (db->update_event == NULL) {
469474
// Add it.
470-
db->update_event = new AsyncUpdate(db, UpdateCallback);
475+
db->update_event = new AsyncUpdate(db->loop, db, UpdateCallback);
471476
sqlite3_update_hook(db->_handle, UpdateCallback, db);
472477
}
473478
else {
@@ -522,7 +527,7 @@ void Database::Work_BeginExec(Baton* baton) {
522527
assert(baton->db->open);
523528
assert(baton->db->_handle);
524529
assert(baton->db->pending == 0);
525-
int status = uv_queue_work(uv_default_loop(),
530+
int status = uv_queue_work(baton->db->loop,
526531
&baton->request, Work_Exec, (uv_after_work_cb)Work_AfterExec);
527532
assert(status == 0);
528533
}
@@ -622,7 +627,7 @@ void Database::Work_BeginLoadExtension(Baton* baton) {
622627
assert(baton->db->open);
623628
assert(baton->db->_handle);
624629
assert(baton->db->pending == 0);
625-
int status = uv_queue_work(uv_default_loop(),
630+
int status = uv_queue_work(baton->db->loop,
626631
&baton->request, Work_LoadExtension, reinterpret_cast<uv_after_work_cb>(Work_AfterLoadExtension));
627632
assert(status == 0);
628633
}

src/database.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ class Database : public Nan::ObjectWrap {
100100
friend class Statement;
101101

102102
protected:
103-
Database() : Nan::ObjectWrap(),
103+
Database(uv_loop_t* loop_) : Nan::ObjectWrap(),
104104
_handle(NULL),
105+
loop(loop_),
105106
open(false),
106107
closing(false),
107108
locked(false),
@@ -172,7 +173,10 @@ class Database : public Nan::ObjectWrap {
172173

173174
protected:
174175
sqlite3* _handle;
176+
public:
177+
uv_loop_t* loop;
175178

179+
protected:
176180
bool open;
177181
bool closing;
178182
bool locked;

src/macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const char* sqlite_authorizer_string(int type);
122122
assert(baton->stmt->prepared); \
123123
baton->stmt->locked = true; \
124124
baton->stmt->db->pending++; \
125-
int status = uv_queue_work(uv_default_loop(), \
125+
int status = uv_queue_work(baton->stmt->db->loop, \
126126
&baton->request, \
127127
Work_##type, reinterpret_cast<uv_after_work_cb>(Work_After##type)); \
128128
assert(status == 0);

src/statement.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ NAN_METHOD(Statement::New) {
115115
void Statement::Work_BeginPrepare(Database::Baton* baton) {
116116
assert(baton->db->open);
117117
baton->db->pending++;
118-
int status = uv_queue_work(uv_default_loop(),
118+
int status = uv_queue_work(baton->db->loop,
119119
&baton->request, Work_Prepare, (uv_after_work_cb)Work_AfterPrepare);
120120
assert(status == 0);
121121
}

src/statement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class Statement : public Nan::ObjectWrap {
174174
watcher.data = this;
175175
NODE_SQLITE3_MUTEX_INIT
176176
stmt->Ref();
177-
uv_async_init(uv_default_loop(), &watcher, async_cb);
177+
uv_async_init(stmt->db->loop, &watcher, async_cb);
178178
}
179179

180180
~Async() {

0 commit comments

Comments
 (0)