io.launcher: cleanup public interface, make some things private or internal.

db4
John Benediktsson 2014-12-30 10:04:09 -08:00
parent e68c0ae8bd
commit 83f7b31910
7 changed files with 93 additions and 76 deletions

View File

@ -1,7 +1,7 @@
! Copyright (C) 2007, 2008 Slava Pestov. ! Copyright (C) 2007, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: calendar help.markup help.syntax io io.files kernel literals math USING: assocs calendar help.markup help.syntax io io.files
quotations sequences ; io.launcher.private kernel literals quotations sequences ;
IN: io.launcher IN: io.launcher
ARTICLE: "io.launcher.command" "Specifying a command" ARTICLE: "io.launcher.command" "Specifying a command"
@ -93,21 +93,21 @@ ARTICLE: "io.launcher.timeouts" "Process run-time timeouts"
"The " { $snippet "timeout" } " slot of a " { $link process } " can be set to a " { $link duration } " specifying a maximum running time for the process. If " { $link wait-for-process } " is called and the process does not exit before the duration expires, it will be killed." ; "The " { $snippet "timeout" } " slot of a " { $link process } " can be set to a " { $link duration } " specifying a maximum running time for the process. If " { $link wait-for-process } " is called and the process does not exit before the duration expires, it will be killed." ;
HELP: get-environment HELP: get-environment
{ $values { "process" process } { "env" "an association" } } { $values { "process" process } { "env" assoc } }
{ $description "Combines the current environment with the value of the " { $snippet "environment" } " slot of the " { $link process } " using the " { $snippet "environment-mode" } " slot." } ; { $description "Combines the current environment with the value of the " { $snippet "environment" } " slot of the " { $link process } " using the " { $snippet "environment-mode" } " slot." } ;
HELP: current-process-handle HELP: (current-process)
{ $values { "handle" "a process handle" } } { $values { "handle" "a process handle" } }
{ $description "Returns the handle of the current process." } { $description "Returns the handle of the current process." }
{ $examples { $examples
{ $example { $example
"USING: io.launcher math prettyprint ;" "USING: io.launcher math prettyprint ;"
"current-process-handle number? ." "(current-process) number? ."
"t" "t"
} }
} ; } ;
HELP: run-process* HELP: (run-process)
{ $values { "process" process } { "handle" "a process handle" } } { $values { "process" process } { "handle" "a process handle" } }
{ $contract "Launches a process." } { $contract "Launches a process." }
{ $notes "User code should call " { $link run-process } " instead." } ; { $notes "User code should call " { $link run-process } " instead." } ;
@ -176,7 +176,7 @@ HELP: kill-process
} }
} ; } ;
HELP: kill-process* HELP: (kill-process)
{ $values { "process" "process" } } { $values { "process" "process" } }
{ $contract "Kills a running process." } { $contract "Kills a running process." }
{ $notes "User code should call " { $link kill-process } " instead." } ; { $notes "User code should call " { $link kill-process } " instead." } ;

View File

