1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.data alien.syntax generic
4 assocs kernel kernel.private math io.ports sequences strings
5 sbufs threads unix unix.ffi unix.stat vectors io.buffers io.backend
6 io.encodings math.parser continuations system libc namespaces
7 make io.timeouts io.encodings.utf8 destructors
8 destructors.private accessors summary combinators locals
9 unix.time unix.types fry io.backend.unix.multiplexers
14 GENERIC: handle-fd ( handle -- fd )
16 TUPLE: fd < disposable fd ;
18 : init-fd ( fd -- fd )
21 dup fd>> F_SETFL O_NONBLOCK [ fcntl ] unix-system-call drop
22 dup fd>> F_SETFD FD_CLOEXEC [ fcntl ] unix-system-call drop
26 fd new-disposable swap >>fd ;
33 [ unregister-disposable ]
38 M: fd handle-fd dup check-disposed fd>> ;
40 M: fd cancel-operation ( fd -- )
44 [ remove-input-callbacks [ t swap resume-with ] each ]
45 [ remove-output-callbacks [ t swap resume-with ] each ]
49 M: unix tell-handle ( handle -- n )
50 fd>> 0 SEEK_CUR [ lseek ] unix-system-call [ io-error ] [ ] bi ;
52 M: unix seek-handle ( n seek-type handle -- )
54 { io:seek-absolute [ SEEK_SET ] }
55 { io:seek-relative [ SEEK_CUR ] }
56 { io:seek-end [ SEEK_END ] }
59 [ fd>> swap ] dip [ lseek ] unix-system-call drop ;
61 M: unix can-seek-handle? ( handle -- ? )
62 fd>> SEEK_CUR 0 lseek -1 = not ;
64 M: unix handle-length ( handle -- n/f )
65 fd>> \ stat <struct> [ fstat -1 = not ] keep
66 swap [ st_size>> ] [ drop f ] if ;
68 SYMBOL: +retry+ ! just try the operation again without blocking
74 M: io-timeout summary drop "I/O operation timed out" ;
76 : wait-for-fd ( handle event -- )
77 dup +retry+ eq? [ 2drop ] [
78 [ [ self ] dip handle-fd mx get-global ] dip {
79 { +input+ [ add-input-callback ] }
80 { +output+ [ add-output-callback ] }
82 "I/O" suspend [ io-timeout ] when
85 : wait-for-port ( port event -- )
86 '[ handle>> _ wait-for-fd ] with-timeout ;
89 CONSTANT: file-mode OCT: 0666
92 : (refill) ( port -- n )
94 [ buffer>> buffer-end ]
95 [ buffer>> buffer-capacity ] tri read ;
97 ! Returns an event to wait for which will ensure completion of
99 GENERIC: refill ( port handle -- event/f )
102 fd>> over buffer>> [ buffer-end ] [ buffer-capacity ] bi read
104 { [ dup 0 >= ] [ swap buffer>> n>buffer f ] }
105 { [ errno EINTR = ] [ 2drop +retry+ ] }
106 { [ errno EAGAIN = ] [ 2drop +input+ ] }
110 M: unix (wait-to-read) ( port -- )
112 dup handle>> dup check-disposed refill dup
113 [ dupd wait-for-port (wait-to-read) ] [ 2drop ] if ;
116 GENERIC: drain ( port handle -- event/f )
119 fd>> over buffer>> [ buffer@ ] [ buffer-length ] bi write
122 over buffer>> buffer-consume
123 buffer>> buffer-empty? f +output+ ?
125 { [ errno EINTR = ] [ 2drop +retry+ ] }
126 { [ errno EAGAIN = ] [ 2drop +output+ ] }
130 M: unix (wait-to-write) ( port -- )
132 dup handle>> dup check-disposed drain
133 dup [ wait-for-port ] [ 2drop ] if ;
135 M: unix io-multiplex ( ms/f -- )
136 mx get-global wait-for-events ;
138 ! On Unix, you're not supposed to set stdin to non-blocking
139 ! because the fd might be shared with another process (either
140 ! parent or child). So what we do is have the VM start a thread
141 ! which pumps data from the real stdin to a pipe. We set the
142 ! pipe to non-blocking, and read from it instead of the real
143 ! stdin. Very crufty, but it will suffice until we get native
144 ! threading support at the language level.
145 TUPLE: stdin < disposable control size data ;
149 [ control>> &dispose drop ]
150 [ size>> &dispose drop ]
151 [ data>> &dispose drop ]
155 : wait-for-stdin ( stdin -- size )
156 [ control>> CHAR: X over io:stream-write1 io:stream-flush ]
157 [ size>> ssize_t heap-size swap io:stream-read ssize_t deref ]
160 :: refill-stdin ( buffer stdin size -- )
161 stdin data>> handle-fd buffer buffer-end size read
164 errno EINTR = [ buffer stdin size refill-stdin ] [ (io-error) ] if
166 size = [ "Error reading stdin pipe" throw ] unless
172 buffer>> _ dup wait-for-stdin refill-stdin f
175 M: stdin cancel-operation
176 [ size>> ] [ control>> ] bi [ cancel-operation ] bi@ ;
178 : control-write-fd ( -- fd ) &: control_write uint deref ;
180 : size-read-fd ( -- fd ) &: size_read uint deref ;
182 : data-read-fd ( -- fd ) &: stdin_read uint deref ;
184 : <stdin> ( -- stdin )
186 control-write-fd <fd> <output-port> >>control
187 size-read-fd <fd> init-fd <input-port> >>size
188 data-read-fd <fd> >>data ;
190 : signal-pipe-fd ( -- n )
191 OBJ-SIGNAL-PIPE special-object { fixnum } declare ; inline
193 : init-signal-pipe ( -- )
194 signal-pipe-fd <fd> init-fd <input-port>
195 '[ [ 4 _ io:stream-read ] loop ] "Signals" spawn drop ;
203 ! mx io-task for embedding an fd-based mx inside another mx
204 TUPLE: mx-port < port mx ;
206 : <mx-port> ( mx -- port )
207 dup fd>> mx-port <port> swap >>mx ;
209 : multiplexer-error ( n -- n )
211 errno [ EAGAIN = ] [ EINTR = ] bi or
212 [ drop 0 ] [ (io-error) ] if
215 :: ?flag ( n mask symbol -- n )
216 n mask bitand 0 > [ symbol , ] when n ;