Unverified Commit 36ec8cf0 by Dane Springmeyer Committed by GitHub

Merge pull request #1007 from rpetrich/experimental-worker

Add support for running inside a node 10.5 worker_thread
parents 5278b2b0 f91b4fe4
...@@ -50,7 +50,8 @@ ...@@ -50,7 +50,8 @@
"prepublishOnly": "npm ls", "prepublishOnly": "npm ls",
"install": "node-pre-gyp install --fallback-to-build", "install": "node-pre-gyp install --fallback-to-build",
"pretest": "node test/support/createdb.js", "pretest": "node test/support/createdb.js",
"test": "mocha -R spec --timeout 480000" "test": "mocha -R spec --timeout 480000",
"test:worker": "node --experimental-worker scripts/mocha-as-worker.js -R spec --timeout 480000"
}, },
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"keywords": [ "keywords": [
......
// Run the mocha tests in a worker
// Not a clean approach, but is sufficient to verify correctness
const worker_threads = require("worker_threads");
const path = require("path");
if (worker_threads.isMainThread) {
const worker = new worker_threads.Worker(__filename, { workerData: { windowSize: process.stdout.getWindowSize() } });
worker.on("error", console.error);
} else {
process.stdout.getWindowSize = () => worker_threads.workerData.windowSize;
const mochaPath = path.resolve(require.resolve("mocha"), "../bin/_mocha");
require(mochaPath);
}
...@@ -22,11 +22,11 @@ public: ...@@ -22,11 +22,11 @@ public:
Parent* parent; Parent* parent;
public: public:
Async(Parent* parent_, Callback cb_) Async(uv_loop_t* loop_, Parent* parent_, Callback cb_)
: callback(cb_), parent(parent_) { : callback(cb_), parent(parent_) {
watcher.data = this; watcher.data = this;
NODE_SQLITE3_MUTEX_INIT NODE_SQLITE3_MUTEX_INIT
uv_async_init(uv_default_loop(), &watcher, reinterpret_cast<uv_async_cb>(listener)); uv_async_init(loop_, &watcher, reinterpret_cast<uv_async_cb>(listener));
} }
static void listener(uv_async_t* handle, int status) { static void listener(uv_async_t* handle, int status) {
......
...@@ -127,7 +127,12 @@ NAN_METHOD(Database::New) { ...@@ -127,7 +127,12 @@ NAN_METHOD(Database::New) {
callback = Local<Function>::Cast(info[pos++]); callback = Local<Function>::Cast(info[pos++]);
} }
Database* db = new Database(); #if NODE_MODULE_VERSION > NODE_9_0_MODULE_VERSION
uv_loop_t* loop = node::GetCurrentEventLoop(info.GetIsolate());
#else
uv_loop_t* loop = uv_default_loop();
#endif
Database* db = new Database(loop);
db->Wrap(info.This()); db->Wrap(info.This());
Nan::ForceSet(info.This(), Nan::New("filename").ToLocalChecked(), info[0].As<String>(), ReadOnly); Nan::ForceSet(info.This(), Nan::New("filename").ToLocalChecked(), info[0].As<String>(), ReadOnly);
...@@ -141,7 +146,7 @@ NAN_METHOD(Database::New) { ...@@ -141,7 +146,7 @@ NAN_METHOD(Database::New) {
} }
void Database::Work_BeginOpen(Baton* baton) { void Database::Work_BeginOpen(Baton* baton) {
int status = uv_queue_work(uv_default_loop(), int status = uv_queue_work(baton->db->loop,
&baton->request, Work_Open, (uv_after_work_cb)Work_AfterOpen); &baton->request, Work_Open, (uv_after_work_cb)Work_AfterOpen);
assert(status == 0); assert(status == 0);
} }
...@@ -227,7 +232,7 @@ void Database::Work_BeginClose(Baton* baton) { ...@@ -227,7 +232,7 @@ void Database::Work_BeginClose(Baton* baton) {
baton->db->RemoveCallbacks(); baton->db->RemoveCallbacks();
baton->db->closing = true; baton->db->closing = true;
int status = uv_queue_work(uv_default_loop(), int status = uv_queue_work(baton->db->loop,
&baton->request, Work_Close, (uv_after_work_cb)Work_AfterClose); &baton->request, Work_Close, (uv_after_work_cb)Work_AfterClose);
assert(status == 0); assert(status == 0);
} }
...@@ -388,7 +393,7 @@ void Database::RegisterTraceCallback(Baton* baton) { ...@@ -388,7 +393,7 @@ void Database::RegisterTraceCallback(Baton* baton) {
if (db->debug_trace == NULL) { if (db->debug_trace == NULL) {
// Add it. // Add it.
db->debug_trace = new AsyncTrace(db, TraceCallback); db->debug_trace = new AsyncTrace(db->loop, db, TraceCallback);
sqlite3_trace(db->_handle, TraceCallback, db); sqlite3_trace(db->_handle, TraceCallback, db);
} }
else { else {
...@@ -426,7 +431,7 @@ void Database::RegisterProfileCallback(Baton* baton) { ...@@ -426,7 +431,7 @@ void Database::RegisterProfileCallback(Baton* baton) {
if (db->debug_profile == NULL) { if (db->debug_profile == NULL) {
// Add it. // Add it.
db->debug_profile = new AsyncProfile(db, ProfileCallback); db->debug_profile = new AsyncProfile(db->loop, db, ProfileCallback);
sqlite3_profile(db->_handle, ProfileCallback, db); sqlite3_profile(db->_handle, ProfileCallback, db);
} }
else { else {
...@@ -467,7 +472,7 @@ void Database::RegisterUpdateCallback(Baton* baton) { ...@@ -467,7 +472,7 @@ void Database::RegisterUpdateCallback(Baton* baton) {
if (db->update_event == NULL) { if (db->update_event == NULL) {
// Add it. // Add it.
db->update_event = new AsyncUpdate(db, UpdateCallback); db->update_event = new AsyncUpdate(db->loop, db, UpdateCallback);
sqlite3_update_hook(db->_handle, UpdateCallback, db); sqlite3_update_hook(db->_handle, UpdateCallback, db);
} }
else { else {
...@@ -522,7 +527,7 @@ void Database::Work_BeginExec(Baton* baton) { ...@@ -522,7 +527,7 @@ void Database::Work_BeginExec(Baton* baton) {
assert(baton->db->open); assert(baton->db->open);
assert(baton->db->_handle); assert(baton->db->_handle);
assert(baton->db->pending == 0); assert(baton->db->pending == 0);
int status = uv_queue_work(uv_default_loop(), int status = uv_queue_work(baton->db->loop,
&baton->request, Work_Exec, (uv_after_work_cb)Work_AfterExec); &baton->request, Work_Exec, (uv_after_work_cb)Work_AfterExec);
assert(status == 0); assert(status == 0);
} }
...@@ -622,7 +627,7 @@ void Database::Work_BeginLoadExtension(Baton* baton) { ...@@ -622,7 +627,7 @@ void Database::Work_BeginLoadExtension(Baton* baton) {
assert(baton->db->open); assert(baton->db->open);
assert(baton->db->_handle); assert(baton->db->_handle);
assert(baton->db->pending == 0); assert(baton->db->pending == 0);
int status = uv_queue_work(uv_default_loop(), int status = uv_queue_work(baton->db->loop,
&baton->request, Work_LoadExtension, reinterpret_cast<uv_after_work_cb>(Work_AfterLoadExtension)); &baton->request, Work_LoadExtension, reinterpret_cast<uv_after_work_cb>(Work_AfterLoadExtension));
assert(status == 0); assert(status == 0);
} }
......
...@@ -100,8 +100,9 @@ public: ...@@ -100,8 +100,9 @@ public:
friend class Statement; friend class Statement;
protected: protected:
Database() : Nan::ObjectWrap(), Database(uv_loop_t* loop_) : Nan::ObjectWrap(),
_handle(NULL), _handle(NULL),
loop(loop_),
open(false), open(false),
closing(false), closing(false),
locked(false), locked(false),
...@@ -172,7 +173,10 @@ protected: ...@@ -172,7 +173,10 @@ protected:
protected: protected:
sqlite3* _handle; sqlite3* _handle;
public:
uv_loop_t* loop;
protected:
bool open; bool open;
bool closing; bool closing;
bool locked; bool locked;
......
...@@ -122,7 +122,7 @@ const char* sqlite_authorizer_string(int type); ...@@ -122,7 +122,7 @@ const char* sqlite_authorizer_string(int type);
assert(baton->stmt->prepared); \ assert(baton->stmt->prepared); \
baton->stmt->locked = true; \ baton->stmt->locked = true; \
baton->stmt->db->pending++; \ baton->stmt->db->pending++; \
int status = uv_queue_work(uv_default_loop(), \ int status = uv_queue_work(baton->stmt->db->loop, \
&baton->request, \ &baton->request, \
Work_##type, reinterpret_cast<uv_after_work_cb>(Work_After##type)); \ Work_##type, reinterpret_cast<uv_after_work_cb>(Work_After##type)); \
assert(status == 0); assert(status == 0);
......
...@@ -115,7 +115,7 @@ NAN_METHOD(Statement::New) { ...@@ -115,7 +115,7 @@ NAN_METHOD(Statement::New) {
void Statement::Work_BeginPrepare(Database::Baton* baton) { void Statement::Work_BeginPrepare(Database::Baton* baton) {
assert(baton->db->open); assert(baton->db->open);
baton->db->pending++; baton->db->pending++;
int status = uv_queue_work(uv_default_loop(), int status = uv_queue_work(baton->db->loop,
&baton->request, Work_Prepare, (uv_after_work_cb)Work_AfterPrepare); &baton->request, Work_Prepare, (uv_after_work_cb)Work_AfterPrepare);
assert(status == 0); assert(status == 0);
} }
......
...@@ -174,7 +174,7 @@ public: ...@@ -174,7 +174,7 @@ public:
watcher.data = this; watcher.data = this;
NODE_SQLITE3_MUTEX_INIT NODE_SQLITE3_MUTEX_INIT
stmt->Ref(); stmt->Ref();
uv_async_init(uv_default_loop(), &watcher, async_cb); uv_async_init(stmt->db->loop, &watcher, async_cb);
} }
~Async() { ~Async() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment