@@ -120,7 +120,6 @@ var LibraryPThread = {
120
120
noExitRuntime = false ;
121
121
#endif
122
122
} ,
123
-
124
123
initMessageRelay : function ( ) {
125
124
// Spawn a dedicated worker for passing messages between threads. Instead
126
125
// of having each thread hold a message port for every other thread, they
@@ -145,7 +144,6 @@ Object.assign(global, {
145
144
` ;
146
145
}
147
146
#endif
148
-
149
147
relayCode += '(' + ( ( ) => {
150
148
// Map pthread IDs to message ports we use to communicate with those
151
149
// pthreads.
@@ -158,7 +156,7 @@ Object.assign(global, {
158
156
function handleMessage ( msg ) {
159
157
const thread = msg . data . targetThread ;
160
158
const port = threadPorts . get ( thread ) ;
161
- if ( port !== undefined ) {
159
+ if ( port ) {
162
160
console . log ( "forwarding message to" , thread ) ;
163
161
port . postMessage ( msg . data , msg . data . transferList ) ;
164
162
} else {
@@ -183,29 +181,33 @@ Object.assign(global, {
183
181
bufferedMessages . get ( thread ) . forEach ( handleMessage ) ;
184
182
bufferedMessages . delete ( thread ) ;
185
183
}
186
- return ;
187
- }
188
- if ( cmd === 'destroy' ) {
184
+ } else if ( cmd === 'destroy' ) {
189
185
bufferedMessages . delete ( thread ) ;
190
186
}
187
+ #if ASSERTIONS
188
+ else {
189
+ console . error ( 'unrecognized message relay command:' , cmd ) ;
190
+ }
191
+ #endif
191
192
} ;
192
193
} ) . toString ( ) + ')()' ;
193
194
194
- var base64 ;
195
195
#if ENVIRONMENT_MAY_BE_NODE
196
196
if ( ENVIRONMENT_IS_NODE ) {
197
197
// TODO: Node 16+ has btoa, so remove this when we drop support for
198
198
// older Nodes.
199
- base64 = ( s ) => { return Buffer . from ( s ) . toString ( 'base64' ) ; } ;
200
- } else {
201
- base64 = btoa ;
199
+ global . btoa = ( s ) => { return Buffer . from ( s ) . toString ( 'base64' ) ; } ;
202
200
}
203
- #else
204
- base64 = btoa ;
205
201
#endif
206
-
207
202
PThread . messageRelay = new Worker ( new URL (
208
- 'data:text/javascript;base64,' + base64 ( relayCode ) ) ) ;
203
+ 'data:text/javascript;base64,' + btoa ( relayCode ) ) ) ;
204
+ #if ENVIRONMENT_MAY_BE_NODE
205
+ if ( ENVIRONMENT_IS_NODE ) {
206
+ // Do not keep Node alive if the message relay is the only thing
207
+ // running.
208
+ PThread . messageRelay . unref ( ) ;
209
+ }
210
+ #endif
209
211
} ,
210
212
211
213
#if PTHREADS_PROFILING
@@ -336,9 +338,9 @@ Object.assign(global, {
336
338
// accessible variable about the thread that initiated the proxying.
337
339
if ( worker . pthread_ptr ) PThread . currentProxiedOperationCallerThread = worker . pthread_ptr ;
338
340
339
- if ( d [ 'targetThread' ] && d [ 'targetThread' ] != _pthread_self ( ) ) {
340
- abort ( "unexpected message intended for thread" + d [ 'targetThread' ] ) ;
341
- }
341
+ # if ASSERTIONS
342
+ assert ( ! d [ 'targetThread' ] || d [ 'targetThread' ] == _pthread_self ( ) ) ;
343
+ #endif
342
344
343
345
if ( cmd === 'processProxyingQueue' ) {
344
346
executeNotifiedProxyingQueue ( d [ 'queue' ] ) ;
@@ -593,7 +595,6 @@ Object.assign(global, {
593
595
}
594
596
} ,
595
597
596
-
597
598
$killThread__deps : [ '_emscripten_thread_free_data' ] ,
598
599
$killThread : function ( pthread_ptr ) {
599
600
#if PTHREADS_DEBUG
@@ -708,18 +709,13 @@ Object.assign(global, {
708
709
709
710
worker . pthread_ptr = threadParams . pthread_ptr ;
710
711
711
- var MsgChannel ;
712
712
#if ENVIRONMENT_MAY_BE_NODE
713
713
if ( ENVIRONMENT_IS_NODE ) {
714
714
// TODO: This isn't necessary in Node 18+
715
- MsgChannel = require ( 'worker_threads' ) . MessageChannel ;
716
- } else {
717
- MsgChannel = MessageChannel ;
715
+ global . MessageChannel = require ( 'worker_threads' ) . MessageChannel ;
718
716
}
719
- #else
720
- MsgChannel = MessageChannel ;
721
717
#endif
722
- var channel = new MsgChannel ( ) ;
718
+ var channel = new MessageChannel ( ) ;
723
719
var msg = {
724
720
'cmd' : 'run' ,
725
721
'start_routine' : threadParams . startRoutine ,
0 commit comments