Skip to content

Commit

Permalink
Update submodules and use SharedArrayBuffers for thread data
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Apr 5, 2024
1 parent 21e13cb commit 541f2e3
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 102 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -160,7 +156,6 @@ target_sources(
PUBLIC
$<TARGET_OBJECTS:uv>
$<TARGET_OBJECTS:napi>
$<TARGET_OBJECTS:mem>
$<TARGET_OBJECTS:utf>
$<TARGET_OBJECTS:url>
$<TARGET_OBJECTS:base64>
Expand Down Expand Up @@ -193,7 +188,6 @@ target_link_libraries(
$<TARGET_PROPERTY:uv,INTERFACE_LINK_LIBRARIES>
PUBLIC
napi
mem
utf
url
base64
Expand Down
2 changes: 1 addition & 1 deletion src/bare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
44 changes: 12 additions & 32 deletions src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down
39 changes: 7 additions & 32 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
}
Expand Down
28 changes: 7 additions & 21 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
};
};

Expand All @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion vendor/libjs
Submodule libjs updated 125 files
1 change: 0 additions & 1 deletion vendor/libmem
Submodule libmem deleted from 6a9563
2 changes: 1 addition & 1 deletion vendor/libnapi
Submodule libnapi updated 2 files
+37 −37 include/napi.h
+1 −1 vendor/libjs

0 comments on commit 541f2e3

Please sign in to comment.