factor/extra/concurrency/conditions/conditions.factor

28 lines
855 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.
USING: dlists dlists.private threads kernel arrays sequences
alarms ;
2008-02-18 10:08:59 -05:00
IN: concurrency.conditions
: notify-1 ( dlist -- )
dup dlist-empty? [ drop ] [ pop-back resume-now ] if ;
2008-02-18 10:08:59 -05:00
: notify-all ( dlist -- )
2008-02-25 18:19:34 -05:00
[ resume-now ] dlist-slurp ;
: 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.
>r self over push-front* [
tuck delete-node
dlist-node-obj 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 ;