factor/extra/concurrency/mailboxes/mailboxes.factor

100 lines
2.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2008 Chris Double, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: concurrency.mailboxes
USING: dlists threads sequences continuations
namespaces random math quotations words kernel arrays assocs
2008-04-11 08:15:26 -04:00
init system concurrency.conditions accessors ;
2008-04-11 08:15:26 -04:00
TUPLE: mailbox threads data closed ;
: check-closed ( mailbox -- )
closed>> [ "Mailbox closed" throw ] when ; inline
M: mailbox dispose
t >>closed threads>> notify-all ;
: <mailbox> ( -- mailbox )
<dlist> <dlist> f mailbox boa ;
: mailbox-empty? ( mailbox -- bool )
2008-04-11 08:15:26 -04:00
data>> dlist-empty? ;
: mailbox-put ( obj mailbox -- )
2008-04-11 08:15:26 -04:00
[ data>> push-front ]
[ threads>> notify-all ] bi yield ;
: wait-for-mailbox ( mailbox timeout -- )
>r threads>> r> "mailbox" wait ;
2008-03-20 21:14:07 -04:00
: block-unless-pred ( mailbox timeout pred -- )
2008-04-11 08:15:26 -04:00
pick check-closed
pick data>> over dlist-contains? [
3drop
] [
2008-04-11 08:15:26 -04:00
>r 2dup wait-for-mailbox r> block-unless-pred
] if ; inline
: block-if-empty ( mailbox timeout -- mailbox )
2008-04-11 08:15:26 -04:00
over check-closed
over mailbox-empty? [
2008-04-11 08:15:26 -04:00
2dup wait-for-mailbox block-if-empty
] [
drop
] if ;
: mailbox-peek ( mailbox -- obj )
2008-04-11 08:15:26 -04:00
data>> peek-back ;
: mailbox-get-timeout ( mailbox timeout -- obj )
2008-04-11 08:15:26 -04:00
block-if-empty data>> pop-back ;
: mailbox-get ( mailbox -- obj )
f mailbox-get-timeout ;
: mailbox-get-all-timeout ( mailbox timeout -- array )
block-if-empty
[ dup mailbox-empty? ]
2008-04-11 08:15:26 -04:00
[ dup data>> pop-back ]
[ ] unfold nip ;
: mailbox-get-all ( mailbox -- array )
f mailbox-get-all-timeout ;
: while-mailbox-empty ( mailbox quot -- )
over mailbox-empty? [
dup >r swap slip r> while-mailbox-empty
] [
2drop
] if ; inline
2008-03-20 21:14:07 -04:00
: mailbox-get-timeout? ( mailbox timeout pred -- obj )
3dup block-unless-pred
2008-04-11 08:15:26 -04:00
nip >r data>> r> delete-node-if ; inline
2008-03-20 21:14:07 -04:00
: mailbox-get? ( mailbox pred -- obj )
f swap mailbox-get-timeout? ; inline
2008-04-11 08:15:26 -04:00
: wait-for-close-timeout ( mailbox timeout -- )
over closed>>
[ 2drop ] [ 2dup wait-for-mailbox wait-for-close-timeout ] if ;
: wait-for-close ( mailbox -- )
f wait-for-close-timeout ;
2008-04-14 06:19:26 -04:00
TUPLE: linked-error error thread ;
2008-04-14 06:19:26 -04:00
C: <linked-error> linked-error
2008-02-27 20:24:24 -05:00
: ?linked dup linked-error? [ rethrow ] when ;
2008-04-14 06:19:26 -04:00
TUPLE: linked-thread < thread supervisor ;
2008-02-27 20:24:24 -05:00
M: linked-thread error-in-thread
2008-04-14 06:19:26 -04:00
[ <linked-error> ] [ supervisor>> ] bi mailbox-put ;
2008-02-27 20:24:24 -05:00
: <linked-thread> ( quot name mailbox -- thread' )
2008-04-14 06:19:26 -04:00
>r linked-thread new-thread r> >>supervisor ;
: spawn-linked-to ( quot name mailbox -- thread )
2008-02-27 20:24:24 -05:00
<linked-thread> [ (spawn) ] keep ;