66#include "libc_wasi_wrapper.h"
77#include "bh_platform.h"
88#include "wasm_export.h"
9+ #include "wasm_runtime_common.h"
10+
11+ #if WASM_ENABLE_THREAD_MGR != 0
12+ #include "../../../thread-mgr/thread_manager.h"
13+ #endif
914
1015void
1116wasm_runtime_set_exception (wasm_module_inst_t module , const char * exception );
@@ -46,19 +51,7 @@ typedef struct iovec_app {
4651 uint32 buf_len ;
4752} iovec_app_t ;
4853
49- typedef struct WASIContext {
50- struct fd_table * curfds ;
51- struct fd_prestats * prestats ;
52- struct argv_environ_values * argv_environ ;
53- struct addr_pool * addr_pool ;
54- char * ns_lookup_buf ;
55- char * * ns_lookup_list ;
56- char * argv_buf ;
57- char * * argv_list ;
58- char * env_buf ;
59- char * * env_list ;
60- uint32_t exit_code ;
61- } * wasi_ctx_t ;
54+ typedef struct WASIContext * wasi_ctx_t ;
6255
6356wasi_ctx_t
6457wasm_runtime_get_wasi_ctx (wasm_module_inst_t module_inst );
@@ -989,13 +982,12 @@ update_clock_subscription_data(wasi_subscription_t *in, uint32 nsubscriptions,
989982}
990983
991984static wasi_errno_t
992- execute_interruptible_poll_oneoff (wasm_module_inst_t module_inst ,
985+ execute_interruptible_poll_oneoff (
993986#if !defined(WASMTIME_SSP_STATIC_CURFDS )
994- struct fd_table * curfds ,
987+ struct fd_table * curfds ,
995988#endif
996- const __wasi_subscription_t * in ,
997- __wasi_event_t * out , size_t nsubscriptions ,
998- size_t * nevents )
989+ const __wasi_subscription_t * in , __wasi_event_t * out , size_t nsubscriptions ,
990+ size_t * nevents , wasm_exec_env_t exec_env )
999991{
1000992 if (nsubscriptions == 0 ) {
1001993 * nevents = 0 ;
@@ -1004,6 +996,8 @@ execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst,
1004996
1005997 wasi_errno_t err ;
1006998 __wasi_timestamp_t elapsed = 0 ;
999+ bool all_outs_are_type_clock ;
1000+ uint32 i ;
10071001
10081002 const __wasi_timestamp_t timeout = get_timeout_for_poll_oneoff (
10091003 in , nsubscriptions ),
@@ -1021,25 +1015,35 @@ execute_interruptible_poll_oneoff(wasm_module_inst_t module_inst,
10211015 bh_memcpy_s (in_copy , size_to_copy , in , size_to_copy );
10221016
10231017 while (timeout == (__wasi_timestamp_t )- 1 || elapsed <= timeout ) {
1024- elapsed += time_quant ;
1025-
10261018 /* update timeout for clock subscription events */
10271019 update_clock_subscription_data (in_copy , nsubscriptions ,
10281020 min (time_quant , timeout - elapsed ));
10291021 err = wasmtime_ssp_poll_oneoff (curfds , in_copy , out , nsubscriptions ,
10301022 nevents );
1023+ elapsed += time_quant ;
1024+
10311025 if (err ) {
10321026 wasm_runtime_free (in_copy );
10331027 return err ;
10341028 }
10351029
1036- if (wasm_runtime_get_exception ( module_inst ) || * nevents > 0 ) {
1030+ if (wasm_cluster_is_thread_terminated ( exec_env ) ) {
10371031 wasm_runtime_free (in_copy );
1032+ return EINTR ;
1033+ }
1034+ else if (* nevents > 0 ) {
1035+ all_outs_are_type_clock = true;
1036+ for (i = 0 ; i < * nevents ; i ++ ) {
1037+ if (out [i ].type != __WASI_EVENTTYPE_CLOCK ) {
1038+ all_outs_are_type_clock = false;
1039+ break ;
1040+ }
1041+ }
10381042
1039- if (* nevents ) {
1043+ if (!all_outs_are_type_clock ) {
1044+ wasm_runtime_free (in_copy );
10401045 return __WASI_ESUCCESS ;
10411046 }
1042- return EINTR ;
10431047 }
10441048 }
10451049
@@ -1069,8 +1073,8 @@ wasi_poll_oneoff(wasm_exec_env_t exec_env, const wasi_subscription_t *in,
10691073#if WASM_ENABLE_THREAD_MGR == 0
10701074 err = wasmtime_ssp_poll_oneoff (curfds , in , out , nsubscriptions , & nevents );
10711075#else
1072- err = execute_interruptible_poll_oneoff (module_inst , curfds , in , out ,
1073- nsubscriptions , & nevents );
1076+ err = execute_interruptible_poll_oneoff (curfds , in , out , nsubscriptions ,
1077+ & nevents , exec_env );
10741078#endif
10751079 if (err )
10761080 return err ;
0 commit comments