Multithreading 6/N: Proxy calls to JavaScript functions from pthreads to main browser thread#5531
Conversation
| var returnValue; | ||
| var funcTable = (e.data.func >= 0) ? proxiedFunctionTable : ASM_CONSTS; | ||
| var funcIdx = (e.data.func >= 0) ? e.data.func : (-1 - e.data.func); | ||
| PThread.currentProxiedOperationCallerThread = worker.pthread.threadInfoStruct; // Sometimes we need to backproxy events to the calling thread (e.g. HTML5 DOM events handlers such as emscripten_set_mousemove_callback()), so keep track in a globally accessible variable about the thread that initiated the proxying. |
There was a problem hiding this comment.
Here I have an exception : worker.pthread is undefined, when a proxy call is lanch but the worker terminate and is clean.
There was a problem hiding this comment.
Any chance you'd be able to make a small test case?
There was a problem hiding this comment.
I test to reproduce it in a small test case but for now, i'm not able. I will continue to try.
kripken
left a comment
There was a problem hiding this comment.
Are the 4 proxying modes documented? the names suggest what they do, but I'm not 100% sure.
src/jsifier.js
Outdated
|
|
||
| // This is ad-hoc numbering scheme to map signatures to IDs, and must agree with call handler in src/library_pthread.js. | ||
| // Once proxied function calls no longer go through postMessage()s but instead in the heap, this will need to change, since int vs float will matter. | ||
| var functionCallOrdinal = sig.length + (sizeofReturn == 8 ? 20 : 0); |
There was a problem hiding this comment.
sizeOfReturn being 8 equals the case of a double, and the ID 20 is where functions with double return type start. The comment refers to the matching IDs at src/library_pthread.js: https://github.com/kripken/emscripten/pull/5531/files#diff-db41bea94577c2dd9b0eef0308b06cf9R272
There was a problem hiding this comment.
Changed sizeOfReturn == 8 test to sig[0] == 'd' to be more self-documenting.
|
I'll need to write a full set of documentation about the updated pthreads support. I'd like to defer that to once these PRs land, so that I can write a coherent update of the current doc page at http://kripken.github.io/emscripten-site/docs/porting/pthreads.html |
4b8ddf2 to
6945de2
Compare
src/jsifier.js
Outdated
|
|
||
| // This is ad-hoc numbering scheme to map signatures to IDs, and must agree with call handler in src/library_pthread.js. | ||
| // Once proxied function calls no longer go through postMessage()s but instead in the heap, this will need to change, since int vs float will matter. | ||
| var functionCallOrdinal = sig.length + (sig[0] == 'd' ? 20 : 0); |
There was a problem hiding this comment.
i still think the 20 is bad - couldn't that become invalid if we change memory layout? or am i missing something?
at minimum lets put a big comment with XXX FIXME on this.
There was a problem hiding this comment.
The number 20 is more like a magic ID, it only needs to match in this spot in src/jsifier.js and then in src/pthread-main.js (in https://github.com/kripken/emscripten/pull/5531/files#diff-db41bea94577c2dd9b0eef0308b06cf9R272). There is nothing about the memory layout itself that would interact with this. Only limit to number 20 is if there's a function that needs proxying that would take in more than 20 input parameters, then we'd have to bump up the number.
Basically this is turning all these signature strings 'vii', 'viii', 'iii', 'dii' into ID numbers, so that the receiving code knows what kind of function signature it should be calling.
There was a problem hiding this comment.
I see, so it just cares about the # of params and whether the return value is float? maybe add a comment with that?
also, how about storing 20 in a const, and also asserting that the sig.length is shorter than it?
There was a problem hiding this comment.
Good suggestions, improved this. Also while reading, found a yet unimplemented case in handling there, fixed that, and also changed the number from 20 to 32 to be more programmer-friendly :)
|
Bigger docs later sounds ok, but just for reviewing this PR, I could use some short explanations :) |
6945de2 to
7a2f474
Compare
|
Edited the PR to remove the |
The The |
|
I see, thanks. Why not call it sync and async, then, instead of main and async? |
7a2f474 to
cca54d2
Compare
…ction calls to main browser thread.
…e.data' to 'd' since it is accessed awfully many times in library_pthread.js.
Hmm, yeah, I first started with |
cca54d2 to
244d10d
Compare
|
Updated the PR, how does it look now? |
src/jsifier.js
Outdated
|
|
||
| function sizeofType(t) { | ||
| switch(t) { | ||
| case 'd': |
There was a problem hiding this comment.
please do case bodies on same line, or {,}
This PR implements a mechanism where JavaScript library functions can be attributed with
foo__proxy: 'mode', wheremodeis one of'main','async','main_gl'or'async_gl'. This allows a pthread running in a web worker to either synchronously or asynchronously call out to JavaScript on the main browser thread.The
_glvariants decide proxying based on the currently active WebGL context for the calling thread. If the calling thread owns the GL context, then the call is not proxied. If the calling thread instead has activated a proxied context that is owned by the main browser thread, then the call is proxied. (This machinery is expanded on along with tests in a later PR)This PR requires #5527 to land first in order to work.