async-channel.scm 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Mike Sperber
  3. (define-record-type async-channel :async-channel
  4. (really-make-async-channel in-channel out-channel)
  5. async-channel?
  6. (in-channel async-channel-in-channel)
  7. (out-channel async-channel-out-channel))
  8. (define (make-async-channel)
  9. (let ((in-channel (make-channel))
  10. (out-channel (make-channel)))
  11. (spawn
  12. (lambda ()
  13. (let ((queue (make-queue)))
  14. (let loop ()
  15. (if (queue-empty? queue)
  16. (begin
  17. (enqueue! queue (receive in-channel))
  18. (loop))
  19. (select
  20. (wrap (receive-rv in-channel)
  21. (lambda (message)
  22. (enqueue! queue message)
  23. (loop)))
  24. (wrap (send-rv out-channel (queue-head queue))
  25. (lambda (ignore)
  26. (dequeue! queue)
  27. (loop)))))))))
  28. (really-make-async-channel in-channel
  29. out-channel)))
  30. (define (send-async channel message)
  31. (send (async-channel-in-channel channel) message))
  32. (define (receive-async-rv channel)
  33. (receive-rv (async-channel-out-channel channel)))
  34. (define (receive-async channel)
  35. (sync (receive-async-rv channel)))