30
30
31
31
32
32
mergeInto ( LibraryManager . library , {
33
- $_wasmWorkers : { } ,
34
- $_wasmWorkersID : 1 ,
33
+ $_wasmWorkers__deps : [ '$HandleAllocator' ] ,
34
+ $_wasmWorkers : "new HandleAllocator();" ,
35
35
36
36
// Starting up a Wasm Worker is an asynchronous operation, hence if the parent thread performs any
37
37
// postMessage()-based wasm function calls s to the Worker, they must be delayed until the async
@@ -105,17 +105,19 @@ mergeInto(LibraryManager.library, {
105
105
$_wasmWorkerBlobUrl : "URL.createObjectURL(new Blob(['onmessage=function(d){onmessage=null;d=d.data;{{{ captureModuleArg() }}}{{{ instantiateWasm() }}}importScripts(d.js);{{{ instantiateModule() }}}d.wasm=d.mem=d.js=0;}'],{type:'application/javascript'}))" ,
106
106
#endif
107
107
108
- _emscripten_create_wasm_worker__deps : [ '$_wasmWorkers' , '$_wasmWorkersID' , '$_wasmWorkerAppendToQueue' , '$_wasmWorkerRunPostMessage'
108
+ _emscripten_create_wasm_worker__deps : [ '$_wasmWorkers' , '$_wasmWorkerAppendToQueue' ,
109
+ '$_wasmWorkerRunPostMessage' ,
109
110
#if WASM_WORKERS == 2
110
- , '$_wasmWorkerBlobUrl'
111
+ '$_wasmWorkerBlobUrl' ,
111
112
#endif
112
113
] ,
113
- _emscripten_create_wasm_worker__postset : 'if (ENVIRONMENT_IS_WASM_WORKER) {\n'
114
- + '_wasmWorkers[0] = this;\n'
115
- + 'addEventListener("message", _wasmWorkerAppendToQueue);\n'
116
- + '}\n' ,
114
+ _emscripten_create_wasm_worker__postset : `
115
+ if (ENVIRONMENT_IS_WASM_WORKER) {
116
+ _wasmWorkers.allocated[0] = this;
117
+ addEventListener('message', _wasmWorkerAppendToQueue);
118
+ }` ,
117
119
_emscripten_create_wasm_worker : function ( stackLowestAddress , stackSize ) {
118
- let worker = _wasmWorkers [ _wasmWorkersID ] = new Worker (
120
+ let worker = new Worker (
119
121
#if WASM_WORKERS == 2 // WASM_WORKERS=2 mode embeds .ww.js file contents into the main .js file as a Blob URL. (convenient, but not CSP security safe, since this is eval-like)
120
122
_wasmWorkerBlobUrl
121
123
#elif MINIMAL_RUNTIME // MINIMAL_RUNTIME has a structure where the .ww.js file is loaded from the main HTML file in parallel to all other files for best performance
@@ -124,9 +126,13 @@ mergeInto(LibraryManager.library, {
124
126
locateFile ( '{{{ WASM_WORKER_FILE }}}' )
125
127
#endif
126
128
) ;
129
+ var id = _wasmWorkers . allocate ( worker ) ;
130
+ #if RUNTIME_DEBUG
131
+ dbg ( 'new wasm worker -> ' + id ) ;
132
+ #endif
127
133
// Craft the Module object for the Wasm Worker scope:
128
134
worker . postMessage ( {
129
- '$ww' : _wasmWorkersID , // Signal with a non-zero value that this Worker will be a Wasm Worker, and not the main browser thread.
135
+ '$ww' : id , // Signal with a non-zero value that this Worker will be a Wasm Worker, and not the main browser thread.
130
136
#if MINIMAL_RUNTIME
131
137
'wasm' : Module [ 'wasm' ] ,
132
138
'js' : Module [ 'js' ] ,
@@ -140,27 +146,35 @@ mergeInto(LibraryManager.library, {
140
146
'sz' : stackSize , // sz = stack size
141
147
} ) ;
142
148
worker . onmessage = _wasmWorkerRunPostMessage ;
143
- return _wasmWorkersID ++ ;
149
+ return id ;
144
150
} ,
145
151
146
152
emscripten_terminate_wasm_worker : function ( id ) {
147
153
#if ASSERTIONS
148
154
assert ( id != 0 , 'emscripten_terminate_wasm_worker() cannot be called with id=0!' ) ;
149
155
#endif
150
- if ( _wasmWorkers [ id ] ) {
151
- _wasmWorkers [ id ] . terminate ( ) ;
152
- delete _wasmWorkers [ id ] ;
156
+ if ( _wasmWorkers . has ( id ) ) {
157
+ #if RUNTIME_DEBUG
158
+ dbg ( 'terminating wasm worker -> ' + id ) ;
159
+ #endif
160
+ _wasmWorkers . get ( id ) . terminate ( ) ;
161
+ _wasmWorkers . free ( id ) ;
153
162
}
154
163
} ,
155
164
156
165
emscripten_terminate_all_wasm_workers : function ( ) {
157
166
#if ASSERTIONS
158
167
assert ( ! ENVIRONMENT_IS_WASM_WORKER , 'emscripten_terminate_all_wasm_workers() cannot be called from a Wasm Worker: only the main browser thread has visibility to terminate all Workers!' ) ;
159
168
#endif
160
- Object . values ( _wasmWorkers ) . forEach ( ( worker ) => {
161
- worker . terminate ( ) ;
169
+ #if RUNTIME_DEBUG
170
+ dbg ( 'emscripten_terminate_all_wasm_workers' ) ;
171
+ #endif
172
+ Object . values ( _wasmWorkers . allocated ) . forEach ( ( worker ) => {
173
+ if ( typeof worker !== 'undefined' ) {
174
+ worker . terminate ( ) ;
175
+ }
162
176
} ) ;
163
- _wasmWorkers = { } ;
177
+ _wasmWorkers = new HandleAllocator ( ) ;
164
178
} ,
165
179
166
180
emscripten_current_thread_is_wasm_worker : function ( ) {
@@ -176,27 +190,27 @@ mergeInto(LibraryManager.library, {
176
190
} ,
177
191
178
192
emscripten_wasm_worker_post_function_v : function ( id , funcPtr ) {
179
- _wasmWorkers [ id ] . postMessage ( { '_wsc' : funcPtr , 'x' : [ ] } ) ; // "WaSm Call"
193
+ _wasmWorkers . get ( id ) . postMessage ( { '_wsc' : funcPtr , 'x' : [ ] } ) ; // "WaSm Call"
180
194
} ,
181
195
182
196
$_wasmWorkerPostFunction1__sig : 'vipd' ,
183
197
$_wasmWorkerPostFunction1 : function ( id , funcPtr , arg0 ) {
184
- _wasmWorkers [ id ] . postMessage ( { '_wsc' : funcPtr , 'x' : [ arg0 ] } ) ; // "WaSm Call"
198
+ _wasmWorkers . get ( id ) . postMessage ( { '_wsc' : funcPtr , 'x' : [ arg0 ] } ) ; // "WaSm Call"
185
199
} ,
186
200
187
201
emscripten_wasm_worker_post_function_vi : '$_wasmWorkerPostFunction1' ,
188
202
emscripten_wasm_worker_post_function_vd : '$_wasmWorkerPostFunction1' ,
189
203
190
204
$_wasmWorkerPostFunction2__sig : 'vipdd' ,
191
205
$_wasmWorkerPostFunction2 : function ( id , funcPtr , arg0 , arg1 ) {
192
- _wasmWorkers [ id ] . postMessage ( { '_wsc' : funcPtr , 'x' : [ arg0 , arg1 ] } ) ; // "WaSm Call"
206
+ _wasmWorkers . get ( id ) . postMessage ( { '_wsc' : funcPtr , 'x' : [ arg0 , arg1 ] } ) ; // "WaSm Call"
193
207
} ,
194
208
emscripten_wasm_worker_post_function_vii : '$_wasmWorkerPostFunction2' ,
195
209
emscripten_wasm_worker_post_function_vdd : '$_wasmWorkerPostFunction2' ,
196
210
197
211
$_wasmWorkerPostFunction3__sig : 'vipddd' ,
198
212
$_wasmWorkerPostFunction3 : function ( id , funcPtr , arg0 , arg1 , arg2 ) {
199
- _wasmWorkers [ id ] . postMessage ( { '_wsc' : funcPtr , 'x' : [ arg0 , arg1 , arg2 ] } ) ; // "WaSm Call"
213
+ _wasmWorkers . get ( id ) . postMessage ( { '_wsc' : funcPtr , 'x' : [ arg0 , arg1 , arg2 ] } ) ; // "WaSm Call"
200
214
} ,
201
215
emscripten_wasm_worker_post_function_viii : '$_wasmWorkerPostFunction3' ,
202
216
emscripten_wasm_worker_post_function_vddd : '$_wasmWorkerPostFunction3' ,
@@ -210,7 +224,7 @@ mergeInto(LibraryManager.library, {
210
224
assert ( UTF8ToString ( sigPtr ) [ 0 ] != 'v' , 'Do NOT specify the return argument in the signature string for a call to emscripten_wasm_worker_post_function_sig(), just pass the function arguments.' ) ;
211
225
assert ( varargs ) ;
212
226
#endif
213
- _wasmWorkers [ id ] . postMessage ( { '_wsc' : funcPtr , 'x' : readEmAsmArgs ( sigPtr , varargs ) } ) ;
227
+ _wasmWorkers . get ( id ) . postMessage ( { '_wsc' : funcPtr , 'x' : readEmAsmArgs ( sigPtr , varargs ) } ) ;
214
228
} ,
215
229
216
230
$atomicWaitStates : "['ok', 'not-equal', 'timed-out']" ,
0 commit comments