1 ! Copyright (C) 2008 Slava Pestov, Doug Coleman
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
3 USING: kernel calendar alarms io io.encodings accessors
\r
7 GENERIC: timeout ( obj -- dt/f )
\r
8 GENERIC: set-timeout ( dt/f obj -- )
\r
10 M: decoder set-timeout stream>> set-timeout ;
\r
12 M: encoder set-timeout stream>> set-timeout ;
\r
14 GENERIC: cancel-operation ( obj -- )
\r
16 : queue-timeout ( obj timeout -- alarm )
\r
17 [ '[ _ cancel-operation ] ] dip later ;
\r
19 : with-timeout* ( obj timeout quot -- )
\r
20 3dup drop queue-timeout [ nip call ] dip cancel-alarm ;
\r
23 : with-timeout ( obj quot -- )
\r
24 over timeout [ [ dup timeout ] dip with-timeout* ] [ call ] if ;
\r
27 : timeouts ( dt -- )
\r
28 [ input-stream get set-timeout ]
\r
29 [ output-stream get set-timeout ] bi ;
\r