1 ! Copyright (C) 2007 Chris Double. All Rights Reserved.
2 ! See https://factorcode.org/license.txt for BSD license.
4 ! Channels - based on ideas from newsqueak
5 USING: accessors kernel random sequences threads ;
8 TUPLE: channel receivers senders ;
10 : <channel> ( -- channel )
11 V{ } clone V{ } clone channel boa ;
13 GENERIC: to ( value channel -- )
14 GENERIC: from ( channel -- value )
19 [ self ] dip senders>> push
20 "channel send" suspend drop ;
22 : (to) ( value receivers -- )
23 delete-random resume-with yield ;
25 : notify ( continuation channel -- channel )
26 [ receivers>> push ] keep ;
28 : (from) ( senders -- )
29 delete-random resume ;
35 [ dup wait to ] [ nip (to) ] if-empty ;
40 [ (from) ] unless-empty
41 "channel receive" suspend ;