2
2
/* eslint-disable no-unreachable */
3
3
4
4
declare const PThread : any
5
+ declare let emnapiWorkerPoolSize : number
5
6
declare const emnapiAsyncWorkerQueue : number [ ]
7
+ declare const emnapiRunningWorkers : Worker [ ]
6
8
// declare function __emnapi_execute_async_work (work: number): void
7
9
8
10
mergeInto ( LibraryManager . library , {
11
+ $emnapiWorkerPoolSize : undefined ,
12
+ $emnapiRunningWorkers : [ ] ,
13
+
9
14
$emnapiAsyncWorkerQueue : [ ] ,
10
- $emnapiAsyncWorkerQueue__deps : [ '$PThread' , '_emnapi_execute_async_work' ] ,
11
- $emnapiAsyncWorkerQueue__postset : 'PThread.unusedWorkers.push=function(){' +
12
- 'var r=Array.prototype.push.apply(this,arguments);' +
13
- 'setTimeout(function(){' +
14
- 'if(PThread.unusedWorkers.length>0&&emnapiAsyncWorkerQueue.length>0){' +
15
+ $emnapiAsyncWorkerQueue__deps : [ '$PThread' , '_emnapi_execute_async_work' , '$emnapiRunningWorkers' , '$emnapiWorkerPoolSize' ] ,
16
+ $emnapiAsyncWorkerQueue__postset : 'PThread.unusedWorkers.push = function () {' +
17
+ 'var r = Array.prototype.push.apply(this, arguments);' +
18
+ 'var worker = arguments[0]; var index;' +
19
+ 'if ((index = emnapiRunningWorkers.indexOf(worker)) !== -1) emnapiRunningWorkers.splice(index, 1);' +
20
+ 'setTimeout(function () {' +
21
+ 'var canExecute = PThread.unusedWorkers.length > 0 && emnapiAsyncWorkerQueue.length > 0;' +
22
+ 'if (emnapiWorkerPoolSize > 0) {' +
23
+ 'canExecute = canExecute && (emnapiWorkerPoolSize - emnapiRunningWorkers.length > 0);' +
24
+ '}' +
25
+ 'if (canExecute) {' +
15
26
'__emnapi_execute_async_work(emnapiAsyncWorkerQueue.shift());' +
16
27
'}' +
17
28
'});' +
18
29
'return r;' +
19
30
'};' ,
20
31
21
- _emnapi_get_worker_count_js__deps : [ '$PThread' ] ,
32
+ _emnapi_get_worker_count_js__deps : [ '$PThread' , '$emnapiWorkerPoolSize' , '$emnapiRunningWorkers' ] ,
22
33
_emnapi_get_worker_count_js : function ( struct : number ) {
23
34
$from64 ( 'struct' )
24
35
const address = struct >> 2
25
36
HEAP32 [ address ] = PThread . unusedWorkers . length
26
37
HEAP32 [ address + 1 ] = PThread . runningWorkers . length
38
+ HEAP32 [ address + 2 ] = emnapiWorkerPoolSize - emnapiRunningWorkers . length
27
39
} ,
28
40
29
41
// _emnapi_delete_async_work_js: function (_work: number) {}
@@ -45,6 +57,7 @@ function _emnapi_queue_async_work_js (work: number): void {
45
57
}
46
58
const pthreadValue = PThread . pthreads [ tid ]
47
59
const worker = ( ( 'worker' in pthreadValue ) && ( 'threadInfoStruct' in pthreadValue ) ) ? pthreadValue . worker : pthreadValue
60
+ emnapiRunningWorkers . push ( worker )
48
61
if ( ! worker . _emnapiAsyncWorkListener ) {
49
62
worker . _emnapiAsyncWorkListener = function ( this : Worker , e : MessageEvent < any > ) : any {
50
63
const data = ENVIRONMENT_IS_NODE ? e : e . data
@@ -115,5 +128,5 @@ function napi_cancel_async_work (env: napi_env, work: number): napi_status {
115
128
// #endif
116
129
}
117
130
118
- emnapiImplement ( '_emnapi_queue_async_work_js' , _emnapi_queue_async_work_js , [ '$PThread' , '$emnapiAsyncWorkerQueue' , '$emnapiGetDynamicCalls' ] )
131
+ emnapiImplement ( '_emnapi_queue_async_work_js' , _emnapi_queue_async_work_js , [ '$PThread' , '$emnapiAsyncWorkerQueue' , '$emnapiGetDynamicCalls' , '$emnapiRunningWorkers' ] )
119
132
emnapiImplement ( 'napi_cancel_async_work' , napi_cancel_async_work , [ '$emnapiAsyncWorkerQueue' , '$emnapiGetDynamicCalls' , 'napi_set_last_error' ] )
0 commit comments