forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Implement ThreadSafeFunction class
This PR is implementing ThreadSafeFunction class wraps napi_threadsafe_function*. Fixes nodejs#312.
- Loading branch information
Showing
8 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#define NAPI_EXPERIMENTAL | ||
#include <uv.h> | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
const size_t ARRAY_LENGTH = 10; | ||
const size_t MAX_QUEUE_SIZE = 2; | ||
|
||
// Thread data to transmit to JS | ||
static int ints[ARRAY_LENGTH]; | ||
|
||
static uv_thread_t uv_threads[2]; | ||
static napi_threadsafe_function ts_fn; | ||
static ThreadSafeFunction ts_fn_wrap; | ||
|
||
int sum = 0; | ||
|
||
static void data_source_thread(void* data) { | ||
int args = 15; | ||
ts_fn_wrap.Call([args](Napi::Env env, Napi::Function jsCallback) { | ||
jsCallback.Call({ Number::New(env, args) }); | ||
}, napi_tsfn_blocking); | ||
sum += 5; | ||
if (data) {} | ||
} | ||
|
||
static Value StartThreadInternal(const CallbackInfo& info, bool block_on_full) { | ||
ts_fn_wrap = ThreadSafeFunction(info.Env(), info[0].As<Function>(), "", Object(), 10, 2); | ||
|
||
if (uv_thread_create(&uv_threads[0], data_source_thread, (void*)ts_fn) == 0) { | ||
printf("created thread1\n"); | ||
} | ||
if (uv_thread_create(&uv_threads[1], data_source_thread, (void*)ts_fn) == 0) { | ||
printf("created thread2\n"); | ||
} | ||
|
||
uv_thread_join(&uv_threads[1]); | ||
uv_thread_join(&uv_threads[0]); | ||
printf("%d\n", sum); | ||
if (block_on_full) {} | ||
|
||
return Object::New(info.Env()); | ||
} | ||
|
||
static Value StartThread(const CallbackInfo& info) { | ||
return StartThreadInternal(info, true); | ||
} | ||
|
||
static Value StartThreadNoNative(const CallbackInfo& info) { | ||
return StartThreadInternal(info, true); | ||
} | ||
|
||
static Value StartThreadNonblocking(const CallbackInfo& info) { | ||
return StartThreadInternal(info, false); | ||
} | ||
|
||
static Value StopThread(const CallbackInfo& info) { | ||
return Object::New(info.Env()); | ||
} | ||
|
||
static Value Unref(const CallbackInfo& info) { | ||
return Object::New(info.Env()); | ||
} | ||
|
||
static Value Release(const CallbackInfo& info) { | ||
return Object::New(info.Env()); | ||
} | ||
|
||
Object InitThreadSafeFunction(Env env) { | ||
for (size_t index = 0; index < ARRAY_LENGTH; index++) { | ||
ints[index] = index; | ||
} | ||
|
||
Object exports = Object::New(env); | ||
exports["ARRAY_LENGTH"] = Number::New(env, ARRAY_LENGTH); | ||
exports["MAX_QUEUE_SIZE"] = Number::New(env, MAX_QUEUE_SIZE); | ||
exports["startThread"] = Function::New(env, StartThread); | ||
exports["startThreadNoNative"] = Function::New(env, StartThreadNoNative); | ||
exports["startThreadNonblocking"] = Function::New(env, StartThreadNonblocking); | ||
exports["stopThread"] = Function::New(env, StopThread); | ||
exports["unref"] = Function::New(env, Unref); | ||
exports["release"] = Function::New(env, Release); | ||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
test(require(`../build/${buildType}/binding.node`)); | ||
test(require(`../build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
binding.threadsafe_function.startThread(function(number) { | ||
console.log("callback-ok?" + number); | ||
}, "hello"); | ||
console.log(binding.threadsafe_function.ARRAY_LENGTH); | ||
console.log(binding.threadsafe_function.MAX_QUEUE_SIZE); | ||
} |