Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/libembind.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var LibraryEmbind = {
$UnboundTypeError: class extends Error {},
$PureVirtualError: class extends Error {},
$GenericWireTypeSize: {{{ 2 * POINTER_SIZE }}},
#if PTHREADS || WASM_WORKERS
$postCtors: null, // Unused symbol stub for injecting post ctors call.
$postCtors__postset: () => { addAtPostCtor('__embind_post_ctors()'); },
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just make this is __postset of some function that a core embind dependnecy? Adding this new symbol and forcing it to be included seems a more complex than it needs to be.

#if EMBIND_AOT
$InvokerFunctions: '<<< EMBIND_AOT_INVOKERS >>>',
#endif
Expand Down Expand Up @@ -2320,4 +2324,8 @@ var LibraryEmbind = {
},
};

#if PTHREADS || WASM_WORKERS
extraLibraryFuncs.push('$postCtors');
#endif

addToLibrary(LibraryEmbind);
2 changes: 1 addition & 1 deletion src/lib/libwasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ addToLibrary({

#if EMBIND
// Embind must initialize itself on all threads, as it generates support JS.
__embind_initialize_bindings();
__embind_initialize_worker_bindings();
#endif

#if AUDIO_WORKLET
Expand Down
2 changes: 1 addition & 1 deletion src/runtime_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ if (ENVIRONMENT_IS_PTHREAD) {
#endif
// Embind must initialize itself on all threads, as it generates support JS.
// We only do this once per worker since they get reused
__embind_initialize_bindings();
__embind_initialize_worker_bindings();
#endif // EMBIND
initializedJS = true;
}
Expand Down
10 changes: 9 additions & 1 deletion system/lib/embind/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using namespace emscripten;
using namespace internal;

static std::atomic<bool> embind_initialized(false);

extern "C" {

const char* __getTypeName(const std::type_info* ti) {
Expand Down Expand Up @@ -52,7 +54,13 @@ const char* __getTypeName(const std::type_info* ti) {

static InitFunc* init_funcs = nullptr;

void _embind_initialize_bindings() {
void _embind_post_ctors() {
embind_initialized = true;
embind_initialized.notify_all();
}

void _embind_initialize_worker_bindings() {
embind_initialized.wait(false);
for (auto* f = init_funcs; f; f = f->next) {
f->init_func();
}
Expand Down
14 changes: 14 additions & 0 deletions test/embind/test_embind_val_in_static_pthread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <emscripten/val.h>
#include <emscripten/emscripten.h>
#include <thread>

auto BGThread = std::thread([]() {
auto globalThis = emscripten::val::global("globalThis");
assert(globalThis.typeOf().as<std::string>() == "object");
printf("worker\n");
});

int main() {
BGThread.join();
printf("done\n");
}
6 changes: 6 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3429,6 +3429,12 @@ class MyClass {};
''')
self.do_runf('main.cpp', emcc_args=['-lembind'])

@node_pthreads
def test_embind_val_in_static_pthread(self):
# Test that threads wait for embind to initialize before running.
self.emcc_args += ['-lembind', '-sPTHREAD_POOL_SIZE=2']
self.do_runf('embind/test_embind_val_in_static_pthread.cpp', 'worker\ndone\n')

@requires_jspi
@parameterized({
'': [['-sJSPI_EXPORTS=async*']],
Expand Down
2 changes: 1 addition & 1 deletion tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ def limit_incoming_module_api():
# https://github.com/emscripten-core/emscripten/issues/21653
settings.REQUIRED_EXPORTS.append('__getTypeName')
if settings.PTHREADS or settings.WASM_WORKERS:
settings.REQUIRED_EXPORTS.append('_embind_initialize_bindings')
settings.REQUIRED_EXPORTS += ['_embind_initialize_worker_bindings', '_embind_post_ctors']
# Needed to assign the embind exports to the ES exports.
if settings.MODULARIZE == 'instance':
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$addOnPostCtor']
Expand Down
2 changes: 1 addition & 1 deletion tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ class libwebgpu_cpp(MTLibrary):
src_files = ['webgpu_cpp.cpp']


class libembind(Library):
class libembind(MTLibrary):
name = 'libembind'
never_force = True

Expand Down