! Copyright (C) 2007 Chris Double. All Rights Reserved. ! See http://factorcode.org/license.txt for BSD license. ! ! Channels - based on ideas from newsqueak USING: kernel sequences threads continuations random math concurrency ; IN: channels TUPLE: channel receivers senders ; : ( -- channel ) V{ } clone V{ } clone channel construct-boa ; GENERIC: to ( value channel -- ) GENERIC: from ( channel -- value ) M: channel to ( value channel -- ) dup channel-receivers empty? [ [ swap channel-senders push stop ] callcc0 to ] [ channel-receivers random-remove [ continue-with ] in-thread 2drop ] if ; M: channel from ( channel -- value ) dup channel-senders empty? [ [ swap channel-receivers push stop ] callcc1 nip ] [ [ over channel-receivers push channel-senders random-remove continue ] callcc1 nip ] if ;