1 ! Copyright (C) 2007 Chris Double. All Rights Reserved.
2 ! See http://factorcode.org/license.txt for BSD license.
4 ! Channels - based on ideas from newsqueak
5 USING: kernel sequences threads continuations
6 random math accessors random ;
9 TUPLE: channel receivers senders ;
11 : <channel> ( -- channel )
12 V{ } clone V{ } clone channel boa ;
14 GENERIC: to ( value channel -- )
15 GENERIC: from ( channel -- value )
20 [ senders>> push ] curry
21 "channel send" suspend drop ;
23 : (to) ( value receivers -- )
24 delete-random resume-with yield ;
26 : notify ( continuation channel -- channel )
27 [ receivers>> push ] keep ;
29 : (from) ( senders -- )
30 delete-random resume ;
34 M: channel to ( value channel -- )
36 [ dup wait to ] [ nip (to) ] if-empty ;
38 M: channel from ( channel -- value )
41 [ (from) ] unless-empty
42 ] curry "channel receive" suspend ;