Fix Windows monitors, implement new io.pipe protocol

db4
U-SLAVA-DFB8FF805\Slava 2008-05-06 02:10:17 -05:00
parent 598ba7dedb
commit cbf886f17d
6 changed files with 60 additions and 104 deletions

2
extra/io/pipes/pipes-tests.factor Normal file → Executable file
View File

@ -14,7 +14,7 @@ IN: io.pipes.tests
[ { f } ] [ { [ f ] } run-pipeline ] unit-test [ { f } ] [ { [ f ] } run-pipeline ] unit-test
[ { "Hello" } ] [ [ { "Hello" } ] [
"Hello" [ "Hello" [
{ [ input-stream [ utf8 <decoder> ] change readln ] } with-pipeline { [ input-stream [ utf8 <decoder> ] change readln ] } run-pipeline
] with-string-reader ] with-string-reader
] unit-test ] unit-test

View File

@ -1,7 +1,7 @@
! Copyright (C) 2007, 2008 Doug Coleman, Slava Pestov. ! Copyright (C) 2007, 2008 Doug Coleman, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: alien alien.c-types arrays continuations destructors io USING: alien alien.c-types arrays continuations destructors io
io.windows libc io.nonblocking windows.types io.windows libc io.nonblocking io.pipes windows.types
math windows.kernel32 windows namespaces io.launcher kernel math windows.kernel32 windows namespaces io.launcher kernel
sequences windows.errors assocs splitting system strings sequences windows.errors assocs splitting system strings
io.windows.launcher io.windows.nt.pipes io.backend io.files io.windows.launcher io.windows.nt.pipes io.backend io.files
@ -19,15 +19,25 @@ IN: io.windows.nt.launcher
DuplicateHandle win32-error=0/f DuplicateHandle win32-error=0/f
] keep *void* ; ] keep *void* ;
! /dev/null simulation
: null-input ( -- pipe )
(pipe) [ in>> handle>> ] [ out>> close-handle ] bi ;
: null-output ( -- pipe )
(pipe) [ in>> close-handle ] [ out>> handle>> ] bi ;
: null-pipe ( mode -- pipe )
{
{ GENERIC_READ [ null-input ] }
{ GENERIC_WRITE [ null-output ] }
} case ;
! The below code is based on the example given in ! The below code is based on the example given in
! http://msdn2.microsoft.com/en-us/library/ms682499.aspx ! http://msdn2.microsoft.com/en-us/library/ms682499.aspx
: redirect-default ( default obj access-mode create-mode -- handle ) : redirect-default ( default obj access-mode create-mode -- handle )
3drop ; 3drop ;
: redirect-inherit ( default obj access-mode create-mode -- handle )
4drop f ;
: redirect-closed ( default obj access-mode create-mode -- handle ) : redirect-closed ( default obj access-mode create-mode -- handle )
drop 2nip null-pipe ; drop 2nip null-pipe ;
@ -44,21 +54,25 @@ IN: io.windows.nt.launcher
: set-inherit ( handle ? -- ) : set-inherit ( handle ? -- )
>r HANDLE_FLAG_INHERIT r> >BOOLEAN SetHandleInformation win32-error=0/f ; >r HANDLE_FLAG_INHERIT r> >BOOLEAN SetHandleInformation win32-error=0/f ;
: redirect-stream ( default stream access-mode create-mode -- handle ) : redirect-handle ( default handle access-mode create-mode -- handle )
2drop nip 2drop nip
underlying-handle win32-file-handle handle>> duplicate-handle dup t set-inherit ;
duplicate-handle dup t set-inherit ;
: redirect-stream ( default stream access-mode create-mode -- handle )
>r >r underlying-handle r> r> redirect-handle ;
: redirect ( default obj access-mode create-mode -- handle ) : redirect ( default obj access-mode create-mode -- handle )
{ {
{ [ pick not ] [ redirect-default ] } { [ pick not ] [ redirect-default ] }
{ [ pick +closed+ eq? ] [ redirect-closed ] } { [ pick +closed+ eq? ] [ redirect-closed ] }
{ [ pick string? ] [ redirect-file ] } { [ pick string? ] [ redirect-file ] }
{ [ pick appender? ] [ redirect-file ] }
{ [ pick win32-file? ] [ redirect-handle ] }
[ redirect-stream ] [ redirect-stream ]
} cond ; } cond ;
: default-stdout ( args -- handle ) : default-stdout ( args -- handle )
stdout-pipe>> dup [ pipe-out ] when ; stdout-pipe>> dup [ out>> ] when ;
: redirect-stdout ( process args -- handle ) : redirect-stdout ( process args -- handle )
default-stdout default-stdout
@ -84,7 +98,7 @@ IN: io.windows.nt.launcher
] if ; ] if ;
: default-stdin ( args -- handle ) : default-stdin ( args -- handle )
stdin-pipe>> dup [ pipe-in ] when ; stdin-pipe>> dup [ in>> ] when ;
: redirect-stdin ( process args -- handle ) : redirect-stdin ( process args -- handle )
default-stdin default-stdin
@ -94,46 +108,8 @@ IN: io.windows.nt.launcher
redirect redirect
STD_INPUT_HANDLE GetStdHandle or ; STD_INPUT_HANDLE GetStdHandle or ;
: add-pipe-dtors ( pipe -- )
dup
in>> close-later
out>> close-later ;
: fill-stdout-pipe ( args -- args )
<unique-incoming-pipe>
dup add-pipe-dtors
dup pipe-in f set-inherit
>>stdout-pipe ;
: fill-stdin-pipe ( args -- args )
<unique-outgoing-pipe>
dup add-pipe-dtors
dup pipe-out f set-inherit
>>stdin-pipe ;
M: winnt fill-redirection ( process args -- ) M: winnt fill-redirection ( process args -- )
[ 2dup redirect-stdout ] keep lpStartupInfo>> set-STARTUPINFO-hStdOutput [ 2dup redirect-stdout ] keep lpStartupInfo>> set-STARTUPINFO-hStdOutput
[ 2dup redirect-stderr ] keep lpStartupInfo>> set-STARTUPINFO-hStdError [ 2dup redirect-stderr ] keep lpStartupInfo>> set-STARTUPINFO-hStdError
[ 2dup redirect-stdin ] keep lpStartupInfo>> set-STARTUPINFO-hStdInput [ 2dup redirect-stdin ] keep lpStartupInfo>> set-STARTUPINFO-hStdInput
2drop ; 2drop ;
M: winnt (process-stream)
[
current-directory get (normalize-path) cd
dup make-CreateProcess-args
fill-stdout-pipe
fill-stdin-pipe
tuck fill-redirection
dup call-CreateProcess
dup stdin-pipe>> pipe-in CloseHandle drop
dup stdout-pipe>> pipe-out CloseHandle drop
dup lpProcessInformation>>
over stdout-pipe>> in>> f <win32-file>
rot stdin-pipe>> out>> f <win32-file>
] with-destructors ;

