factor/basis/channels/channels.factor

43 lines
967 B
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2007 Chris Double. All Rights Reserved.
! See http://factorcode.org/license.txt for BSD license.
!
! Channels - based on ideas from newsqueak
2008-09-05 20:29:14 -04:00
USING: kernel sequences threads continuations
random math accessors random ;
2007-09-20 18:09:08 -04:00
IN: channels
TUPLE: channel receivers senders ;
: <channel> ( -- channel )
V{ } clone V{ } clone channel boa ;
2007-09-20 18:09:08 -04:00
GENERIC: to ( value channel -- )
GENERIC: from ( channel -- value )
<PRIVATE
: wait ( channel -- )
2008-08-30 21:38:18 -04:00
[ senders>> push ] curry
2008-02-21 00:13:22 -05:00
"channel send" suspend drop ;
2007-09-20 18:09:08 -04:00
: (to) ( value receivers -- )
2008-02-18 10:08:59 -05:00
delete-random resume-with yield ;
2007-09-20 18:09:08 -04:00
: notify ( continuation channel -- channel )
2008-08-30 21:38:18 -04:00
[ receivers>> push ] keep ;
2007-09-20 18:09:08 -04:00
2008-02-21 00:13:22 -05:00
: (from) ( senders -- )
delete-random resume ;
2007-09-20 18:09:08 -04:00
PRIVATE>
M: channel to ( value channel -- )
2008-08-30 21:38:18 -04:00
dup receivers>>
2008-09-06 20:13:59 -04:00
[ dup wait to ] [ nip (to) ] if-empty ;
2007-09-20 18:09:08 -04:00
M: channel from ( channel -- value )
[
2008-08-30 21:38:18 -04:00
notify senders>>
2008-09-06 20:13:59 -04:00
[ (from) ] unless-empty
2008-02-21 00:13:22 -05:00
] curry "channel receive" suspend ;