@@ -600,13 +600,43 @@ var SyscallsLibrary = {
600600 return 0 ;
601601 } ,
602602 __syscall__newselect__i53abi : true ,
603- __syscall__newselect__deps : [ '$parseSelectFDSet' ] ,
603+ __syscall__newselect__proxy : 'none ',
604+ __syscall__newselect__deps : [ '$doNewselect' ,
605+ #if PTHREADS
606+ '_emscripten_proxy_newselect' ,
607+ #endif
608+ ] ,
604609 __syscall__newselect : ( nfds , readfds , writefds , exceptfds , timeoutInMillis ) = > {
610+ #if PTHREADS
611+ if ( ENVIRONMENT_IS_PTHREAD ) {
612+ return __emscripten_proxy_newselect ( nfds ,
613+ { { { to64 ( 'readfds' ) } } } ,
614+ { { { to64 ( 'writefds' ) } } } ,
615+ { { { to64 ( 'exceptfds' ) } } } ,
616+ { { { splitI64 ( 'timeoutInMillis' ) } } } ) ;
617+ }
618+ #endif
619+ return doNewselect ( null , null, nfds, readfds, writefds, exceptfds, timeoutInMillis) ;
620+ } ,
621+ _newselect_js__i53abi : true ,
622+ _newselect_js__proxy : 'none' ,
623+ _newselect_js__deps : [ '$doNewselect' ] ,
624+ _newselect_js : ( ctx , arg , nfds , readfds , writefds , exceptfds , timeoutInMillis ) = > {
625+ return doNewselect ( ctx , arg , nfds , readfds , writefds , exceptfds , timeoutInMillis ) ;
626+ } ,
627+ $doNewselect__proxy : 'none' ,
628+ $doNewselect__deps : [ '$parseSelectFDSet' ,
629+ #if PTHREADS
630+ '_emscripten_proxy_newselect_finish' ,
631+ #endif
632+ ] ,
633+ $doNewselect : ( ctx , arg , nfds , readfds , writefds , exceptfds , timeoutInMillis ) = > {
605634 // readfds are supported,
606635 // writefds checks socket open status
607636 // exceptfds are supported, although on web, such exceptional conditions never arise in web sockets
608637 // and so the exceptfds list will always return empty.
609- // timeout is supported, although on SOCKFS and PIPEFS these are ignored and always treated as 0 - fully async
638+ // timeout is supported, although on SOCKFS these are ignored and always treated as 0 - fully async
639+ // and PIPEFS supports timeout only when the select is called from a worker.
610640#if ASSERTIONS
611641 assert ( nfds <= 64 , 'nfds must be less than or equal to 64' ) ; // fd sets have 64 bits // TODO: this could be 1024 based on current musl headers
612642#endif
@@ -618,6 +648,36 @@ var SyscallsLibrary = {
618648
619649 var check = ( fd , low , high , val ) => fd < 32 ? ( low & val ) : ( high & val ) ;
620650
651+ #if PTHREADS
652+ var makeNotifyCallback = null ;
653+ if ( ctx ) {
654+ // Enable event handlers only when the select call is proxied from a worker.
655+ var cleanupFuncs = [ ] ;
656+ var notifyDone = false ;
657+ makeNotifyCallback = ( fd ) => {
658+ var cb = ( flags ) => {
659+ if ( notifyDone ) {
660+ return ;
661+ }
662+ if ( fd >= 0 ) {
663+ fdSet . setFlags ( fd , flags ) ;
664+ }
665+ notifyDone = true ;
666+ cleanupFuncs . forEach ( cb => cb ( ) ) ;
667+ fdSet . commit ( ) ;
668+ __emscripten_proxy_newselect_finish ( { { { to64 ( 'ctx' ) } } } , { { { to64 ( 'arg' ) } } } , fdSet . getTotal ( ) ) ;
669+ }
670+ cb . registerCleanupFunc = ( f ) => {
671+ if ( f != null ) cleanupFuncs . push ( f ) ;
672+ }
673+ return cb ;
674+ }
675+ if ( timeoutInMillis > 0 ) {
676+ setTimeout ( ( ) => makeNotifyCallback ( - 1 ) ( 0 ) , timeoutInMillis ) ;
677+ }
678+ }
679+ #endif
680+
621681 for ( var fd = 0 ; fd < nfds ; fd ++ ) {
622682 var mask = 1 << ( fd % 32 ) ;
623683 if ( ! ( check ( fd , allLow , allHigh , mask ) ) ) {
@@ -629,7 +689,14 @@ var SyscallsLibrary = {
629689 var flags = SYSCALLS . DEFAULT_POLLMASK ;
630690
631691 if ( stream . stream_ops . poll ) {
632- flags = stream . stream_ops . poll ( stream , timeoutInMillis ) ;
692+ flags = ( ( ) => {
693+ #if PTHREADS
694+ if ( makeNotifyCallback != null ) {
695+ return stream . stream_ops . poll ( stream , timeoutInMillis , timeoutInMillis != 0 ? makeNotifyCallback ( fd ) : null ) ;
696+ }
697+ #endif
698+ return stream . stream_ops . poll ( stream , timeoutInMillis ) ;
699+ } ) ( ) ;
633700 } else {
634701#if ASSERTIONS
635702 if ( timeoutInMillis != 0 ) warnOnce ( 'non-zero select() timeout not supported: ' + timeoutInMillis )
@@ -639,6 +706,14 @@ var SyscallsLibrary = {
639706 fdSet . setFlags ( fd , flags ) ;
640707 }
641708
709+ #if PTHREADS
710+ if ( makeNotifyCallback != null ) {
711+ if ( ( fdSet . getTotal ( ) > 0 ) || ( timeoutInMillis == 0 ) ) {
712+ makeNotifyCallback ( - 1 ) ( 0 ) ;
713+ }
714+ return 0 ;
715+ }
716+ #endif
642717
643718 fdSet . commit ( ) ;
644719
0 commit comments