View File

@ -2,4 +2,4 @@ USE: io
USE: namespaces USE: namespaces
"output" write flush "output" write flush
"error" stderr get stream-write stderr get stream-flush "error" error-stream get stream-write error-stream get stream-flush

View File

@ -3,9 +3,9 @@
USING: alien alien.c-types libc destructors locals USING: alien alien.c-types libc destructors locals
kernel math assocs namespaces continuations sequences hashtables kernel math assocs namespaces continuations sequences hashtables
sorting arrays combinators math.bitfields strings system sorting arrays combinators math.bitfields strings system
accessors threads accessors threads splitting
io.backend io.windows io.windows.nt.backend io.monitors io.backend io.windows io.windows.nt.backend io.windows.nt.files
io.nonblocking io.buffers io.files io.timeouts io io.monitors io.nonblocking io.buffers io.files io.timeouts io
windows windows.kernel32 windows.types ; windows windows.kernel32 windows.types ;
IN: io.windows.nt.monitors IN: io.windows.nt.monitors
@ -79,9 +79,12 @@ TUPLE: win32-monitor < monitor port ;
: file-notify-records ( buffer -- seq ) : file-notify-records ( buffer -- seq )
[ (file-notify-records) drop ] { } make ; [ (file-notify-records) drop ] { } make ;
: parse-notify-records ( monitor buffer -- ) :: parse-notify-records ( monitor buffer -- )
file-notify-records buffer file-notify-records [
[ parse-notify-record rot queue-change ] with each ; parse-notify-record
[ monitor path>> prepend-path normalize-path ] dip
monitor queue-change
] each ;
: fill-queue ( monitor -- ) : fill-queue ( monitor -- )
dup port>> check-closed dup port>> check-closed

View File

