factor/basis/concurrency/conditions/conditions.factor

27 lines
856 B
Factor
Raw Normal View History

! Copyright (C) 2008 Slava Pestov.
2008-02-18 10:08:59 -05:00
! See http://factorcode.org/license.txt for BSD license.
2008-08-19 15:06:20 -04:00
USING: deques threads kernel arrays sequences alarms ;
2008-02-18 10:08:59 -05:00
IN: concurrency.conditions
2008-08-19 15:06:20 -04:00
: notify-1 ( deque -- )
dup deque-empty? [ drop ] [ pop-back resume-now ] if ;
2008-02-18 10:08:59 -05:00
2008-08-19 15:06:20 -04:00
: notify-all ( deque -- )
[ resume-now ] slurp-deque ;
: queue-timeout ( queue timeout -- alarm )
#! Add an alarm which removes the current thread from the
#! queue, and resumes it, passing it a value of t.
2008-06-11 03:58:38 -04:00
>r [ self swap push-front* ] keep [
[ delete-node ] [ drop node-value ] 2bi
t swap resume-with
] 2curry r> later ;
2008-02-18 10:08:59 -05:00
: wait ( queue timeout status -- )
over [
>r queue-timeout [ drop ] r> suspend
[ "Timeout" throw ] [ cancel-alarm ] if
] [
>r drop [ push-front ] curry r> suspend drop
] if ;