]> gitweb.factorcode.org Git - factor.git/blob - basis/io/timeouts/timeouts.factor
core, basis, extra: Remove DOS line endings from files.
[factor.git] / basis / io / timeouts / timeouts.factor
1 ! Copyright (C) 2008 Slava Pestov, Doug Coleman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors fry io io.encodings io.streams.null kernel
4 namespaces timers ;
5 IN: io.timeouts
6
7 GENERIC: timeout ( obj -- dt/f )
8 GENERIC: set-timeout ( dt/f obj -- )
9
10 M: decoder set-timeout stream>> set-timeout ;
11
12 M: encoder set-timeout stream>> set-timeout ;
13
14 GENERIC: cancel-operation ( obj -- )
15
16 : queue-timeout ( obj timeout -- timer )
17     [ '[ _ cancel-operation ] ] dip later ;
18
19 : with-timeout* ( obj timeout quot -- )
20     2over queue-timeout [ nip call ] dip stop-timer ;
21     inline
22
23 : with-timeout ( obj quot -- )
24     over timeout [ [ dup timeout ] dip with-timeout* ] [ call ] if ;
25     inline
26
27 : timeouts ( dt -- )
28     [ input-stream get set-timeout ]
29     [ output-stream get set-timeout ] bi ;
30
31 M: null-stream set-timeout 2drop ;