factor/basis/concurrency/mailboxes/mailboxes.factor

97 lines
2.7 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
2008-06-11 03:58:38 -04:00
USING: dlists dequeues threads sequences continuations
destructors namespaces random math quotations words kernel
arrays assocs init system concurrency.conditions accessors
2008-07-30 05:12:17 -04:00
debugger debugger.threads ;
2008-05-15 00:23:12 -04:00
TUPLE: mailbox threads data disposed ;
2008-04-11 08:15:26 -04:00
2008-05-15 00:23:12 -04:00
M: mailbox dispose* threads>> notify-all ;
: <mailbox> ( -- mailbox )
<dlist> <dlist> f mailbox boa ;
: mailbox-empty? ( mailbox -- bool )
2008-06-11 03:58:38 -04:00
data>> dequeue-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-07-18 20:22:59 -04:00
: block-unless-pred ( mailbox timeout pred: ( message -- ? ) -- )
2008-05-15 00:23:12 -04:00
pick check-disposed
2008-04-11 08:15:26 -04:00
pick data>> over dlist-contains? [
3drop
] [
2008-04-11 08:15:26 -04:00
>r 2dup wait-for-mailbox r> block-unless-pred
2008-07-18 20:22:59 -04:00
] if ; inline recursive
: block-if-empty ( mailbox timeout -- mailbox )
2008-05-15 00:23:12 -04:00
over check-disposed
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 ]
[ ] produce nip ;
: mailbox-get-all ( mailbox -- array )
f mailbox-get-all-timeout ;
: while-mailbox-empty ( mailbox quot -- )
2008-07-18 20:22:59 -04:00
[ [ mailbox-empty? ] curry ] dip [ ] while ; 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 -- )
2008-05-15 01:03:21 -04:00
over disposed>>
2008-04-11 08:15:26 -04:00
[ 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 ;
M: linked-error error.
[ thread>> error-in-thread. ] [ error>> error. ] bi ;
2008-04-14 06:19:26 -04:00
C: <linked-error> linked-error
2008-06-08 16:32:55 -04:00
: ?linked ( message -- message )
dup linked-error? [ rethrow ] when ;
2008-02-27 20:24:24 -05:00
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 ;