From 050fba7213eae15b1fc05e8f6776112ce28189e0 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 11 Oct 2019 03:30:00 +0000 Subject: [PATCH] emulated non-blocking pipe read with PeekNamedPipe() in win32 --- moo/kernel/test-bi.moo | 32 +++++++++++++++++--------------- moo/lib/poll-msw.c | 2 +- moo/lib/std.c | 28 ++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/moo/kernel/test-bi.moo b/moo/kernel/test-bi.moo index 4021d0e..52f74df 100644 --- a/moo/kernel/test-bi.moo +++ b/moo/kernel/test-bi.moo @@ -40,11 +40,13 @@ class MyObject(Object) | ffi now | - [ ffi := FFI new: "libc.so.6". ] on: Exception do: [:ex | ffi := FFI new: "libc.so" ]. + [ ffi := FFI new: "libc.so.6". ] on: Exception do: [:ex | + [ ffi := FFI new: "libc.so" ] on: Exception do: [ :ex2 | ffi := FFI new: "msvcrt.dll" ] + ]. now := ffi call: #time signature: "l>i" arguments: #(0). - ////ffi call: #srand signature: "i>" arguments: ##(now). - ffi call: #srandom signature: "i>" arguments: ##(now). + ffi call: #srand signature: "i>" arguments: ##(now). + ///ffi call: #srandom signature: "i>" arguments: ##(now). [ | i q r divd divr divd_ubound divr_ubound x | @@ -55,25 +57,25 @@ class MyObject(Object) while (true) { - //x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20. - //divd := (ffi call: #rand signature: ">i" arguments: nil). - x := (ffi call: #random signature: ">l" arguments: nil) rem: 20. - divd := (ffi call: #random signature: ">l" arguments: nil). + x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20. + divd := (ffi call: #rand signature: ">i" arguments: nil). + ///x := (ffi call: #random signature: ">l" arguments: nil) rem: 20. + ///divd := (ffi call: #random signature: ">l" arguments: nil). while (x > 0) { - //divd := (divd bitShift: 7) bitOr: (ffi call: #rand signature: ">i" arguments: nil). - divd := (divd bitShift: 7) bitOr: (ffi call: #random signature: ">l" arguments: nil). + divd := (divd bitShift: 7) bitOr: (ffi call: #rand signature: ">i" arguments: nil). + ///divd := (divd bitShift: 7) bitOr: (ffi call: #random signature: ">l" arguments: nil). x := x - 1. }. - //x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20. - //divr := (ffi call: #rand signature: ">i" arguments: nil). - x := (ffi call: #random signature: ">l" arguments: nil) rem: 20. - divr := (ffi call: #random signature: ">l" arguments: nil). + x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20. + divr := (ffi call: #rand signature: ">i" arguments: nil). + ///x := (ffi call: #random signature: ">l" arguments: nil) rem: 20. + ///divr := (ffi call: #random signature: ">l" arguments: nil). while (x > 0) { - //divr := (divr bitShift: 7) bitOr: (ffi call: #rand signature: ">i" arguments: nil). - divr := (divr bitShift: 7) bitOr: (ffi call: #random signature: ">l" arguments: nil). + divr := (divr bitShift: 7) bitOr: (ffi call: #rand signature: ">i" arguments: nil). + //divr := (divr bitShift: 7) bitOr: (ffi call: #random signature: ">l" arguments: nil). x := x - 1. }. if (divr = 0) { divr := 1 }. diff --git a/moo/lib/poll-msw.c b/moo/lib/poll-msw.c index 6fc93e2..c96d3fa 100644 --- a/moo/lib/poll-msw.c +++ b/moo/lib/poll-msw.c @@ -135,7 +135,7 @@ static int windows_compute_revents (HANDLE h, int *p_sought) } happened = 0; - if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0) + if (PeekNamedPipe(h, NULL, 0, NULL, &avail, NULL) != 0) { if (avail) happened |= *p_sought & (POLLIN | POLLRDNORM); diff --git a/moo/lib/std.c b/moo/lib/std.c index 2439352..0010523 100644 --- a/moo/lib/std.c +++ b/moo/lib/std.c @@ -49,6 +49,7 @@ # include # include # include +# include # include # if defined(MOO_HAVE_CFG_H) && defined(MOO_ENABLE_LIBLTDL) # include @@ -2233,7 +2234,7 @@ static int open_pipes (moo_t* moo, int p[2]) int flags; #if defined(_WIN32) - if (_pipe(p, 256, _O_BINARY) == -1) + if (_pipe(p, 256, _O_BINARY | _O_NOINHERIT) == -1) #elif defined(HAVE_PIPE2) && defined(O_CLOEXEC) && defined(O_NONBLOCK) if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) #else @@ -3143,12 +3144,23 @@ static moo_ooi_t vm_getsigfd (moo_t* moo) static int vm_getsig (moo_t* moo, moo_uint8_t* u8) { xtn_t* xtn = GET_XTN(moo); +#if defined(_WIN32) + /* TODO: can i make the pipe non-block in win32? */ + DWORD navail; + if (PeekNamedPipe(_get_osfhandle(xtn->sigfd.p[0]), MOO_NULL, 0, MOO_NULL, &navail, MOO_NULL) == 0) + { + moo_seterrwithsyserr (moo, 1, GetLastError()); + return -1; + } + if (navail <= 0) return 0; +#endif if (read(xtn->sigfd.p[0], u8, MOO_SIZEOF(*u8)) == -1) { if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) return 0; moo_seterrwithsyserr (moo, 0, errno); return -1; } + return 1; } @@ -4173,7 +4185,7 @@ static BOOL WINAPI handle_term (DWORD ctrl_type) { if (ctrl_type == CTRL_C_EVENT || ctrl_type == CTRL_CLOSE_EVENT) { - abort_all_moos (0); + abort_all_moos (SIGINT); return TRUE; } @@ -4206,7 +4218,7 @@ static ULONG APIENTRY handle_term ( p1->ExceptionInfo[0] == XCPT_SIGNAL_KILLPROC || p1->ExceptionInfo[0] == XCPT_SIGNAL_BREAK) { - abort_all_moos (0); + abort_all_moos (SIGINT); return (DosAcknowledgeSignalException(p1->ExceptionInfo[0]) != NO_ERROR)? 1: XCPT_CONTINUE_EXECUTION; } } @@ -4246,7 +4258,7 @@ static void __interrupt dos_int23_handler (void) /* prevent the DOS interrupt handler from being called */ _XSTACK* stk = (_XSTACK*)_get_stk_frame(); stk->opts |= _STK_NOINT; - abort_all_moos (0); + abort_all_moos (SIGINT); /* if i call the previous handler, it's likely to kill the application. * so i don't chain-call the previous handler. but another call could * have changed the handler already to something else. then it would be @@ -4279,13 +4291,13 @@ static void __interrupt dos_int23_handler (void) /* key release */ sc = sc & 0x7F; keyboard[sc] = 0; - printf ("%key released ... %x\n", sc); + /*printf ("%key released ... %x\n", sc);*/ } else { keyboard[sc] = 1; - printf ("%key pressed ... %x %c\n", sc, sc); - abort_all_moos (0); + /*printf ("%key pressed ... %x %c\n", sc, sc);*/ + abort_all_moos (SIGINT); } extended = 0; @@ -4294,7 +4306,7 @@ static void __interrupt dos_int23_handler (void) /*_chain_intr (dos_prev_int23_handler);*/ outp (0x20, 0x20); #else - abort_all_moos (0); + abort_all_moos (SIGINT); _chain_intr (dos_prev_int23_handler); #endif #endif