Changeset 34432 in project


Ignore:
Timestamp:
08/27/17 23:35:07 (3 months ago)
Author:
svnwiki
Message:

Anonymous wiki edit for IP [92.221.220.145]:

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/gochan

    r34020 r34432  
    1 
    21== chicken-gochan
    32[[toc:]]
     
    2928* channels don't have any type information
    3029* sending to a channel that gets closed does not panic, it unblocks
    31   all senders immediately with the {{ok}} flag set to {{#f}}.
     30  all senders immediately with the {{fail}} flag set to non-{{#f}}.
    3231* closing an already closed channel has no effect, and is not an error
    3332  ({{gochan-close}} is idempotent).
     
    5655will block until a remote end sends/receives.
    5756
    58 <procedure> (gochan-select ((chan <-|-> msg [ ok ]) body ...) ... [(else body ...])</procedure>
     57<procedure> (gochan-select ((chan <-|-> msg [ fail ]) body ...) ... [(else body ...])</procedure>
    5958
    6059This is a channel switch that will send or receive on a single
     
    6968<enscript highlight="scheme">   
    7069(gochan-select
    71   ((chan1 -> msg ok) (if ok (print "chan1 says " msg) (print "chan1 closed!")))
    72   ((chan2 -> msg ok) (if ok (print "chan2 says " msg) (print "chan2 closed!"))))
     70  ((chan1 -> msg fail) (if fail (print "chan1 closed!") (print "chan1 says " msg)))
     71  ((chan2 -> msg fail) (if fail (print "chan2 closed!") (print "chan2 says " msg))))
    7372</enscript>
    7473
    75 Receive clauses, {{((chan -> msg [ok]) body ...)}}, execute {{body}} with
    76 {{msg}} bound to the message object and {{ok}} bound to a flag indicating
    77 success. Receiving from a closed channel immediately completes with the {{ok}}
    78 flag set to {{#f}}.
     74Receive clauses, {{((chan -> msg [fail]) body ...)}}, execute {{body}}
     75with {{msg}} bound to the message object and {{fail}} bound to a flag
     76indicating failure. Receiving from a closed channel immediately
     77completes with this {{fail}} flag set to non-{{#f}}.
    7978
    80 Send clauses, {{((chan <- msg [ok]) body ...)}}, execute {{body}} after
     79Send clauses, {{((chan <- msg [fail]) body ...)}}, execute {{body}} after
    8180{{msg}} has been sent to a receiver, successfully buffered onto the
    8281channel, or if channel was closed. Sending to a closed channel
    83 immediately completes with the {{ok}} flag set to {{#f}}.
     82immediately completes with the {{fail}} flag set to {{#f}}.
    8483
    85 A send or receive clause on a closed channel with no {{ok}} binding
    86 specified will immediately return {{(void)}} without executing
     84A send or receive clause on a closed channel with no {{fail}}-flag
     85binding specified will immediately return {{(void)}} without executing
    8786{{body}}. This can be combined with recursion like this:
    8887
     
    101100(let loop ((chan2 chan2))
    102101  (gochan-select
    103     ((chan1 -> msg)    (print "chan1 says " msg) (loop chan2))
    104     ((chan2 -> msg ok) (cond (ok (print "chan2 says " msg) (loop chan2))
    105                              (else (print "chan2 closed, keep going")
    106                                    ;; replace chan2 with new forever-blocking channel:
    107                                    (loop (gochan 0)))))))
     102   ((chan1 -> msg)      (print "chan1 says " msg) (loop chan2))
     103   ((chan2 -> msg fail) (if fail
     104                            (begin (print "chan2 closed, keep going")
     105                                   ;; create new forever-blocking channel
     106                                   (loop (gochan 0)))
     107                            (begin (print "chan2 says " msg)
     108                                   (loop chan2))))))
    108109</enscript>
    109110
     
    114115
    115116<enscript highlight="scheme">   
    116 (gochan-select ((chan1 -> msg ok) (if ok msg #!eof))
     117(gochan-select ((chan1 -> msg fail) (if fail #!eof msg))
    117118               (else 'eagain))
    118119
     
    126127This is short for {{(gochan-select ((chan -> msg) msg))}}.
    127128
    128 <procedure> (gochan-close chan)</procedure>
     129<procedure> (gochan-close chan [fail-flag])</procedure>
    129130
    130131Close the channel. Note that this will unblock existing receivers and
    131 senders waiting for an operation on {{chan}}.
     132senders waiting for an operation on {{chan}} with the {{fail-flag}} set to
     133a non-{{#f}} value. All ''future'' receivers and senders will also
     134immdiately unblock in this way, so watch out for busy-loops.
     135
     136The optional {{fail-flag}} can be used to specify an alternative to the
     137default {{#t}}. As this value is given to all receivers and senders of
     138{{chan}}, the {{fail-flag}} can be used as a "broadcast"
     139mechanism. {{fail-flag}} cannot be {{#f}} as that would indicate a
     140successful message transaction.
    132141
    133142<procedure> (gochan-after duration/ms)</procedure>
    134143
    135 Return a {{gochan}} that will "send" a single message after
     144Returns a {{gochan}} that will "send" a single message after
    136145{{duration/ms}} milliseconds of its creation. The message is the
    137146{{(current-milliseconds)}} value at the time of the timeout (not when
Note: See TracChangeset for help on using the changeset viewer.