From 541f2e34860e2759774ecd378bf287f7e5d849e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 5 Apr 2024 11:27:15 +0200 Subject: [PATCH] Update submodules and use `SharedArrayBuffer`s for thread data --- .gitmodules | 4 ---- CMakeLists.txt | 6 ------ src/bare.js | 2 +- src/runtime.c | 44 ++++++++++++-------------------------------- src/thread.c | 39 +++++++-------------------------------- src/thread.h | 2 +- src/thread.js | 6 ++++-- src/types.h | 28 +++++++--------------------- vendor/libjs | 2 +- vendor/libmem | 1 - vendor/libnapi | 2 +- 11 files changed, 34 insertions(+), 102 deletions(-) delete mode 160000 vendor/libmem diff --git a/.gitmodules b/.gitmodules index af42fd45..1dd397f9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,7 +14,3 @@ path = vendor/libutf url = ../../holepunchto/libutf.git shallow = true -[submodule "vendor/libmem"] - path = vendor/libmem - url = ../../holepunchto/libmem.git - shallow = true diff --git a/CMakeLists.txt b/CMakeLists.txt index 93acd2c8..93f4356f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,10 +27,6 @@ if(NOT TARGET utf) add_subdirectory(vendor/libutf EXCLUDE_FROM_ALL) endif() -if(NOT TARGET mem) - add_subdirectory(vendor/libmem EXCLUDE_FROM_ALL) -endif() - if(NOT TARGET c++) add_library(c++ STATIC IMPORTED GLOBAL) @@ -160,7 +156,6 @@ target_sources( PUBLIC $ $ - $ $ $ $ @@ -193,7 +188,6 @@ target_link_libraries( $ PUBLIC napi - mem utf url base64 diff --git a/src/bare.js b/src/bare.js index d691b880..dddfd2e2 100644 --- a/src/bare.js +++ b/src/bare.js @@ -261,5 +261,5 @@ const Module = require('bare-module') const url = require('bare-url') bare.run = function run (filename, source) { - Module.load(url.pathToFileURL(filename), source) + Module.load(url.pathToFileURL(filename), source ? Buffer.from(source) : null) } diff --git a/src/runtime.c b/src/runtime.c index 2c93dca8..ceb8caf6 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -721,30 +721,30 @@ bare_runtime_setup_thread (js_env_t *env, js_callback_info_t *info) { err = js_get_value_string_utf8(env, argv[0], filename, 4096, NULL); assert(err == 0); - bare_thread_source_t source = {bare_thread_source_none}; + bare_source_t source = {bare_source_none}; bool has_source; err = js_is_typedarray(env, argv[1], &has_source); assert(err == 0); if (has_source) { + source.type = bare_source_buffer; + err = js_get_typedarray_info(env, argv[1], NULL, (void **) &source.buffer.base, (size_t *) &source.buffer.len, NULL, NULL); assert(err == 0); - - source.type = bare_thread_source_buffer; } - bare_thread_data_t data = {bare_thread_data_none}; + bare_data_t data = {bare_data_none}; bool has_data; - err = js_is_typedarray(env, argv[2], &has_data); + err = js_is_sharedarraybuffer(env, argv[2], &has_data); assert(err == 0); if (has_data) { - err = js_get_typedarray_info(env, argv[2], NULL, (void **) &data.buffer.base, (size_t *) &data.buffer.len, NULL, NULL); - assert(err == 0); + data.type = bare_data_sharedarraybuffer; - data.type = bare_thread_data_buffer; + err = js_get_sharedarraybuffer_backing_store(env, argv[2], &data.backing_store); + assert(err == 0); } uint32_t stack_size; @@ -1070,9 +1070,11 @@ bare_runtime_teardown (bare_runtime_t *runtime, int *exit_code) { assert(err == 0); uv_ref((uv_handle_t *) &runtime->signals.suspend); + uv_ref((uv_handle_t *) &runtime->signals.resume); uv_close((uv_handle_t *) &runtime->signals.suspend, bare_runtime_on_handle_close); + uv_close((uv_handle_t *) &runtime->signals.resume, bare_runtime_on_handle_close); err = uv_run(runtime->loop, UV_RUN_DEFAULT); @@ -1108,35 +1110,13 @@ bare_runtime_run (bare_runtime_t *runtime, const char *filename, bare_source_t s switch (source.type) { case bare_source_none: - err = js_get_undefined(env, &args[1]); + err = js_get_null(env, &args[1]); assert(err == 0); break; case bare_source_buffer: { - js_value_t *arraybuffer; - - err = js_create_external_arraybuffer(env, source.buffer.base, source.buffer.len, NULL, NULL, &arraybuffer); + err = js_create_external_arraybuffer(env, source.buffer.base, source.buffer.len, NULL, NULL, &args[1]); assert(err == 0); - - err = js_create_typedarray(env, js_uint8_array, source.buffer.len, arraybuffer, 0, &args[1]); - if (err < 0) goto err; - break; - } - - case bare_source_arraybuffer: { - js_value_t *arraybuffer; - err = js_get_reference_value(env, source.arraybuffer, &arraybuffer); - assert(err == 0); - - err = js_delete_reference(env, source.arraybuffer); - assert(err == 0); - - size_t len; - err = js_get_arraybuffer_info(env, arraybuffer, NULL, &len); - assert(err == 0); - - err = js_create_typedarray(env, js_uint8_array, len, arraybuffer, 0, &args[1]); - if (err < 0) goto err; break; } } diff --git a/src/thread.c b/src/thread.c index 8d257a08..f1a86241 100644 --- a/src/thread.c +++ b/src/thread.c @@ -38,48 +38,22 @@ bare_thread_entry (void *data) { err = js_open_handle_scope(env, &scope); assert(err == 0); - bare_source_t thread_source; - - switch (thread->source.type) { - case bare_thread_source_none: - thread_source.type = bare_source_none; - break; - - case bare_thread_source_buffer: - thread_source.type = bare_source_arraybuffer; - - js_value_t *arraybuffer; - - void *data; - err = js_create_arraybuffer(env, thread->source.buffer.len, &data, &arraybuffer); - assert(err == 0); - - memcpy(data, thread->source.buffer.base, thread->source.buffer.len); - - err = js_create_reference(env, arraybuffer, 1, &thread_source.arraybuffer); - assert(err == 0); - break; - } + bare_source_t thread_source = thread->source; js_value_t *thread_data; switch (thread->data.type) { - case bare_thread_data_none: + case bare_data_none: default: err = js_get_null(runtime->env, &thread_data); assert(err == 0); break; - case bare_thread_data_buffer: { - js_value_t *arraybuffer; - - void *data; - err = js_create_arraybuffer(runtime->env, thread->data.buffer.len, &data, &arraybuffer); + case bare_data_sharedarraybuffer: { + err = js_create_sharedarraybuffer_with_backing_store(env, thread->data.backing_store, NULL, NULL, &thread_data); assert(err == 0); - memcpy(data, thread->data.buffer.base, thread->data.buffer.len); - - err = js_create_typedarray(runtime->env, js_uint8_array, thread->data.buffer.len, arraybuffer, 0, &thread_data); + err = js_release_arraybuffer_backing_store(env, thread->data.backing_store); assert(err == 0); break; } @@ -122,7 +96,7 @@ bare_thread_entry (void *data) { } int -bare_thread_create (bare_runtime_t *runtime, const char *filename, bare_thread_source_t source, bare_thread_data_t data, size_t stack_size, bare_thread_t **result) { +bare_thread_create (bare_runtime_t *runtime, const char *filename, bare_source_t source, bare_data_t data, size_t stack_size, bare_thread_t **result) { int err; js_env_t *env = runtime->env; @@ -160,6 +134,7 @@ bare_thread_create (bare_runtime_t *runtime, const char *filename, bare_thread_s } uv_sem_wait(&thread->lock); + uv_sem_post(&thread->lock); *result = thread; diff --git a/src/thread.h b/src/thread.h index d14b499c..6c0b64e6 100644 --- a/src/thread.h +++ b/src/thread.h @@ -7,7 +7,7 @@ #include "types.h" int -bare_thread_create (bare_runtime_t *runtime, const char *filename, bare_thread_source_t source, bare_thread_data_t data, size_t stack_size, bare_thread_t **result); +bare_thread_create (bare_runtime_t *runtime, const char *filename, bare_source_t source, bare_data_t data, size_t stack_size, bare_thread_t **result); int bare_thread_join (bare_runtime_t *runtime, bare_thread_t *thread); diff --git a/src/thread.js b/src/thread.js index f58e1b4f..9f159fd1 100644 --- a/src/thread.js +++ b/src/thread.js @@ -40,7 +40,9 @@ module.exports = exports = class Thread { structuredClone.preencode(state, serialized) - state.buffer = data = Buffer.allocUnsafe(state.end) + data = new SharedArrayBuffer(state.end) + + state.buffer = Buffer.from(data) structuredClone.encode(state, serialized) } @@ -98,7 +100,7 @@ class ThreadProxy { _ondata (data) { if (data === null) return - const state = { start: 0, end: data.byteLength, buffer: data } + const state = { start: 0, end: data.byteLength, buffer: Buffer.from(data) } this.data = structuredClone.deserialize(structuredClone.decode(state)) } diff --git a/src/types.h b/src/types.h index 9d269e30..2449af47 100644 --- a/src/types.h +++ b/src/types.h @@ -10,9 +10,8 @@ typedef struct bare_runtime_s bare_runtime_t; typedef struct bare_process_s bare_process_t; typedef struct bare_source_s bare_source_t; +typedef struct bare_data_s bare_data_t; typedef struct bare_thread_s bare_thread_t; -typedef struct bare_thread_source_s bare_thread_source_t; -typedef struct bare_thread_data_s bare_thread_data_t; typedef struct bare_thread_list_s bare_thread_list_t; typedef struct bare_module_list_s bare_module_list_t; @@ -58,34 +57,21 @@ struct bare_source_s { enum { bare_source_none, bare_source_buffer, - bare_source_arraybuffer, } type; union { uv_buf_t buffer; - js_ref_t *arraybuffer; }; }; -struct bare_thread_source_s { +struct bare_data_s { enum { - bare_thread_source_none, - bare_thread_source_buffer, + bare_data_none, + bare_data_sharedarraybuffer, } type; union { - uv_buf_t buffer; - }; -}; - -struct bare_thread_data_s { - enum { - bare_thread_data_none, - bare_thread_data_buffer, - } type; - - union { - uv_buf_t buffer; + js_arraybuffer_backing_store_t *backing_store; }; }; @@ -97,8 +83,8 @@ struct bare_thread_s { char *filename; - bare_thread_source_t source; - bare_thread_data_t data; + bare_source_t source; + bare_data_t data; bool exited; }; diff --git a/vendor/libjs b/vendor/libjs index 1c88ac1b..837c2ecb 160000 --- a/vendor/libjs +++ b/vendor/libjs @@ -1 +1 @@ -Subproject commit 1c88ac1bbbfb2345f15d0f77ac26ceae20426e21 +Subproject commit 837c2ecb5e83a0b3d5cd8e17f91f08019c82cc8a diff --git a/vendor/libmem b/vendor/libmem deleted file mode 160000 index 6a956363..00000000 --- a/vendor/libmem +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6a95636301f8b15605f8b6465b814e39c4f62541 diff --git a/vendor/libnapi b/vendor/libnapi index 7cdc2269..0d66b696 160000 --- a/vendor/libnapi +++ b/vendor/libnapi @@ -1 +1 @@ -Subproject commit 7cdc2269fd0bd5334ad54dedfe7284f7468d65fa +Subproject commit 0d66b696a855a85995430a114393200c6946b799