]> gitweb.factorcode.org Git - factor.git/blob - basis/io/backend/unix/unix.factor
Merge branch 'master' of git://factorcode.org/git/factor into s3
[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.syntax generic assocs kernel
4 kernel.private math io.ports sequences strings sbufs threads
5 unix unix.ffi vectors io.buffers io.backend io.encodings math.parser
6 continuations system libc namespaces make io.timeouts
7 io.encodings.utf8 destructors destructors.private accessors
8 summary combinators locals unix.time unix.types fry
9 io.backend.unix.multiplexers ;
10 QUALIFIED: io
11 IN: io.backend.unix
12
13 GENERIC: handle-fd ( handle -- fd )
14
15 TUPLE: fd < disposable fd ;
16
17 : init-fd ( fd -- fd )
18     [
19         |dispose
20         dup fd>> F_SETFL O_NONBLOCK [ fcntl ] unix-system-call drop
21         dup fd>> F_SETFD FD_CLOEXEC [ fcntl ] unix-system-call drop
22     ] with-destructors ;
23
24 : <fd> ( n -- fd )
25     fd new-disposable swap >>fd ;
26
27 M: fd dispose
28     dup disposed>> [ drop ] [
29         {
30             [ cancel-operation ]
31             [ t >>disposed drop ]
32             [ unregister-disposable ]
33             [ fd>> close-file ]
34         } cleave
35     ] if ;
36
37 M: fd handle-fd dup check-disposed fd>> ;
38
39 M: fd cancel-operation ( fd -- )
40     dup disposed>> [ drop ] [
41         fd>>
42         mx get-global
43         [ remove-input-callbacks [ t swap resume-with ] each ]
44         [ remove-output-callbacks [ t swap resume-with ] each ]
45         2bi
46     ] if ;
47
48 M: unix tell-handle ( handle -- n )
49     fd>> 0 SEEK_CUR [ lseek ] unix-system-call [ io-error ] [ ] bi ;
50
51 M: unix seek-handle ( n seek-type handle -- )
52     swap {
53         { io:seek-absolute [ SEEK_SET ] }
54         { io:seek-relative [ SEEK_CUR ] }
55         { io:seek-end [ SEEK_END ] }
56         [ io:bad-seek-type ]
57     } case
58     [ fd>> swap ] dip [ lseek ] unix-system-call drop ;
59
60 SYMBOL: +retry+ ! just try the operation again without blocking
61 SYMBOL: +input+
62 SYMBOL: +output+
63
64 ERROR: io-timeout ;
65
66 M: io-timeout summary drop "I/O operation timed out" ;
67
68 : wait-for-fd ( handle event -- )
69     dup +retry+ eq? [ 2drop ] [
70         '[
71             swap handle-fd mx get-global _ {
72                 { +input+ [ add-input-callback ] }
73                 { +output+ [ add-output-callback ] }
74             } case
75         ] "I/O" suspend nip [ io-timeout ] when
76     ] if ;
77
78 : wait-for-port ( port event -- )
79     '[ handle>> _ wait-for-fd ] with-timeout ;
80
81 ! Some general stuff
82 CONSTANT: file-mode OCT: 0666
83  
84 ! Readers
85 : (refill) ( port -- n )
86     [ handle>> ]
87     [ buffer>> buffer-end ]
88     [ buffer>> buffer-capacity ] tri read ;
89
90 ! Returns an event to wait for which will ensure completion of
91 ! this request
92 GENERIC: refill ( port handle -- event/f )
93
94 M: fd refill
95     fd>> over buffer>> [ buffer-end ] [ buffer-capacity ] bi read
96     {
97         { [ dup 0 >= ] [ swap buffer>> n>buffer f ] }
98         { [ errno EINTR = ] [ 2drop +retry+ ] }
99         { [ errno EAGAIN = ] [ 2drop +input+ ] }
100         [ (io-error) ]
101     } cond ;
102
103 M: unix (wait-to-read) ( port -- )
104     dup
105     dup handle>> dup check-disposed refill dup
106     [ dupd wait-for-port (wait-to-read) ] [ 2drop ] if ;
107
108 ! Writers
109 GENERIC: drain ( port handle -- event/f )
110
111 M: fd drain
112     fd>> over buffer>> [ buffer@ ] [ buffer-length ] bi write
113     {
114         { [ dup 0 >= ] [
115             over buffer>> buffer-consume
116             buffer>> buffer-empty? f +output+ ?
117         ] }
118         { [ errno EINTR = ] [ 2drop +retry+ ] }
119         { [ errno EAGAIN = ] [ 2drop +output+ ] }
120         [ (io-error) ]
121     } cond ;
122
123 M: unix (wait-to-write) ( port -- )
124     dup
125     dup handle>> dup check-disposed drain
126     dup [ wait-for-port ] [ 2drop ] if ;
127
128 M: unix io-multiplex ( ms/f -- )
129     mx get-global wait-for-events ;
130
131 ! On Unix, you're not supposed to set stdin to non-blocking
132 ! because the fd might be shared with another process (either
133 ! parent or child). So what we do is have the VM start a thread
134 ! which pumps data from the real stdin to a pipe. We set the
135 ! pipe to non-blocking, and read from it instead of the real
136 ! stdin. Very crufty, but it will suffice until we get native
137 ! threading support at the language level.
138 TUPLE: stdin < disposable control size data ;
139
140 M: stdin dispose*
141     [
142         [ control>> &dispose drop ]
143         [ size>> &dispose drop ]
144         [ data>> &dispose drop ]
145         tri
146     ] with-destructors ;
147
148 : wait-for-stdin ( stdin -- n )
149     [ control>> CHAR: X over io:stream-write1 io:stream-flush ]
150     [ size>> ssize_t heap-size swap io:stream-read *int ]
151     bi ;
152
153 :: refill-stdin ( buffer stdin size -- )
154     stdin data>> handle-fd buffer buffer-end size read
155     dup 0 < [
156         drop
157         errno EINTR = [ buffer stdin size refill-stdin ] [ (io-error) ] if
158     ] [
159         size = [ "Error reading stdin pipe" throw ] unless
160         size buffer n>buffer
161     ] if ;
162
163 M: stdin refill
164     [ buffer>> ] [ dup wait-for-stdin ] bi* refill-stdin f ;
165
166 : control-write-fd ( -- fd ) &: control_write *uint ;
167
168 : size-read-fd ( -- fd ) &: size_read *uint ;
169
170 : data-read-fd ( -- fd ) &: stdin_read *uint ;
171
172 : <stdin> ( -- stdin )
173     stdin new-disposable
174         control-write-fd <fd> <output-port> >>control
175         size-read-fd <fd> init-fd <input-port> >>size
176         data-read-fd <fd> >>data ;
177
178 M: unix init-stdio
179     <stdin> <input-port>
180     1 <fd> <output-port>
181     2 <fd> <output-port>
182     set-stdio ;
183
184 ! mx io-task for embedding an fd-based mx inside another mx
185 TUPLE: mx-port < port mx ;
186
187 : <mx-port> ( mx -- port )
188     dup fd>> mx-port <port> swap >>mx ;
189
190 : multiplexer-error ( n -- n )
191     dup 0 < [
192         errno [ EAGAIN = ] [ EINTR = ] bi or
193         [ drop 0 ] [ (io-error) ] if
194     ] when ;
195
196 :: ?flag ( n mask symbol -- n )
197     n mask bitand 0 > [ symbol , ] when n ;