@ -1,16 +1,16 @@
! Copyright (C) 2007, 2008 Doug Coleman, Slava Pestov. ! Copyright (C) 2007, 2008 Doug Coleman, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: alien alien.c-types arrays destructors io io.windows libc USING: alien alien.c-types arrays destructors io io.windows libc
windows.types math windows.kernel32 windows namespaces kernel windows.types math.bitfields windows.kernel32 windows namespaces
sequences windows.errors assocs math.parser system random kernel sequences windows.errors assocs math.parser system random
combinators accessors io.pipes ; combinators accessors io.pipes io.nonblocking ;
IN: io.windows.nt.pipes IN: io.windows.nt.pipes
! This code is based on ! This code is based on
! http://twistedmatrix.com/trac/browser/trunk/twisted/internet/iocpreactor/process.py ! http://twistedmatrix.com/trac/browser/trunk/twisted/internet/iocpreactor/process.py
: create-named-pipe ( name in-mode -- handle ) : create-named-pipe ( name -- handle )
FILE_FLAG_OVERLAPPED bitor { PIPE_ACCESS_INBOUND FILE_FLAG_OVERLAPPED } flags
PIPE_TYPE_BYTE PIPE_TYPE_BYTE
1 1
4096 4096
@ -19,30 +19,20 @@ IN: io.windows.nt.pipes
security-attributes-inherit security-attributes-inherit
CreateNamedPipe CreateNamedPipe
dup win32-error=0/f dup win32-error=0/f
dup add-completion ; dup add-completion
f <win32-file> ;
: open-other-end ( name out-mode -- handle ) : open-other-end ( name -- handle )
FILE_SHARE_READ FILE_SHARE_WRITE bitor GENERIC_WRITE
{ FILE_SHARE_READ FILE_SHARE_WRITE } flags
security-attributes-inherit security-attributes-inherit
OPEN_EXISTING OPEN_EXISTING
FILE_FLAG_OVERLAPPED FILE_FLAG_OVERLAPPED
f f
CreateFile CreateFile
dup win32-error=0/f dup win32-error=0/f
dup add-completion ; dup add-completion
f <win32-file> ;
: <pipe> ( name in-mode out-mode -- pipe )
[
>r over >r create-named-pipe dup close-later
r> r> open-other-end dup close-later
pipe boa
] with-destructors ;
: <incoming-pipe> ( name -- pipe )
PIPE_ACCESS_INBOUND GENERIC_WRITE <pipe> ;
: <outgoing-pipe> ( name -- pipe )
PIPE_ACCESS_OUTBOUND GENERIC_READ <pipe> ;
: unique-pipe-name ( -- string ) : unique-pipe-name ( -- string )
[ [
@ -54,23 +44,10 @@ IN: io.windows.nt.pipes
millis # millis #
] "" make ; ] "" make ;
: <unique-incoming-pipe> ( -- pipe ) M: winnt (pipe) ( -- pipe )
unique-pipe-name <incoming-pipe> ; [
unique-pipe-name
: <unique-outgoing-pipe> ( -- pipe ) [ create-named-pipe dup close-later ]
unique-pipe-name <outgoing-pipe> ; [ open-other-end dup close-later ]
bi pipe boa
! /dev/null simulation ] with-destructors ;
: null-input ( -- pipe )
<unique-outgoing-pipe>
[ in>> ] [ out>> CloseHandle drop ] bi ;
: null-output ( -- pipe )
<unique-incoming-pipe>
[ in>> CloseHandle drop ] [ out>> ] bi ;
: null-pipe ( mode -- pipe )
{
{ GENERIC_READ [ null-input ] }
{ GENERIC_WRITE [ null-output ] }
} case ;

View File

@ -13,9 +13,9 @@ IN: tools.vocabs.monitor
dup ".factor" tail? [ parent-directory ] when ; dup ".factor" tail? [ parent-directory ] when ;
: chop-vocab-root ( path -- path' ) : chop-vocab-root ( path -- path' )
"resource:" prepend-path (normalize-path) "resource:" prepend-path normalize-path
dup vocab-roots get dup vocab-roots get
[ (normalize-path) ] map [ normalize-path ] map
[ head? ] with find nip [ head? ] with find nip
?head drop ; ?head drop ;
@ -30,7 +30,7 @@ IN: tools.vocabs.monitor
monitor-loop ; monitor-loop ;
: add-monitor-for-path ( path -- ) : add-monitor-for-path ( path -- )
normalize-path dup exists? [ t my-mailbox (monitor) ] when drop ; dup exists? [ t my-mailbox (monitor) ] when drop ;
: monitor-thread ( -- ) : monitor-thread ( -- )
[ [