User:Roc/SharedMemoryWorkersProposal: Difference between revisions

Jump to navigation Jump to search
no edit summary
(Created page with "Our dilemma is that we want to restrict to a worker "running asm.js only", but we want regular JS code to act as a glue layer between asm.js and Web platform APIs. So, what i...")
 
No edit summary
 
Line 11: Line 11:
* The asm.js type system prevents asm.js code from passing a SharedArrayBuffer to any external JS methods it calls.
* The asm.js type system prevents asm.js code from passing a SharedArrayBuffer to any external JS methods it calls.
No state flags, stack inspection or other dynamic checks required.
No state flags, stack inspection or other dynamic checks required.
== Exchanging Buffers With Web APIs ==
We need low-overhead data exchange with Web APIs that take typed-array parameters, let's say ArrayBufferViews. Here's one possible way to do that:
Give each SharedArrayBufferModule a map from ints to ArrayBufferViews. Let's call this the buffer map. Add the following APIs to SharedArrayBufferModule:
* <tt>int putBuffer(ArrayBufferView view)</tt>: Lets regular JS attach an ArrayBufferView to the buffer map, returning the index of the new entry.
* <tt>ArrayBufferView takeBuffer(int index)</tt>: Lets regular JS take an ArrayBufferView out of the buffer map, removing the entry.
Add the following APIs to SharedArrayBuffer:
* <tt>int createBuffer(int fromAddr, int length)</tt>: Lets asm.js code copy a range of the SharedArrayBuffer to a new entry in the buffer map, returning the index of the new entry.
* <tt>void consumeBuffer(int index, int toAddr)</tt>: Lets asm.js code copy the contents of a buffer map entry to the SharedArrayBuffer and remove the entry.
[Obvious variations on these APIs might make sense.]
Thus, to pass data from asm.js to regular JS, asm.js calls createBuffer to copy the data, passes the resulting buffer index to regular JS, and then regular JS calls takeBuffer to retrieve the buffer. To pass data in the other direction, regular JS attaches the ArrayBufferView to the buffer map, then passes the index of the buffer to asm.js, which calls consumeBuffer to copy the data to its heap.
== Zero-Copy Data Exchange With Web APIs ==
For performance-critical APIs where data copying is a problem (e.g. perhaps WebGLRenderingContext::texImage2D), we could introduce a SharedArrayBufferView representing a region of a SharedArrayBuffer. This is unrelated to an ArrayBufferView in the WebIDL type hierarchy so WebIDL objects would need to add explicit overloads taking SharedArrayBufferView. This is good because SharedArrayBufferView is dangerous to work with, since its contents can be changed asynchronously by other threads.
Then, similar to the previous section, we add a shared-buffer map to to SharedArrayBufferModule. SharedArrayBufferModule gets the following API:
* <tt>SharedArrayBufferView takeSharedBuffer(int index)</tt>: Lets regular JS take a SharedArrayBufferView out of the shared-buffer map, deleting the entry.
Add the following API to SharedArrayBuffer:
* <tt>int createSharedBuffer(int fromAddr, int length)</tt>: Lets asm.js create a new shared-buffer entry and returns its index.
1,295

edits

Navigation menu