]> gitweb.factorcode.org Git - factor.git/blob - basis/channels/channels.factor
factor: trim using lists
[factor.git] / basis / channels / channels.factor
1 ! Copyright (C) 2007 Chris Double. All Rights Reserved.
2 ! See http://factorcode.org/license.txt for BSD license.
3 !
4 ! Channels - based on ideas from newsqueak
5 USING: accessors kernel random sequences threads ;
6 IN: channels
7
8 TUPLE: channel receivers senders ;
9
10 : <channel> ( -- channel )
11     V{ } clone V{ } clone channel boa ;
12
13 GENERIC: to ( value channel -- )
14 GENERIC: from ( channel -- value )
15
16 <PRIVATE
17
18 : wait ( channel -- )
19     [ self ] dip senders>> push
20     "channel send" suspend drop ;
21
22 : (to) ( value receivers -- )
23     delete-random resume-with yield ;
24
25 : notify ( continuation channel -- channel )
26     [ receivers>> push ] keep ;
27
28 : (from) ( senders -- )
29     delete-random resume ;
30
31 PRIVATE>
32
33 M: channel to
34     dup receivers>>
35     [ dup wait to ] [ nip (to) ] if-empty ;
36
37 M: channel from
38     [ self ] dip
39     notify senders>>
40     [ (from) ] unless-empty
41     "channel receive" suspend ;