1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data alien.syntax
4 classes.struct combinators destructors destructors.private fry
5 io.backend io.backend.unix.multiplexers io.buffers io.files
6 io.ports io.timeouts kernel kernel.private libc locals make math
7 namespaces sequences summary system threads unix unix.ffi
12 CONSTANT: file-mode 0o0666
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 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 ;
70 M: io-timeout summary drop "I/O operation timed out" ;
72 M: unix wait-for-fd ( handle event -- )
73 dup +retry+ eq? [ 2drop ] [
74 [ [ self ] dip handle-fd mx get-global ] dip {
75 { +input+ [ add-input-callback ] }
76 { +output+ [ add-output-callback ] }
78 "I/O" suspend [ io-timeout ] when
81 : wait-for-port ( port event -- )
82 '[ handle>> _ wait-for-fd ] with-timeout ;
86 ERROR: not-a-buffered-port port ;
88 : check-buffered-port ( port -- port )
89 dup buffered-port? [ not-a-buffered-port ] unless ; inline
92 [ check-buffered-port buffer>> ] [ fd>> ] bi*
93 over [ buffer-end ] [ buffer-capacity ] bi read
94 { fixnum } declare dup 0 >= [
98 { EINTR [ 2drop +retry+ ] }
99 { EAGAIN [ 2drop +input+ ] }
104 M: unix (wait-to-read) ( port -- )
106 dup handle>> check-disposed refill dup
107 [ dupd wait-for-port (wait-to-read) ] [ 2drop ] if ;
111 [ check-buffered-port buffer>> ] [ fd>> ] bi*
112 over [ buffer@ ] [ buffer-length ] bi write
113 { fixnum } declare dup 0 >= [
115 buffer-empty? f +output+ ?
118 { EINTR [ 2drop +retry+ ] }
119 { EAGAIN [ 2drop +output+ ] }
124 M: unix (wait-to-write) ( port -- )
126 dup handle>> check-disposed drain
127 [ wait-for-port ] [ drop ] if* ;
129 M: unix io-multiplex ( nanos -- )
130 mx get-global wait-for-events ;
132 ! On Unix, you're not supposed to set stdin to non-blocking
133 ! because the fd might be shared with another process (either
134 ! parent or child). So what we do is have the VM start a thread
135 ! which pumps data from the real stdin to a pipe. We set the
136 ! pipe to non-blocking, and read from it instead of the real
137 ! stdin. Very crufty, but it will suffice until we get native
138 ! threading support at the language level.
139 TUPLE: stdin < disposable control size data ;
143 [ control>> &dispose drop ]
144 [ size>> &dispose drop ]
145 [ data>> &dispose drop ]
149 : wait-for-stdin ( stdin -- size )
150 [ control>> CHAR: X over io:stream-write1 io:stream-flush ]
151 [ size>> ssize_t heap-size swap io:stream-read ssize_t deref ]
154 :: refill-stdin ( buffer stdin size -- )
155 stdin data>> handle-fd buffer buffer-end size read
159 buffer stdin size refill-stdin
164 size = [ "Error reading stdin pipe" throw ] unless
170 buffer>> _ dup wait-for-stdin refill-stdin f
173 M: stdin cancel-operation
174 [ size>> ] [ control>> ] bi [ cancel-operation ] bi@ ;
176 : control-write-fd ( -- fd ) &: control_write uint deref ;
178 : size-read-fd ( -- fd ) &: size_read uint deref ;
180 : data-read-fd ( -- fd ) &: stdin_read uint deref ;
182 : <stdin> ( -- stdin )
184 control-write-fd <fd> <output-port> >>control
185 size-read-fd <fd> init-fd <input-port> >>size
186 data-read-fd <fd> >>data ;
188 SYMBOL: dispatch-signal-hook
190 dispatch-signal-hook [ [ drop ] ] initialize
192 : signal-pipe-fd ( -- n )
193 OBJ-SIGNAL-PIPE special-object ; inline
195 : signal-pipe-loop ( port -- )
197 int heap-size _ io:stream-read
198 dup [ int deref dispatch-signal-hook get call( x -- ) ] when*
201 : start-signal-pipe-thread ( -- )
203 <fd> init-fd <input-port>
204 '[ _ signal-pipe-loop ] "Signals" spawn drop
213 ! mx io-task for embedding an fd-based mx inside another mx
214 TUPLE: mx-port < port mx ;
216 : <mx-port> ( mx -- port )
217 dup fd>> mx-port <port> swap >>mx ;
219 : multiplexer-error ( n -- n )
221 errno [ EAGAIN = ] [ EINTR = ] bi or
222 [ drop 0 ] [ throw-errno ] if
225 :: ?flag ( n mask symbol -- n )
226 n mask bitand 0 > [ symbol , ] when n ;