 
 GENERIC: send ( message process -- )
 
-: random-64 ( -- id )
-    #! Generate a random id to use for pids
-    "ID" 64 [ drop 10 random CHAR: 0 + ] map append ;
+: random-pid ( -- id ) 8 big-random ;
 
 <PRIVATE
 : make-process ( -- process )

        [ set-promise-value ] keep
        [ t swap set-promise-fulfilled? ] keep
        [ promise-processes ] keep
        0 <vector> swap set-promise-processes
        [ schedule-thread ] each yield
    ] if ;

<PRIVATE
 : (maybe-block-promise) ( promise -- promise )
    #! Block the process if the promise is unfulfilled. This is different from
    #! (mailbox-block-if-empty) in that when a promise is fulfilled, all threads
    #! need to be resumed, rather than just one.
    dup promise-fulfilled? [
        [ swap promise-processes push stop ] callcc0
    ] unless ;
PRIVATE>

: ?promise ( promise -- result )
    (maybe-block-promise) promise-value ;

! ******************************
! Experimental code below

! concurrency.
USING: vectors dlists threads sequences continuations
       namespaces random math quotations words kernel match
       arrays io assocs init shuffle system ;
IN: concurrency

TUPLE: mailbox threads data ;

TUPLE: thread timeout continuation continued? ;

: <thread> ( timeout continuation -- obj )
    >r dup [ millis + ] when r>
    {
        set-thread-timeout
        set-thread-continuation
    } thread construct ;

: make-mailbox ( -- mailbox )
    V{ } clone <dlist> mailbox construct-boa ;