@ -1,11 +1,12 @@
! Copyright (C) 2008, 2011 Slava Pestov. ! Copyright (C) 2008, 2011 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: system kernel namespaces strings hashtables sequences
assocs combinators vocabs init threads continuations math USING: accessors assocs calendar combinators concurrency.flags
accessors concurrency.flags destructors environment fry io debugger destructors environment fry init io io.backend
io.encodings.ascii io.backend io.timeouts io.pipes io.encodings io.encodings.utf8 io.pipes io.pipes.private
io.pipes.private io.encodings io.encodings.utf8 io.ports io.streams.duplex io.timeouts kernel namespaces
io.streams.duplex io.ports debugger prettyprint summary calendar ; prettyprint sequences strings system threads vocabs ;
IN: io.launcher IN: io.launcher
TUPLE: process < identity-tuple TUPLE: process < identity-tuple
@ -35,7 +36,7 @@ SYMBOL: +stdout+
TUPLE: appender path ; TUPLE: appender path ;
: <appender> ( path -- appender ) appender boa ; C: <appender> appender
SYMBOL: +prepend-environment+ SYMBOL: +prepend-environment+
SYMBOL: +replace-environment+ SYMBOL: +replace-environment+
@ -67,14 +68,16 @@ SYMBOL: +new-session+
! Non-blocking process exit notification facility ! Non-blocking process exit notification facility
SYMBOL: processes SYMBOL: processes
HOOK: wait-for-processes io-backend ( -- ? ) HOOK: (wait-for-processes) io-backend ( -- ? )
<PRIVATE
SYMBOL: wait-flag SYMBOL: wait-flag
: wait-loop ( -- ) : wait-loop ( -- )
processes get assoc-empty? processes get assoc-empty?
[ wait-flag get-global lower-flag ] [ wait-flag get-global lower-flag ]
[ wait-for-processes [ 100 milliseconds sleep ] when ] if ; [ (wait-for-processes) [ 100 milliseconds sleep ] when ] if ;
: start-wait-thread ( -- ) : start-wait-thread ( -- )
<flag> wait-flag set-global <flag> wait-flag set-global
@ -95,15 +98,13 @@ SYMBOL: wait-flag
swap environment-mode>> +replace-environment+ eq? or ; swap environment-mode>> +replace-environment+ eq? or ;
: get-environment ( process -- env ) : get-environment ( process -- env )
dup environment>> [ environment>> ] [ environment-mode>> ] bi {
swap environment-mode>> {
{ +prepend-environment+ [ os-envs assoc-union ] } { +prepend-environment+ [ os-envs assoc-union ] }
{ +append-environment+ [ os-envs swap assoc-union ] } { +append-environment+ [ os-envs swap assoc-union ] }
{ +replace-environment+ [ ] } { +replace-environment+ [ ] }
} case ; } case ;
: string-array? ( obj -- ? ) PRIVATE>
dup sequence? [ [ string? ] all? ] [ drop f ] if ;
GENERIC: >process ( obj -- process ) GENERIC: >process ( obj -- process )
@ -115,16 +116,12 @@ M: process-already-started error.
process>> . ; process>> . ;
M: process >process M: process >process
dup process-started? [ dup process-started? [ process-already-started ] when
process-already-started
] when
clone ; clone ;
M: object >process <process> swap >>command ; M: object >process <process> swap >>command ;
HOOK: current-process-handle io-backend ( -- handle ) HOOK: (current-process) io-backend ( -- handle )
HOOK: run-process* io-backend ( process -- handle )
ERROR: process-was-killed process ; ERROR: process-was-killed process ;
@ -143,8 +140,10 @@ M: process-was-killed error.
: wait-for-process ( process -- status ) : wait-for-process ( process -- status )
[ (wait-for-process) ] with-timeout ; [ (wait-for-process) ] with-timeout ;
HOOK: (run-process) io-backend ( process -- handle )
: run-detached ( desc -- process ) : run-detached ( desc -- process )
>process [ dup run-process* process-started ] keep ; >process [ dup (run-process) process-started ] keep ;
: run-process ( desc -- process ) : run-process ( desc -- process )
run-detached run-detached
@ -164,12 +163,12 @@ M: process-failed error.
: try-process ( desc -- ) : try-process ( desc -- )
run-process wait-for-success ; run-process wait-for-success ;
HOOK: kill-process* io-backend ( process -- ) HOOK: (kill-process) io-backend ( process -- )
: kill-process ( process -- ) : kill-process ( process -- )
t >>killed t >>killed
[ pipe>> [ dispose ] when* ] [ pipe>> [ dispose ] when* ]
[ dup handle>> [ kill-process* ] [ drop ] if ] bi ; [ dup handle>> [ (kill-process) ] [ drop ] if ] bi ;
M: process timeout timeout>> ; M: process timeout timeout>> ;
@ -178,19 +177,23 @@ M: process set-timeout timeout<< ;
M: process cancel-operation kill-process ; M: process cancel-operation kill-process ;
M: object run-pipeline-element M: object run-pipeline-element
[ >process swap >>stdout swap >>stdin run-detached ] [
[ [ drop [ [ &dispose drop ] when* ] bi@ ] with-destructors ] >process
3bi swap >>stdout
wait-for-process ; swap >>stdin
run-detached
] [
[
drop [ [ &dispose drop ] when* ] bi@
] with-destructors
] 3bi wait-for-process ;
<PRIVATE <PRIVATE
: <process-with-pipe> ( desc -- process pipe ) : <process-with-pipe> ( desc -- process pipe )
>process (pipe) |dispose [ >>pipe ] keep ; >process (pipe) |dispose [ >>pipe ] keep ;
PRIVATE> : (process-reader) ( desc encoding -- stream process )
: <process-reader*> ( desc encoding -- stream process )
[ [
[ [
<process-with-pipe> { <process-with-pipe> {
@ -202,15 +205,19 @@ PRIVATE>
] dip <decoder> swap ] dip <decoder> swap
] with-destructors ; ] with-destructors ;
PRIVATE>
: <process-reader> ( desc encoding -- stream ) : <process-reader> ( desc encoding -- stream )
<process-reader*> drop ; inline (process-reader) drop ; inline
: with-process-reader ( desc encoding quot -- ) : with-process-reader ( desc encoding quot -- )
[ <process-reader*> ] dip [ (process-reader) ] dip
swap [ with-input-stream ] dip '[ _ with-input-stream ] dip
wait-for-success ; inline wait-for-success ; inline
: <process-writer*> ( desc encoding -- stream process ) <PRIVATE
: (process-writer) ( desc encoding -- stream process )
[ [
[ [
<process-with-pipe> { <process-with-pipe> {
@ -222,15 +229,19 @@ PRIVATE>
] dip <encoder> swap ] dip <encoder> swap
] with-destructors ; ] with-destructors ;
PRIVATE>
: <process-writer> ( desc encoding -- stream ) : <process-writer> ( desc encoding -- stream )
<process-writer*> drop ; inline (process-writer) drop ; inline
: with-process-writer ( desc encoding quot -- ) : with-process-writer ( desc encoding quot -- )
[ <process-writer*> ] dip [ (process-writer) ] dip
swap [ with-output-stream ] dip '[ _ with-output-stream ] dip
wait-for-success ; inline wait-for-success ; inline
: <process-stream*> ( desc encoding -- stream process ) <PRIVATE
: (process-stream) ( desc encoding -- stream process )
[ [
[ [
(pipe) |dispose (pipe) |dispose
@ -247,12 +258,14 @@ PRIVATE>
] dip <encoder-duplex> swap ] dip <encoder-duplex> swap
] with-destructors ; ] with-destructors ;
PRIVATE>
: <process-stream> ( desc encoding -- stream ) : <process-stream> ( desc encoding -- stream )
<process-stream*> drop ; inline (process-stream) drop ; inline
: with-process-stream ( desc encoding quot -- ) : with-process-stream ( desc encoding quot -- )
[ <process-stream*> ] dip [ (process-stream) ] dip
swap [ with-stream ] dip '[ _ with-stream ] dip
wait-for-success ; inline wait-for-success ; inline
ERROR: output-process-error { output string } { process process } ; ERROR: output-process-error { output string } { process process } ;
@ -266,16 +279,20 @@ M: output-process-error error.
>process >process
+stdout+ >>stderr +stdout+ >>stderr
[ +closed+ or ] change-stdin [ +closed+ or ] change-stdin
utf8 <process-reader*> utf8 (process-reader)
[ [ stream-contents ] [ dup (wait-for-process) ] bi* ] with-timeout [ [ stream-contents ] [ dup (wait-for-process) ] bi* ] with-timeout
0 = [ 2drop ] [ output-process-error ] if ; 0 = [ 2drop ] [ output-process-error ] if ;
<PRIVATE
: notify-exit ( process status -- ) : notify-exit ( process status -- )
>>status >>status
[ processes get delete-at* drop [ resume ] each ] keep [ processes get delete-at* drop [ resume ] each ] keep
f >>handle f >>handle
drop ; drop ;
PRIVATE>
{ {
{ [ os unix? ] [ "io.launcher.unix" require ] } { [ os unix? ] [ "io.launcher.unix" require ] }
{ [ os windows? ] [ "io.launcher.windows" require ] } { [ os windows? ] [ "io.launcher.windows" require ] }

View File

@ -161,7 +161,7 @@ IN: io.launcher.unix.tests
[ p fulfill ] [ wait-for-process s fulfill ] bi [ p fulfill ] [ wait-for-process s fulfill ] bi
] in-thread ] in-thread
p 1 seconds ?promise-timeout kill-process* p 1 seconds ?promise-timeout (kill-process)
s 3 seconds ?promise-timeout 0 = s 3 seconds ?promise-timeout 0 =
] ]
] unit-test ] unit-test
@ -173,7 +173,7 @@ IN: io.launcher.unix.tests
"SIGPIPE" signal-names index 1 + "SIGPIPE" signal-names index 1 +
kill io-error ; kill io-error ;
[ ] [ current-process-handle send-sigpipe ] unit-test [ ] [ (current-process) send-sigpipe ] unit-test
! Spawn a process ! Spawn a process
[ T{ signal f 13 } ] [ [ T{ signal f 13 } ] [

View File

@ -1,11 +1,10 @@
! Copyright (C) 2007, 2010 Slava Pestov. ! Copyright (C) 2007, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types alien.data arrays assocs USING: accessors alien.c-types alien.data assocs combinators
combinators continuations environment io io.backend continuations environment io.backend io.backend.unix
io.backend.unix io.files io.files.private io.files.unix io.files.private io.files.unix io.launcher io.launcher.private
io.launcher io.pathnames io.ports kernel libc math io.pathnames io.ports kernel libc math namespaces sequences
namespaces sequences strings system threads unix unix.process simple-tokenizer strings system unix unix.ffi unix.process ;
unix.ffi simple-tokenizer ;
IN: io.launcher.unix IN: io.launcher.unix
: get-arguments ( process -- seq ) : get-arguments ( process -- seq )
@ -90,12 +89,12 @@ IN: io.launcher.unix
255 _exit 255 _exit
f throw ; f throw ;
M: unix current-process-handle ( -- handle ) getpid ; M: unix (current-process) ( -- handle ) getpid ;
M: unix run-process* ( process -- pid ) M: unix (run-process) ( process -- pid )
[ spawn-process ] curry [ ] with-fork ; [ spawn-process ] curry [ ] with-fork ;
M: unix kill-process* ( process -- ) M: unix (kill-process) ( process -- )
[ handle>> SIGTERM ] [ group>> ] bi { [ handle>> SIGTERM ] [ group>> ] bi {
{ +same-group+ [ kill ] } { +same-group+ [ kill ] }
{ +new-group+ [ killpg ] } { +new-group+ [ killpg ] }
@ -111,7 +110,7 @@ TUPLE: signal n ;
: code>status ( code -- obj ) : code>status ( code -- obj )
dup WIFSIGNALED [ WTERMSIG signal boa ] [ WEXITSTATUS ] if ; dup WIFSIGNALED [ WTERMSIG signal boa ] [ WEXITSTATUS ] if ;
M: unix wait-for-processes ( -- ? ) M: unix (wait-for-processes) ( -- ? )
{ int } [ -1 swap WNOHANG waitpid ] with-out-parameters { int } [ -1 swap WNOHANG waitpid ] with-out-parameters
swap dup 0 <= [ swap dup 0 <= [
2drop t 2drop t

View File

@ -4,11 +4,12 @@ USING: accessors alien alien.c-types alien.data arrays assocs
classes classes.struct combinators concurrency.flags classes classes.struct combinators concurrency.flags
continuations debugger destructors init io io.backend continuations debugger destructors init io io.backend
io.backend.windows io.files io.files.private io.files.windows io.backend.windows io.files io.files.private io.files.windows
io.launcher io.pathnames io.pipes io.pipes.windows io.ports io.launcher io.launcher.private io.pathnames io.pipes
kernel libc locals make math namespaces prettyprint sequences io.pipes.windows io.ports kernel libc locals make math
specialized-arrays splitting splitting.monotonic namespaces prettyprint sequences specialized-arrays splitting
strings system threads windows windows.errors windows.handles splitting.monotonic strings system threads windows
windows.kernel32 windows.types combinators.short-circuit ; windows.errors windows.handles windows.kernel32 windows.types
combinators.short-circuit ;
SPECIALIZED-ARRAY: ushort SPECIALIZED-ARRAY: ushort
SPECIALIZED-ARRAY: void* SPECIALIZED-ARRAY: void*
IN: io.launcher.windows IN: io.launcher.windows
@ -132,7 +133,7 @@ TUPLE: CreateProcess-args
fill-startup-info fill-startup-info
nip ; nip ;
M: windows current-process-handle ( -- handle ) M: windows (current-process) ( -- handle )
GetCurrentProcessId ; GetCurrentProcessId ;
ERROR: launch-error process error ; ERROR: launch-error process error ;
@ -143,7 +144,7 @@ M: launch-error error.
"Launch descriptor:" print nl "Launch descriptor:" print nl
process>> . ; process>> . ;
M: windows kill-process* ( process -- ) M: windows (kill-process) ( process -- )
handle>> hProcess>> 255 TerminateProcess win32-error=0/f ; handle>> hProcess>> 255 TerminateProcess win32-error=0/f ;
: dispose-process ( process-information -- ) : dispose-process ( process-information -- )
@ -162,7 +163,7 @@ M: windows kill-process* ( process -- )
over handle>> dispose-process over handle>> dispose-process
notify-exit ; notify-exit ;
M: windows wait-for-processes ( -- ? ) M: windows (wait-for-processes) ( -- ? )
processes get keys dup processes get keys dup
[ handle>> hProcess>> ] void*-array{ } map-as [ handle>> hProcess>> ] void*-array{ } map-as
[ length ] keep 0 0 [ length ] keep 0 0
@ -271,7 +272,7 @@ M: windows wait-for-processes ( -- ? )
[ [ redirect-stderr ] dip hStdError<< ] [ [ redirect-stderr ] dip hStdError<< ]
[ [ redirect-stdin ] dip hStdInput<< ] 3tri ; [ [ redirect-stdin ] dip hStdInput<< ] 3tri ;
M: windows run-process* ( process -- handle ) M: windows (run-process) ( process -- handle )
[ [
[ [
dup make-CreateProcess-args dup make-CreateProcess-args

View File

@ -15,7 +15,7 @@ SINGLETON: gdb-disassembler
:: make-disassemble-cmd ( from to -- ) :: make-disassemble-cmd ( from to -- )
in-file ascii [ in-file ascii [
"attach " write "attach " write
current-process-handle number>string print (current-process) number>string print
"x/" write to from - 4 / number>string write "x/" write to from - 4 / number>string write
"i" write bl from number>string write "i" write bl from number>string write
] with-file-writer ; ] with-file-writer ;

View File

@ -51,7 +51,7 @@ DEFER: to-strings
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: git-id ( -- id ) : git-id ( -- id )
{ "git" "show" } utf8 <process-reader> [ readln ] with-input-stream { "git" "show" } utf8 [ readln ] with-process-reader
" " split second ; " " split second ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!