! See http://factorcode.org/license.txt for BSD license.
!
! Channels - based on ideas from newsqueak
-USING: kernel sequences sequences.lib threads continuations
-random math ;
+USING: accessors kernel random sequences threads ;
IN: channels
TUPLE: channel receivers senders ;
<PRIVATE
: wait ( channel -- )
- [ channel-senders push ] curry
+ [ self ] dip senders>> push
"channel send" suspend drop ;
: (to) ( value receivers -- )
delete-random resume-with yield ;
: notify ( continuation channel -- channel )
- [ channel-receivers push ] keep ;
+ [ receivers>> push ] keep ;
: (from) ( senders -- )
delete-random resume ;
PRIVATE>
-M: channel to ( value channel -- )
- dup channel-receivers
- dup empty? [ drop dup wait to ] [ nip (to) ] if ;
+M: channel to
+ dup receivers>>
+ [ dup wait to ] [ nip (to) ] if-empty ;
-M: channel from ( channel -- value )
- [
- notify channel-senders
- dup empty? [ drop ] [ (from) ] if
- ] curry "channel receive" suspend ;
+M: channel from
+ [ self ] dip
+ notify senders>>
+ [ (from) ] unless-empty
+ "channel receive" suspend ;