factor/extra/io/unix/backend/backend.factor

184 lines
4.7 KiB
Factor
Raw Normal View History

2008-01-18 18:18:54 -05:00
! Copyright (C) 2004, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-01-18 18:18:54 -05:00
USING: alien generic assocs kernel kernel.private math
2008-05-13 19:24:46 -04:00
io.ports sequences strings structs sbufs threads unix
vectors io.buffers io.backend io.encodings math.parser
continuations system libc qualified namespaces io.timeouts
2008-05-13 19:24:46 -04:00
io.encodings.utf8 accessors inspector combinators ;
2008-01-18 18:18:54 -05:00
QUALIFIED: io
2007-09-20 18:09:08 -04:00
IN: io.unix.backend
! I/O tasks
GENERIC: handle-fd ( handle -- fd )
2008-05-14 04:55:33 -04:00
TUPLE: fd fd closed ;
: <fd> ( n -- fd ) f fd boa ;
M: fd dispose
dup closed>>
[ drop ] [ t >>closed fd>> close-file ] if ;
M: fd handle-fd fd>> ;
2008-05-13 19:24:46 -04:00
! I/O multiplexers
TUPLE: mx fd reads writes ;
2008-05-13 19:24:46 -04:00
: new-mx ( class -- obj )
new
2008-05-13 19:24:46 -04:00
H{ } clone >>reads
H{ } clone >>writes ; inline
2008-05-13 19:24:46 -04:00
GENERIC: add-input-callback ( thread fd mx -- )
2008-01-21 15:33:43 -05:00
2008-05-13 19:24:46 -04:00
: add-callback ( thread fd assoc -- )
[ ?push ] change-at ;
2008-01-21 15:33:43 -05:00
2008-05-13 19:24:46 -04:00
M: mx add-input-callback reads>> add-callback ;
2008-05-13 19:24:46 -04:00
GENERIC: add-output-callback ( thread fd mx -- )
2007-09-20 18:09:08 -04:00
2008-05-13 19:24:46 -04:00
M: mx add-output-callback writes>> add-callback ;
2008-01-21 15:33:43 -05:00
2008-05-13 19:24:46 -04:00
GENERIC: remove-input-callbacks ( fd mx -- callbacks )
2008-01-21 15:33:43 -05:00
2008-05-13 19:24:46 -04:00
M: mx remove-input-callbacks reads>> delete-at* drop ;
GENERIC: remove-output-callbacks ( fd mx -- callbacks )
M: mx remove-output-callbacks writes>> delete-at* drop ;
2008-01-21 15:33:43 -05:00
GENERIC: wait-for-events ( ms mx -- )
2008-05-13 19:24:46 -04:00
TUPLE: unix-io-error error port ;
2008-05-13 19:24:46 -04:00
: report-error ( error port -- )
tuck unix-io-error boa >>error drop ;
: input-available ( fd mx -- )
remove-input-callbacks [ resume ] each ;
2008-05-13 19:24:46 -04:00
: output-available ( fd mx -- )
remove-output-callbacks [ resume ] each ;
2008-05-13 19:24:46 -04:00
TUPLE: io-timeout ;
2008-01-31 13:27:37 -05:00
2008-05-13 19:24:46 -04:00
M: io-timeout summary drop "I/O operation timed out" ;
2008-05-13 19:24:46 -04:00
M: unix cancel-io ( port -- )
io-timeout new over report-error
handle>> handle-fd mx get-global
[ input-available ] [ output-available ] 2bi ;
SYMBOL: +retry+ ! just try the operation again without blocking
SYMBOL: +input+
SYMBOL: +output+
2008-05-14 04:55:33 -04:00
: wait-for-fd ( handle event -- )
2008-05-13 19:24:46 -04:00
dup +retry+ eq? [ 2drop ] [
[
2008-05-14 04:55:33 -04:00
>r
swap handle-fd
mx get-global
r> {
{ +input+ [ add-input-callback ] }
{ +output+ [ add-output-callback ] }
} case
] curry "I/O" suspend 2drop
2008-05-13 19:24:46 -04:00
] if ;
2007-09-20 18:09:08 -04:00
2008-05-14 04:55:33 -04:00
: wait-for-port ( port event -- )
[ >r dup handle>> r> wait-for-fd ] curry
with-timeout pending-error ;
2007-09-20 18:09:08 -04:00
! Some general stuff
: file-mode OCT: 0666 ;
: (io-error) ( -- * ) err_no strerror throw ;
: check-errno ( -- )
err_no dup zero? [ drop ] [ strerror throw ] if ;
2007-09-20 18:09:08 -04:00
: check-null ( n -- ) zero? [ (io-error) ] when ;
: io-error ( n -- ) 0 < [ (io-error) ] when ;
2008-05-14 04:55:33 -04:00
M: fd init-handle ( fd -- )
2007-09-20 18:09:08 -04:00
#! We drop the error code rather than calling io-error,
#! since on OS X 10.3, this operation fails from init-io
#! when running the Factor.app (presumably because fd 0 and
#! 1 are closed).
2008-05-14 04:55:33 -04:00
fd>>
2008-05-05 04:15:24 -04:00
[ F_SETFL O_NONBLOCK fcntl drop ]
[ F_SETFD FD_CLOEXEC fcntl drop ] bi ;
2007-09-20 18:09:08 -04:00
2008-05-14 04:55:33 -04:00
M: fd close-handle ( fd -- ) dispose ;
2007-11-07 14:01:45 -05:00
2007-09-20 18:09:08 -04:00
! Readers
2008-05-13 19:24:46 -04:00
: eof ( reader -- )
dup buffer>> buffer-empty? [ t >>eof ] when drop ;
2007-09-20 18:09:08 -04:00
: (refill) ( port -- n )
[ handle>> ]
[ buffer>> buffer-end ]
[ buffer>> buffer-capacity ] tri read ;
2007-09-20 18:09:08 -04:00
2008-05-13 19:24:46 -04:00
! Returns an event to wait for which will ensure completion of
! this request
GENERIC: refill ( port handle -- event/f )
2008-05-14 04:55:33 -04:00
M: fd refill
fd>> over buffer>> [ buffer-end ] [ buffer-capacity ] bi read
2008-05-13 19:24:46 -04:00
{
{ [ dup 0 = ] [ drop eof f ] }
{ [ dup 0 > ] [ swap buffer>> n>buffer f ] }
{ [ err_no EINTR = ] [ 2drop +retry+ ] }
{ [ err_no EAGAIN = ] [ 2drop +input+ ] }
[ (io-error) ]
} cond ;
M: unix (wait-to-read) ( port -- )
dup dup handle>> refill dup
[ dupd wait-for-port (wait-to-read) ] [ 2drop ] if ;
2007-09-20 18:09:08 -04:00
! Writers
2008-05-13 19:24:46 -04:00
GENERIC: drain ( port handle -- event/f )
2008-05-14 04:55:33 -04:00
M: fd drain
fd>> over buffer>> [ buffer@ ] [ buffer-length ] bi write
2008-05-13 19:24:46 -04:00
{
{ [ dup 0 >= ] [
over buffer>> buffer-consume
buffer>> buffer-empty? f +output+ ?
] }
{ [ err_no EINTR = ] [ 2drop +retry+ ] }
{ [ err_no EAGAIN = ] [ 2drop +output+ ] }
[ (io-error) ]
} cond ;
M: unix (wait-to-write) ( port -- )
dup dup handle>> drain dup
[ dupd wait-for-port (wait-to-write) ] [ 2drop ] if ;
2007-09-20 18:09:08 -04:00
2008-04-02 21:09:56 -04:00
M: unix io-multiplex ( ms/f -- )
2008-02-09 22:34:42 -05:00
mx get-global wait-for-events ;
2007-09-20 18:09:08 -04:00
2008-04-02 21:09:56 -04:00
M: unix (init-stdio) ( -- )
2008-05-14 04:55:33 -04:00
0 <fd> <input-port>
1 <fd> <output-port>
2 <fd> <output-port> ;
2008-01-18 19:43:14 -05:00
! mx io-task for embedding an fd-based mx inside another mx
2008-04-11 17:08:40 -04:00
TUPLE: mx-port < port mx ;
: <mx-port> ( mx -- port )
2008-04-11 17:08:40 -04:00
dup fd>> mx-port <port> swap >>mx ;
2008-01-18 19:43:14 -05:00
: multiplexer-error ( n -- )
2008-05-13 19:24:46 -04:00
0 < [
err_no [ EAGAIN = ] [ EINTR = ] bi or [ (io-error) ] unless
] when ;
2008-04-11 10:54:50 -04:00
: ?flag ( n mask symbol -- n )
pick rot bitand 0 > [ , ] [ drop ] if ;