2008-01-24 02:27:15 -05:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-02-09 22:34:42 -05:00
|
|
|
USING: io io.backend io.timeouts system kernel namespaces
|
|
|
|
strings hashtables sequences assocs combinators vocabs.loader
|
2008-03-04 18:51:40 -05:00
|
|
|
init threads continuations math io.encodings io.streams.duplex
|
2008-04-05 05:26:58 -04:00
|
|
|
io.nonblocking accessors concurrency.flags ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: io.launcher
|
|
|
|
|
2008-04-02 22:27:49 -04:00
|
|
|
TUPLE: process < identity-tuple
|
2008-03-06 21:44:52 -05:00
|
|
|
|
|
|
|
command
|
|
|
|
detached
|
|
|
|
|
|
|
|
environment
|
|
|
|
environment-mode
|
|
|
|
|
|
|
|
stdin
|
|
|
|
stdout
|
|
|
|
stderr
|
|
|
|
|
2008-03-24 19:02:39 -04:00
|
|
|
priority
|
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
timeout
|
|
|
|
|
|
|
|
handle status
|
|
|
|
killed ;
|
|
|
|
|
|
|
|
SYMBOL: +closed+
|
|
|
|
SYMBOL: +inherit+
|
|
|
|
SYMBOL: +stdout+
|
|
|
|
|
|
|
|
SYMBOL: +prepend-environment+
|
|
|
|
SYMBOL: +replace-environment+
|
|
|
|
SYMBOL: +append-environment+
|
|
|
|
|
2008-03-24 19:02:39 -04:00
|
|
|
SYMBOL: +lowest-priority+
|
|
|
|
SYMBOL: +low-priority+
|
|
|
|
SYMBOL: +normal-priority+
|
|
|
|
SYMBOL: +high-priority+
|
|
|
|
SYMBOL: +highest-priority+
|
2008-03-26 16:55:55 -04:00
|
|
|
SYMBOL: +realtime-priority+
|
2008-03-24 19:02:39 -04:00
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
: <process> ( -- process )
|
2008-04-13 16:06:27 -04:00
|
|
|
process new
|
2008-03-06 21:44:52 -05:00
|
|
|
H{ } clone >>environment
|
|
|
|
+append-environment+ >>environment-mode ;
|
|
|
|
|
|
|
|
: process-started? ( process -- ? )
|
|
|
|
dup handle>> swap status>> or ;
|
|
|
|
|
|
|
|
: process-running? ( process -- ? )
|
|
|
|
process-handle >boolean ;
|
|
|
|
|
2008-01-24 03:19:15 -05:00
|
|
|
! Non-blocking process exit notification facility
|
|
|
|
SYMBOL: processes
|
|
|
|
|
|
|
|
[ H{ } clone processes set-global ] "io.launcher" add-init-hook
|
|
|
|
|
2008-04-05 05:26:58 -04:00
|
|
|
HOOK: wait-for-processes io-backend ( -- ? )
|
2008-01-24 03:19:15 -05:00
|
|
|
|
2008-04-05 05:26:58 -04:00
|
|
|
SYMBOL: wait-flag
|
|
|
|
|
|
|
|
: wait-loop ( -- )
|
|
|
|
processes get assoc-empty?
|
|
|
|
[ wait-flag get-global lower-flag ]
|
|
|
|
[ wait-for-processes [ 100 sleep ] when ] if ;
|
|
|
|
|
|
|
|
: start-wait-thread ( -- )
|
|
|
|
<flag> wait-flag set-global
|
|
|
|
[ wait-loop t ] "Process wait" spawn-server drop ;
|
|
|
|
|
|
|
|
[ start-wait-thread ] "io.launcher" add-init-hook
|
2008-01-24 03:19:15 -05:00
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
: process-started ( process handle -- )
|
|
|
|
>>handle
|
2008-04-05 05:26:58 -04:00
|
|
|
V{ } clone swap processes get set-at
|
|
|
|
wait-flag get-global raise-flag ;
|
2008-01-24 02:27:15 -05:00
|
|
|
|
|
|
|
M: process hashcode* process-handle hashcode* ;
|
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
: pass-environment? ( process -- ? )
|
|
|
|
dup environment>> assoc-empty? not
|
|
|
|
swap environment-mode>> +replace-environment+ eq? or ;
|
2008-03-04 16:07:57 -05:00
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
: get-environment ( process -- env )
|
|
|
|
dup environment>>
|
|
|
|
swap environment-mode>> {
|
2008-04-13 23:58:07 -04:00
|
|
|
{ +prepend-environment+ [ os-envs assoc-union ] }
|
|
|
|
{ +append-environment+ [ os-envs swap assoc-union ] }
|
2008-02-05 18:33:36 -05:00
|
|
|
{ +replace-environment+ [ ] }
|
2007-11-13 01:10:26 -05:00
|
|
|
} case ;
|
|
|
|
|
2008-02-09 23:28:22 -05:00
|
|
|
: string-array? ( obj -- ? )
|
|
|
|
dup sequence? [ [ string? ] all? ] [ drop f ] if ;
|
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
GENERIC: >process ( obj -- process )
|
|
|
|
|
|
|
|
M: process >process
|
|
|
|
dup process-started? [
|
|
|
|
"Process has already been started once" throw
|
|
|
|
] when
|
|
|
|
clone ;
|
|
|
|
|
|
|
|
M: object >process <process> swap >>command ;
|
2007-11-12 23:18:42 -05:00
|
|
|
|
2008-02-15 00:29:06 -05:00
|
|
|
HOOK: current-process-handle io-backend ( -- handle )
|
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
HOOK: run-process* io-backend ( process -- handle )
|
2007-11-12 23:18:42 -05:00
|
|
|
|
2008-01-24 02:27:15 -05:00
|
|
|
: wait-for-process ( process -- status )
|
2008-02-09 22:34:42 -05:00
|
|
|
[
|
2008-03-06 21:44:52 -05:00
|
|
|
dup handle>>
|
2008-02-19 15:38:02 -05:00
|
|
|
[
|
|
|
|
dup [ processes get at push ] curry
|
|
|
|
"process" suspend drop
|
|
|
|
] when
|
2008-03-06 21:44:52 -05:00
|
|
|
dup killed>>
|
|
|
|
[ "Process was killed" throw ] [ status>> ] if
|
2008-02-09 22:34:42 -05:00
|
|
|
] with-timeout ;
|
2007-11-12 23:18:42 -05:00
|
|
|
|
2008-01-31 00:16:20 -05:00
|
|
|
: run-detached ( desc -- process )
|
2008-03-06 21:44:52 -05:00
|
|
|
>process
|
|
|
|
dup dup run-process* process-started
|
|
|
|
dup timeout>> [ over set-timeout ] when* ;
|
|
|
|
|
|
|
|
: run-process ( desc -- process )
|
|
|
|
run-detached
|
|
|
|
dup detached>> [ dup wait-for-process drop ] unless ;
|
2008-01-24 02:27:15 -05:00
|
|
|
|
2008-04-18 14:42:56 -04:00
|
|
|
ERROR: process-failed code ;
|
2008-02-08 22:15:29 -05:00
|
|
|
|
2008-03-11 20:51:58 -04:00
|
|
|
: try-process ( desc -- )
|
2008-02-08 22:15:29 -05:00
|
|
|
run-process wait-for-process dup zero?
|
|
|
|
[ drop ] [ process-failed ] if ;
|
|
|
|
|
2008-02-03 15:23:14 -05:00
|
|
|
HOOK: kill-process* io-backend ( handle -- )
|
|
|
|
|
|
|
|
: kill-process ( process -- )
|
2008-03-06 21:44:52 -05:00
|
|
|
t >>killed
|
|
|
|
handle>> [ kill-process* ] when* ;
|
2008-02-03 15:23:14 -05:00
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
M: process timeout timeout>> ;
|
2008-02-21 20:12:55 -05:00
|
|
|
|
|
|
|
M: process set-timeout set-process-timeout ;
|
2008-02-09 22:56:50 -05:00
|
|
|
|
2008-02-09 22:34:42 -05:00
|
|
|
M: process timed-out kill-process ;
|
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
HOOK: (process-stream) io-backend ( process -- handle in out )
|
2008-01-24 02:27:15 -05:00
|
|
|
|
2008-04-11 17:08:40 -04:00
|
|
|
: <process-stream*> ( desc encoding -- stream process )
|
|
|
|
>r >process dup dup (process-stream) <reader&writer>
|
|
|
|
r> <encoder-duplex> -roll
|
|
|
|
process-started ;
|
2007-11-12 23:18:42 -05:00
|
|
|
|
2008-02-21 16:22:49 -05:00
|
|
|
: <process-stream> ( desc encoding -- stream )
|
2008-04-11 17:08:40 -04:00
|
|
|
<process-stream*> drop ; inline
|
2008-01-24 02:27:15 -05:00
|
|
|
|
2008-02-04 20:38:19 -05:00
|
|
|
: with-process-stream ( desc quot -- status )
|
2008-04-11 17:08:40 -04:00
|
|
|
swap <process-stream*> >r
|
2008-01-24 02:27:15 -05:00
|
|
|
[ swap with-stream ] keep
|
2008-04-11 17:08:40 -04:00
|
|
|
r> wait-for-process ; inline
|
2008-01-24 03:19:15 -05:00
|
|
|
|
2008-03-06 21:44:52 -05:00
|
|
|
: notify-exit ( process status -- )
|
|
|
|
>>status
|
2008-02-18 06:07:40 -05:00
|
|
|
[ processes get delete-at* drop [ resume ] each ] keep
|
2008-03-06 21:44:52 -05:00
|
|
|
f >>handle
|
|
|
|
drop ;
|
2008-03-03 18:44:57 -05:00
|
|
|
|
|
|
|
GENERIC: underlying-handle ( stream -- handle )
|
|
|
|
|
|
|
|
M: port underlying-handle port-handle ;
|
|
|
|
|
|
|
|
M: duplex-stream underlying-handle
|
|
|
|
dup duplex-stream-in underlying-handle
|
|
|
|
swap duplex-stream-out underlying-handle tuck =
|
|
|
|
[ "Invalid duplex stream" throw ] when ;
|