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
cleanup code
  • Loading branch information
toyobayashi committed Jun 17, 2022
commit fe1985fc7334fd02a0d199d2c7d5f12f40819db0
46 changes: 11 additions & 35 deletions packages/emnapi/src/emnapi.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <emscripten.h>
#include <stdlib.h>
#include "emnapi.h"
#include "node_api.h"

#ifdef __EMSCRIPTEN_PTHREADS__
// #include <emscripten/proxying.h>
#include <pthread.h>
#endif

#include <emscripten.h>
#include "emnapi.h"
#include "node_api.h"

#define CHECK_ENV(env) \
do { \
if ((env) == NULL) { \
Expand Down Expand Up @@ -61,26 +61,11 @@ const char* emnapi_error_messages[] = {
#define EMNAPI_MOD_NAME_X_HELPER(modname) #modname
#define EMNAPI_MOD_NAME_X(modname) EMNAPI_MOD_NAME_X_HELPER(modname)

// #ifdef __EMSCRIPTEN_PTHREADS__
// void* returner_main(void* queue) {
// emscripten_exit_with_live_runtime();
// }

// static pthread_t worker_thread = NULL;
// static em_proxying_queue* proxy_queue = NULL;
// #endif

EMSCRIPTEN_KEEPALIVE
void _emnapi_runtime_init(int* malloc_p,
int* free_p,
const char** key,
const char*** error_messages) {
// #ifdef __EMSCRIPTEN_PTHREADS__
// proxy_queue = em_proxying_queue_create();
// pthread_create(&worker_thread, NULL, returner_main, proxy_queue);
// pthread_detach(worker_thread);
// #endif

if (malloc_p) *malloc_p = (int)(malloc);
if (free_p) *free_p = (int)(free);
if (key) {
Expand All @@ -92,10 +77,8 @@ void _emnapi_runtime_init(int* malloc_p,
napi_status
napi_get_node_version(napi_env env,
const napi_node_version** version) {
if (env == NULL) return napi_invalid_arg;
if (version == NULL) {
return napi_set_last_error(env, napi_invalid_arg, 0, NULL);
}
CHECK_ENV(env);
CHECK_ARG(env, version);
static napi_node_version node_version = {
16,
15,
Expand All @@ -109,10 +92,8 @@ napi_get_node_version(napi_env env,
napi_status
emnapi_get_emscripten_version(napi_env env,
const emnapi_emscripten_version** version) {
if (env == NULL) return napi_invalid_arg;
if (version == NULL) {
return napi_set_last_error(env, napi_invalid_arg, 0, NULL);
}
CHECK_ENV(env);
CHECK_ARG(env, version);
static emnapi_emscripten_version emscripten_version = {
__EMSCRIPTEN_major__,
__EMSCRIPTEN_minor__,
Expand Down Expand Up @@ -141,7 +122,7 @@ typedef struct worker_count {
// 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_worker_count(worker_count* count);
extern int _emnapi_get_worker_count_js(worker_count* count);

napi_async_work _emnapi_async_work_init(
napi_env env,
Expand All @@ -165,7 +146,7 @@ void _emnapi_async_work_destroy(napi_async_work work) {
void* _emnapi_on_execute_async_work(void* arg) {
napi_async_work work = (napi_async_work) arg;
work->execute(work->env, work->data);
_emnapi_on_execute_async_work_js(work); // postMessage to main thread
_emnapi_on_execute_async_work_js(work); // postMessage to main thread
return NULL;
}
#endif
Expand Down Expand Up @@ -222,18 +203,13 @@ napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
CHECK_ARG(env, work);

worker_count count;
_emnapi_get_worker_count(&count);
_emnapi_get_worker_count_js(&count);
if (count.unused > 0 || count.running == 0) {
_emnapi_execute_async_work(work);
} else {
_emnapi_queue_async_work_js(work); // queue work
}

// work->tid = worker_thread;
// _emnapi_queue_async_work_js(work); // listen complete event
// emscripten_proxy_async(proxy_queue, worker_thread,
// _emnapi_on_execute_async_work, work);

return napi_clear_last_error(env);
#else
return napi_set_last_error(env, napi_generic_failure, 0, NULL);
Expand Down
4 changes: 2 additions & 2 deletions packages/emnapi/src/simple-async-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ mergeInto(LibraryManager.library, {
'return r;' +
'};',

_emnapi_get_worker_count__deps: ['$PThread'],
_emnapi_get_worker_count: function (struct: number) {
_emnapi_get_worker_count_js__deps: ['$PThread'],
_emnapi_get_worker_count_js: function (struct: number) {
const address = struct >> 2
HEAP32[address] = PThread.unusedWorkers.length
HEAP32[address + 1] = PThread.runningWorkers.length
Expand Down