async-channel.scm 1014 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. (define-record-type async-channel :async-channel
  2. (really-make-async-channel in-channel out-channel)
  3. async-channel?
  4. (in-channel async-channel-in-channel)
  5. (out-channel async-channel-out-channel))
  6. (define (make-async-channel)
  7. (let ((in-channel (make-channel))
  8. (out-channel (make-channel)))
  9. (spawn
  10. (lambda ()
  11. (let ((queue (make-queue)))
  12. (let loop ()
  13. (if (queue-empty? queue)
  14. (begin
  15. (enqueue! queue (receive in-channel))
  16. (loop))
  17. (select
  18. (wrap (receive-rv in-channel)
  19. (lambda (message)
  20. (enqueue! queue message)
  21. (loop)))
  22. (wrap (send-rv out-channel (queue-head queue))
  23. (lambda (ignore)
  24. (dequeue! queue)
  25. (loop)))))))))
  26. (really-make-async-channel in-channel
  27. out-channel)))
  28. (define (send-async channel message)
  29. (send (async-channel-in-channel channel) message))
  30. (define (receive-async-rv channel)
  31. (receive-rv (async-channel-out-channel channel)))
  32. (define (receive-async channel)
  33. (sync (receive-async-rv channel)))