emulated non-blocking pipe read with PeekNamedPipe() in win32

This commit is contained in:
hyunghwan.chung 2019-10-11 03:30:00 +00:00
parent 1a10655784
commit 050fba7213
3 changed files with 38 additions and 24 deletions

View File

@ -40,11 +40,13 @@ class MyObject(Object)
| ffi now | | 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). now := ffi call: #time signature: "l>i" arguments: #(0).
////ffi call: #srand signature: "i>" arguments: ##(now). ffi call: #srand signature: "i>" arguments: ##(now).
ffi call: #srandom signature: "i>" arguments: ##(now). ///ffi call: #srandom signature: "i>" arguments: ##(now).
[ [
| i q r divd divr divd_ubound divr_ubound x | | i q r divd divr divd_ubound divr_ubound x |
@ -55,25 +57,25 @@ class MyObject(Object)
while (true) while (true)
{ {
//x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20. x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20.
//divd := (ffi call: #rand signature: ">i" arguments: nil). divd := (ffi call: #rand signature: ">i" arguments: nil).
x := (ffi call: #random signature: ">l" arguments: nil) rem: 20. ///x := (ffi call: #random signature: ">l" arguments: nil) rem: 20.
divd := (ffi call: #random signature: ">l" arguments: nil). ///divd := (ffi call: #random signature: ">l" arguments: nil).
while (x > 0) while (x > 0)
{ {
//divd := (divd bitShift: 7) bitOr: (ffi call: #rand signature: ">i" 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). ///divd := (divd bitShift: 7) bitOr: (ffi call: #random signature: ">l" arguments: nil).
x := x - 1. x := x - 1.
}. }.
//x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20. x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20.
//divr := (ffi call: #rand signature: ">i" arguments: nil). divr := (ffi call: #rand signature: ">i" arguments: nil).
x := (ffi call: #random signature: ">l" arguments: nil) rem: 20. ///x := (ffi call: #random signature: ">l" arguments: nil) rem: 20.
divr := (ffi call: #random signature: ">l" arguments: nil). ///divr := (ffi call: #random signature: ">l" arguments: nil).
while (x > 0) while (x > 0)
{ {
//divr := (divr bitShift: 7) bitOr: (ffi call: #rand signature: ">i" 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). //divr := (divr bitShift: 7) bitOr: (ffi call: #random signature: ">l" arguments: nil).
x := x - 1. x := x - 1.
}. }.
if (divr = 0) { divr := 1 }. if (divr = 0) { divr := 1 }.

View File

@ -49,6 +49,7 @@
# include <time.h> # include <time.h>
# include <io.h> # include <io.h>
# include <fcntl.h> # include <fcntl.h>
# include <signal.h>
# include <errno.h> # include <errno.h>
# if defined(MOO_HAVE_CFG_H) && defined(MOO_ENABLE_LIBLTDL) # if defined(MOO_HAVE_CFG_H) && defined(MOO_ENABLE_LIBLTDL)
# include <ltdl.h> # include <ltdl.h>
@ -2233,7 +2234,7 @@ static int open_pipes (moo_t* moo, int p[2])
int flags; int flags;
#if defined(_WIN32) #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) #elif defined(HAVE_PIPE2) && defined(O_CLOEXEC) && defined(O_NONBLOCK)
if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1) if (pipe2(p, O_CLOEXEC | O_NONBLOCK) == -1)
#else #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) static int vm_getsig (moo_t* moo, moo_uint8_t* u8)
{ {
xtn_t* xtn = GET_XTN(moo); 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 (read(xtn->sigfd.p[0], u8, MOO_SIZEOF(*u8)) == -1)
{ {
if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) return 0; if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) return 0;
moo_seterrwithsyserr (moo, 0, errno); moo_seterrwithsyserr (moo, 0, errno);
return -1; return -1;
} }
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) if (ctrl_type == CTRL_C_EVENT || ctrl_type == CTRL_CLOSE_EVENT)
{ {
abort_all_moos (0); abort_all_moos (SIGINT);
return TRUE; return TRUE;
} }
@ -4206,7 +4218,7 @@ static ULONG APIENTRY handle_term (
p1->ExceptionInfo[0] == XCPT_SIGNAL_KILLPROC || p1->ExceptionInfo[0] == XCPT_SIGNAL_KILLPROC ||
p1->ExceptionInfo[0] == XCPT_SIGNAL_BREAK) 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; 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 */ /* prevent the DOS interrupt handler from being called */
_XSTACK* stk = (_XSTACK*)_get_stk_frame(); _XSTACK* stk = (_XSTACK*)_get_stk_frame();
stk->opts |= _STK_NOINT; 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. /* 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 * 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 * 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 */ /* key release */
sc = sc & 0x7F; sc = sc & 0x7F;
keyboard[sc] = 0; keyboard[sc] = 0;
printf ("%key released ... %x\n", sc); /*printf ("%key released ... %x\n", sc);*/
} }
else else
{ {
keyboard[sc] = 1; keyboard[sc] = 1;
printf ("%key pressed ... %x %c\n", sc, sc); /*printf ("%key pressed ... %x %c\n", sc, sc);*/
abort_all_moos (0); abort_all_moos (SIGINT);
} }
extended = 0; extended = 0;
@ -4294,7 +4306,7 @@ static void __interrupt dos_int23_handler (void)
/*_chain_intr (dos_prev_int23_handler);*/ /*_chain_intr (dos_prev_int23_handler);*/
outp (0x20, 0x20); outp (0x20, 0x20);
#else #else
abort_all_moos (0); abort_all_moos (SIGINT);
_chain_intr (dos_prev_int23_handler); _chain_intr (dos_prev_int23_handler);
#endif #endif
#endif #endif