Skip to content

Instantly share code, notes, and snippets.

@athos
Last active November 29, 2016 06:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athos/37bf3b120196b004ac46 to your computer and use it in GitHub Desktop.
Save athos/37bf3b120196b004ac46 to your computer and use it in GitHub Desktop.
(use gauche.partcont)
(use util.queue)
(define-class <channel> (buf takes puts))
(define (chan)
(make <channel> (make-queue) (make-queue) (make-queue)))
(define-syntax go
(syntax-rules ()
[(_ comm ...)
(reset comm ...)]))
(define (>! c v)
(if (queue-empty? (channel-takes c))
(begin
(enqueue! (channel-buf c) v)
#f)
(shift k
(let ([taker (dequeue! (channel-takes c))])
(enqueue! (channel-puts c) k)
(taker v)))))
(define (<! c)
(if (queue-empty? (channel-buf c))
(shift k
(let ([putter (dequeue! (channel-puts c))])
(enqueue! (channel-takes c) k)
(putter #f)))
(dequeue! (channel-buf c))))
(define c (chan))
(go
(let loop ()
(let1 v (<! c)
(print "received" v)
(loop))))
(go
(let loop ([i 0])
(>! c i)
(print "sent" i)
(loop)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment