fan-out.scm 957 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env guile
  2. # -*- scheme -*-
  3. !#
  4. (use-modules (ice-9 match)
  5. (fibers)
  6. (fibers channels))
  7. (define (make-fan-out degree make-head make-tail)
  8. (let ((ch (make-head)))
  9. (let lp ((degree degree))
  10. (when (positive? degree)
  11. (make-tail ch)
  12. (lp (1- degree))))))
  13. (define (test degree message-count)
  14. (let ((ch (make-channel)))
  15. (make-fan-out
  16. degree
  17. (lambda () ch)
  18. (lambda (ch)
  19. (spawn-fiber (lambda ()
  20. (let lp () (get-message ch) (lp)))
  21. #:parallel? #t)))
  22. (let lp ((n 0))
  23. (when (< n message-count)
  24. (put-message ch n)
  25. (lp (1+ n))))))
  26. (define (main args)
  27. (match args
  28. ((_ degree message-count)
  29. (let ((degree (string->number degree))
  30. (message-count (string->number message-count)))
  31. (run-fibers (lambda () (test degree message-count)))))))
  32. (when (batch-mode?) (main (program-arguments)))