]> gitweb.factorcode.org Git - factor.git/blob - basis/io/backend/unix/unix.factor
use radix literals
[factor.git] / basis / io / backend / unix / unix.factor
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
10 classes.struct hints ;
11 QUALIFIED: io
12 IN: io.backend.unix
13
14 GENERIC: handle-fd ( handle -- fd )
15
16 TUPLE: fd < disposable fd ;
17
18 : init-fd ( fd -- fd )
19     [
20         |dispose
21         dup fd>> F_SETFL O_NONBLOCK [ fcntl ] unix-system-call drop
22         dup fd>> F_SETFD FD_CLOEXEC [ fcntl ] unix-system-call drop
23     ] with-destructors ;
24
25 : <fd> ( n -- fd )
26     fd new-disposable swap >>fd ;
27
28 M: fd dispose
29     [
30         {
31             [ cancel-operation ]
32             [ t >>disposed drop ]
33             [ unregister-disposable ]
34             [ fd>> close-file ]
35         } cleave
36     ] unless-disposed ;
37
38 M: fd handle-fd dup check-disposed fd>> ;
39
40 M: fd cancel-operation ( fd -- )
41     [
42         fd>>
43         mx get-global
44         [ remove-input-callbacks [ t swap resume-with ] each ]
45         [ remove-output-callbacks [ t swap resume-with ] each ]
46         2bi
47     ] unless-disposed ;
48
49 M: unix tell-handle ( handle -- n )
50     fd>> 0 SEEK_CUR [ lseek ] unix-system-call [ io-error ] [ ] bi ;
51
52 M: unix seek-handle ( n seek-type handle -- )
53     swap {
54         { io:seek-absolute [ SEEK_SET ] }
55         { io:seek-relative [ SEEK_CUR ] }
56         { io:seek-end [ SEEK_END ] }
57         [ io:bad-seek-type ]
58     } case
59     [ fd>> swap ] dip [ lseek ] unix-system-call drop ;
60
61 M: unix can-seek-handle? ( handle -- ? )
62     fd>> SEEK_CUR 0 lseek -1 = not ;
63
64 M: unix handle-length ( handle -- n/f )
65     fd>> \ stat <struct> [ fstat -1 = not ] keep
66     swap [ st_size>> ] [ drop f ] if ;
67
68 SYMBOL: +retry+ ! just try the operation again without blocking
69 SYMBOL: +input+
70 SYMBOL: +output+
71
72 ERROR: io-timeout ;
73
74 M: io-timeout summary drop "I/O operation timed out" ;
75
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 ] }
81         } case
82         "I/O" suspend [ io-timeout ] when
83     ] if ;
84
85 : wait-for-port ( port event -- )
86     '[ handle>> _ wait-for-fd ] with-timeout ;
87
88 ! Some general stuff
89 CONSTANT: file-mode 0o0666
90  
91 ! Readers
92 : (refill) ( port -- n )
93     [ handle>> ]
94     [ buffer>> buffer-end ]
95     [ buffer>> buffer-capacity ] tri read ;
96
97 ! Returns an event to wait for which will ensure completion of
98 ! this request
99 GENERIC: refill ( port handle -- event/f )
100
101 M: fd refill
102     fd>> over buffer>> [ buffer-end ] [ buffer-capacity ] bi read
103     {
104         { [ dup 0 >= ] [ swap buffer>> n>buffer f ] }
105         { [ errno EINTR = ] [ 2drop +retry+ ] }
106         { [ errno EAGAIN = ] [ 2drop +input+ ] }
107         [ (io-error) ]
108     } cond ;
109
110 HINTS: M\ fd refill
111     { buffered-port fd } ;
112
113 M: unix (wait-to-read) ( port -- )
114     dup
115     dup handle>> dup check-disposed refill dup
116     [ dupd wait-for-port (wait-to-read) ] [ 2drop ] if ;
117
118 ! Writers
119 GENERIC: drain ( port handle -- event/f )
120
121 M: fd drain
122     fd>> over buffer>> [ buffer@ ] [ buffer-length ] bi write
123     {
124         { [ dup 0 >= ] [
125             over buffer>> buffer-consume
126             buffer>> buffer-empty? f +output+ ?
127         ] }
128         { [ errno EINTR = ] [ 2drop +retry+ ] }
129         { [ errno EAGAIN = ] [ 2drop +output+ ] }
130         [ (io-error) ]
131     } cond ;
132
133 M: unix (wait-to-write) ( port -- )
134     dup
135     dup handle>> dup check-disposed drain
136     dup [ wait-for-port ] [ 2drop ] if ;
137
138 M: unix io-multiplex ( ms/f -- )
139     mx get-global wait-for-events ;
140
141 ! On Unix, you're not supposed to set stdin to non-blocking
142 ! because the fd might be shared with another process (either
143 ! parent or child). So what we do is have the VM start a thread
144 ! which pumps data from the real stdin to a pipe. We set the
145 ! pipe to non-blocking, and read from it instead of the real
146 ! stdin. Very crufty, but it will suffice until we get native
147 ! threading support at the language level.
148 TUPLE: stdin < disposable control size data ;
149
150 M: stdin dispose*
151     [
152         [ control>> &dispose drop ]
153         [ size>> &dispose drop ]
154         [ data>> &dispose drop ]
155         tri
156     ] with-destructors ;
157
158 : wait-for-stdin ( stdin -- size )
159     [ control>> CHAR: X over io:stream-write1 io:stream-flush ]
160     [ size>> ssize_t heap-size swap io:stream-read ssize_t deref ]
161     bi ;
162
163 :: refill-stdin ( buffer stdin size -- )
164     stdin data>> handle-fd buffer buffer-end size read
165     dup 0 < [
166         drop
167         errno EINTR = [ buffer stdin size refill-stdin ] [ (io-error) ] if
168     ] [
169         size = [ "Error reading stdin pipe" throw ] unless
170         size buffer n>buffer
171     ] if ;
172
173 M: stdin refill
174     '[
175         buffer>> _ dup wait-for-stdin refill-stdin f
176     ] with-timeout ;
177
178 M: stdin cancel-operation
179     [ size>> ] [ control>> ] bi [ cancel-operation ] bi@ ;
180
181 : control-write-fd ( -- fd ) &: control_write uint deref ;
182
183 : size-read-fd ( -- fd ) &: size_read uint deref ;
184
185 : data-read-fd ( -- fd ) &: stdin_read uint deref ;
186
187 : <stdin> ( -- stdin )
188     stdin new-disposable
189         control-write-fd <fd> <output-port> >>control
190         size-read-fd <fd> init-fd <input-port> >>size
191         data-read-fd <fd> >>data ;
192
193 SYMBOL: dispatch-signal-hook
194
195 dispatch-signal-hook [ [ drop ] ] initialize
196
197 : signal-pipe-fd ( -- n )
198     OBJ-SIGNAL-PIPE special-object ; inline
199
200 : signal-pipe-loop ( port -- )
201     '[
202         int heap-size _ io:stream-read
203         dup [ int deref dispatch-signal-hook get call( x -- ) ] when*
204     ] loop ;
205
206 : start-signal-pipe-thread ( -- )
207     signal-pipe-fd [
208         <fd> init-fd <input-port>
209         '[ _ signal-pipe-loop ] "Signals" spawn drop
210     ] when* ;
211
212 M: unix init-stdio
213     <stdin> <input-port>
214     1 <fd> <output-port>
215     2 <fd> <output-port>
216     set-stdio ;
217
218 ! mx io-task for embedding an fd-based mx inside another mx
219 TUPLE: mx-port < port mx ;
220
221 : <mx-port> ( mx -- port )
222     dup fd>> mx-port <port> swap >>mx ;
223
224 : multiplexer-error ( n -- n )
225     dup 0 < [
226         errno [ EAGAIN = ] [ EINTR = ] bi or
227         [ drop 0 ] [ (io-error) ] if
228     ] when ;
229
230 :: ?flag ( n mask symbol -- n )
231     n mask bitand 0 > [ symbol , ] when n ;