]> gitweb.factorcode.org Git - factor.git/blob - basis/concurrency/mailboxes/mailboxes.factor
factor: Make source files/resources 644 instead of 755.
[factor.git] / basis / concurrency / mailboxes / mailboxes.factor
1 ! Copyright (C) 2005, 2010 Chris Double, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors concurrency.conditions continuations deques
4 destructors dlists fry kernel locals sequences threads
5 vocabs.loader ;
6 IN: concurrency.mailboxes
7
8 TUPLE: mailbox { threads dlist } { data dlist } ;
9
10 : <mailbox> ( -- mailbox )
11     mailbox new
12         <dlist> >>threads
13         <dlist> >>data ; inline
14
15 : mailbox-empty? ( mailbox -- bool )
16     data>> deque-empty? ; inline
17
18 GENERIC: mailbox-put ( obj mailbox -- )
19
20 M: mailbox mailbox-put
21     [ data>> push-front ]
22     [ threads>> notify-all ] bi yield ;
23
24 : wait-for-mailbox ( mailbox timeout -- )
25     [ threads>> ] dip "mailbox" wait ; inline
26
27 :: block-unless-pred ( ... mailbox timeout pred: ( ... message -- ... ? ) -- ... )
28     mailbox data>> pred dlist-any? [
29         mailbox timeout wait-for-mailbox
30         mailbox timeout pred block-unless-pred
31     ] unless ; inline recursive
32
33 : block-if-empty ( mailbox timeout -- mailbox )
34     over mailbox-empty? [
35         2dup wait-for-mailbox block-if-empty
36     ] [
37         drop
38     ] if ; inline recursive
39
40 : mailbox-peek ( mailbox -- obj )
41     data>> peek-back ;
42
43 GENERIC#: mailbox-get-timeout 1 ( mailbox timeout -- obj )
44
45 M: mailbox mailbox-get-timeout block-if-empty data>> pop-back ;
46
47 : mailbox-get ( mailbox -- obj )
48     f mailbox-get-timeout ; inline
49
50 : mailbox-get-all-timeout ( mailbox timeout -- seq )
51     block-if-empty data>> [ ] collector [ slurp-deque ] dip ;
52
53 : mailbox-get-all ( mailbox -- seq )
54     f mailbox-get-all-timeout ;
55
56 : while-mailbox-empty ( mailbox quot -- )
57     [ '[ _ mailbox-empty? ] ] dip while ; inline
58
59 : mailbox-get-timeout? ( mailbox timeout pred -- obj )
60     [ block-unless-pred ]
61     [ [ drop data>> ] dip delete-node-if ]
62     3bi ; inline
63
64 : mailbox-get? ( mailbox pred -- obj )
65     f swap mailbox-get-timeout? ; inline
66
67 : wait-for-close-timeout ( mailbox timeout -- )
68     '[
69         _ 2dup wait-for-mailbox wait-for-close-timeout
70     ] unless-disposed ;
71
72 : wait-for-close ( mailbox -- )
73     f wait-for-close-timeout ;
74
75 TUPLE: linked-error error thread ;
76
77 C: <linked-error> linked-error
78
79 : ?linked ( message -- message )
80     dup linked-error? [ rethrow ] when ;
81
82 TUPLE: linked-thread < thread supervisor ;
83
84 M: linked-thread error-in-thread
85     [ <linked-error> ] [ supervisor>> ] bi mailbox-put stop ;
86
87 : <linked-thread> ( quot name mailbox -- thread' )
88     [ linked-thread new-thread ] dip >>supervisor ;
89
90 : spawn-linked-to ( quot name mailbox -- thread )
91     <linked-thread> [ (spawn) ] keep ;
92
93 { "concurrency.mailboxes" "debugger" } "concurrency.mailboxes.debugger" require-when