Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support simple asynchronous operations #3

Merged
merged 14 commits into from
Jun 17, 2022
Prev Previous commit
Next Next commit
support pthread pool size is 0
  • Loading branch information
toyobayashi committed Jun 17, 2022
commit 9a6edc8c3ed9f72b380222efeeda1d663753acd2
12 changes: 9 additions & 3 deletions packages/emnapi/src/emnapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,16 @@ struct napi_async_work__ {
pthread_t tid;
};

typedef struct worker_count {
int unused;
int running;
} worker_count;

// extern void _emnapi_create_async_work_js(napi_async_work work);
extern void _emnapi_delete_async_work_js(napi_async_work work);
extern void _emnapi_queue_async_work_js(napi_async_work work);
extern void _emnapi_on_execute_async_work_js(napi_async_work work);
extern int _emnapi_get_unused_worker_size();
extern int _emnapi_get_worker_count(worker_count* count);

napi_async_work _emnapi_async_work_init(
napi_env env,
Expand Down Expand Up @@ -219,8 +224,9 @@ napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
CHECK_ENV(env);
CHECK_ARG(env, work);

int unused_worker_size = _emnapi_get_unused_worker_size();
if (unused_worker_size > 0) {
worker_count count;
_emnapi_get_worker_count(&count);
if (count.unused > 0 || count.running == 0) {
_emnapi_execute_async_work(work);
} else {
_emnapi_queue_async_work_js(work); // queue work
Expand Down
8 changes: 5 additions & 3 deletions packages/emnapi/src/simple-async-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ mergeInto(LibraryManager.library, {
'return r;' +
'};',

_emnapi_get_unused_worker_size__deps: ['$PThread'],
_emnapi_get_unused_worker_size: function () {
return PThread.unusedWorkers.length
_emnapi_get_worker_count__deps: ['$PThread'],
_emnapi_get_worker_count: function (struct: number) {
const address = struct >> 2
HEAP32[address] = PThread.unusedWorkers.length
HEAP32[address + 1] = PThread.runningWorkers.length
},

_emnapi_on_execute_async_work_js: function (work: number) {
Expand Down