Skip to content

Commit

Permalink
Convert unnecessary EM_ASM() blocks to JS library functions in pthrea…
Browse files Browse the repository at this point in the history
…ds library. (#10124)
  • Loading branch information
juj authored Jan 3, 2020
1 parent 041988e commit ef45532
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
23 changes: 23 additions & 0 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,29 @@ var LibraryPThread = {
// Call inside asm.js/wasm module to set up the stack frame for this pthread in asm.js/wasm module scope
establishStackSpace(stackTop, stackMax);
},
// This function is called internally to notify target thread ID that it has messages it needs to
// process in its message queue inside the Wasm heap. As a helper, the caller must also pass the
// ID of the main browser thread to this function, to avoid needlessly ping-ponging between JS and
// Wasm boundaries.
_emscripten_notify_thread_queue: function(targetThreadId, mainThreadId) {
if (targetThreadId == mainThreadId) {
postMessage({cmd : 'processQueuedMainThreadWork'});
} else if (ENVIRONMENT_IS_PTHREAD) {
postMessage({targetThread: targetThreadId, cmd: 'processThreadQueue'});
} else {
var pthread = PThread.pthreads[targetThreadId];
var worker = pthread && pthread.worker;
if (!worker) {
#if ASSERTIONS
err('Cannot send message to thread with ID ' + targetThreadId + ', unknown thread ID!');
#endif
return /*0*/;
}
worker.postMessage({cmd : 'processThreadQueue'});
}
return 1;
}
};
autoAddDeps(LibraryPThread, '$PThread');
Expand Down
36 changes: 7 additions & 29 deletions system/lib/pthread/library_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ static void em_queued_call_free(em_queued_call* call) {
void emscripten_async_waitable_close(em_queued_call* call) { em_queued_call_free(call); }

extern double emscripten_receive_on_main_thread_js(int functionIndex, int numCallArgs, double* args);
extern int _emscripten_notify_thread_queue(pthread_t targetThreadId, pthread_t mainThreadId);

#if defined(__has_feature)
#if __has_feature(address_sanitizer)
Expand Down Expand Up @@ -513,35 +514,12 @@ static void EMSCRIPTEN_KEEPALIVE emscripten_async_queue_call_on_thread(
// so send a message to it to ensure that it wakes up to start processing the command we have
// posted.
if (head == tail) {
if (target_thread == emscripten_main_browser_thread_id()) {
EM_ASM(postMessage({cmd : 'processQueuedMainThreadWork'}));
} else {
int success = EM_ASM_INT(
{
if (!ENVIRONMENT_IS_PTHREAD) {
if (!PThread.pthreads[$0] || !PThread.pthreads[$0].worker) {
// #if DEBUG
// Module.printErr('Cannot send message
//to thread with ID ' + $0 + ', unknown thread ID!');
// #endif
return 0;
}
PThread.pthreads[$0].worker.postMessage({cmd : 'processThreadQueue'});
} else {
postMessage({targetThread : $0, cmd : 'processThreadQueue'});
}
return 1;
},
target_thread);

// Failed to dispatch the thread, delete the crafted message.
if (!success) {
em_queued_call_free(call);
pthread_mutex_unlock(&call_queue_lock);
return;
}

// TODO: Need to postMessage() to a specific target Worker that is hosting target_thread....
int success = _emscripten_notify_thread_queue(target_thread, emscripten_main_browser_thread_id());
// Failed to dispatch the thread, delete the crafted message.
if (!success) {
em_queued_call_free(call);
pthread_mutex_unlock(&call_queue_lock);
return;
}
}

Expand Down

0 comments on commit ef45532

Please sign in to comment.