M: fd handle-fd check-disposed fd>> ;
-M: fd cancel-operation ( fd -- )
+M: fd cancel-operation
[
fd>>
mx get-global
2bi
] unless-disposed ;
-M: unix tell-handle ( handle -- n )
+M: unix tell-handle
fd>> 0 SEEK_CUR [ lseek ] unix-system-call [ io-error ] [ ] bi ;
-M: unix seek-handle ( n seek-type handle -- )
+M: unix seek-handle
swap {
{ io:seek-absolute [ SEEK_SET ] }
{ io:seek-relative [ SEEK_CUR ] }
} case
[ fd>> swap ] dip [ lseek ] unix-system-call drop ;
-M: unix can-seek-handle? ( handle -- ? )
+M: unix can-seek-handle?
fd>> SEEK_CUR 0 lseek -1 = not ;
-M: unix handle-length ( handle -- n/f )
+M: unix handle-length
fd>> \ stat <struct> [ fstat -1 = not ] keep
swap [ st_size>> ] [ drop f ] if ;
M: io-timeout summary drop "I/O operation timed out" ;
-M: unix wait-for-fd ( handle event -- )
+M: unix wait-for-fd
dup +retry+ eq? [ 2drop ] [
[ [ self ] dip handle-fd mx get-global ] dip {
{ +input+ [ add-input-callback ] }
} case
] if ;
-M: unix (wait-to-read) ( port -- )
+M: unix (wait-to-read)
dup
dup handle>> check-disposed refill dup
[ dupd wait-for-port (wait-to-read) ] [ 2drop ] if ;
} case
] if ;
-M: unix (wait-to-write) ( port -- )
+M: unix (wait-to-write)
dup
dup handle>> check-disposed drain
[ wait-for-port ] [ drop ] if* ;
-M: unix io-multiplex ( nanos -- )
+M: unix io-multiplex
mx get-global wait-for-events ;
! On Unix, you're not supposed to set stdin to non-blocking